Add part3
This commit is contained in:
1
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/.gitignore
vendored
Normal file
1
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
elm-stuff
|
||||
40
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/.travis.yml
vendored
Normal file
40
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/.travis.yml
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
sudo: false
|
||||
|
||||
language: node_js
|
||||
node_js: node
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- elm-stuff/build-artifacts
|
||||
- elm-stuff/packages
|
||||
- sysconfcpus
|
||||
os:
|
||||
- linux
|
||||
|
||||
env: ELM_VERSION=0.18.0
|
||||
|
||||
before_install:
|
||||
- echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
|
||||
|
||||
install:
|
||||
- node --version
|
||||
- npm --version
|
||||
- npm install -g elm@$ELM_VERSION elm-test
|
||||
- git clone https://github.com/NoRedInk/elm-ops-tooling
|
||||
- elm-ops-tooling/with_retry.rb elm package install --yes
|
||||
# Faster compile on Travis.
|
||||
- |
|
||||
if [ ! -d sysconfcpus/bin ];
|
||||
then
|
||||
git clone https://github.com/obmarg/libsysconfcpus.git;
|
||||
cd libsysconfcpus;
|
||||
./configure --prefix=$TRAVIS_BUILD_DIR/sysconfcpus;
|
||||
make && make install;
|
||||
cd ..;
|
||||
fi
|
||||
|
||||
before_script:
|
||||
- cd tests && $TRAVIS_BUILD_DIR/sysconfcpus/bin/sysconfcpus -n 2 elm-make --yes Tests.elm && cd ..
|
||||
|
||||
script:
|
||||
- $TRAVIS_BUILD_DIR/sysconfcpus/bin/sysconfcpus -n 2 elm-test
|
||||
120
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/CHANGELOG.md
vendored
Normal file
120
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/CHANGELOG.md
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
### 5.2.0
|
||||
|
||||
#### Additions
|
||||
|
||||
- `withBearerToken`: add an `Authorization: Bearer ` header (@amarantedaniel)
|
||||
- `withExpectJson`: shortcut for using `Http.expectJson` (@dbottisti)
|
||||
- `withExpectString`: shortcut for using `Http.expectString` (@dbottisti)
|
||||
|
||||
#### Stuff
|
||||
|
||||
- Upgrade `elm-test`
|
||||
- Remove my old Twitter handle from README
|
||||
|
||||
### 5.0.0
|
||||
|
||||
This release accomplishes two goals.
|
||||
|
||||
1. The `RequestBuilder` type is no longer opaque. This comes after observing
|
||||
much debate in the community over the merits and drawbacks of hiding details of
|
||||
a library's types from the user. In this case I have determined it no longer
|
||||
makes sense to do so. If `RequestBuilder` is opaque and so is `Http.Request`,
|
||||
there is no longer any opportunity to do introspection, write tests, or create
|
||||
tooling around the `RequestBuilder` type. So we simply expose the internal
|
||||
structure of `RequestBuilder` as a record.
|
||||
|
||||
2. We're taking over the `send` function again. Thanks to a pull request by @s60
|
||||
I am convinced that this will be okay. We can still have the same signature for
|
||||
`send`, and then use tasks inside of `send` to mess around and do extra stuff.
|
||||
This will just require that the docs for `toRequest` be very clear that it is
|
||||
lossy with respect to `HttpBuilder` features. So far there is one new feature
|
||||
that is being brought over from the previous versions, `withCacheBuster`. This
|
||||
will be a foundation for bringing back other stuff and adding new things as
|
||||
well.
|
||||
|
||||
#### Removals
|
||||
|
||||
_None_
|
||||
|
||||
#### Breaking Changes
|
||||
|
||||
- `RequestBuilder a` is no longer opaque
|
||||
|
||||
#### Additions
|
||||
|
||||
- `toTask`: Convert your `RequestBuilder a` into a `Task Http.Error a` with all
|
||||
the extras that `HttpBuilder` has and will have to offer.
|
||||
- `withCacheBuster`: append a cache buster query param with the current
|
||||
timestamp to your request's URL.
|
||||
|
||||
---
|
||||
|
||||
### 4.0.0
|
||||
|
||||
A lot has happened since 3.0.0! The API is smaller, and more focused and I'm
|
||||
excited about that. In particular, `BodyReader` and its friends have been
|
||||
removed. Since this is the official upgrade for Elm 0.18, the new
|
||||
`elm-lang/http` package has a lot of influence. The new `Http` package includes
|
||||
a much more cohesive experience for declaring expectations of response bodies.
|
||||
[See the `Http.Expect` type in `elm-lang/http`](http://package.elm-lang.org/packages/elm-lang/http/1.0.0/Http#Expect).
|
||||
This feature is even reminiscent of `BodyReader`!
|
||||
|
||||
Since we have this as a part of the platform now, all of the `BodyReader`-
|
||||
related features are gone. For these you'll just "use the platform", as they
|
||||
say, and make use of `withExpect` in your builder pipelines. In the future
|
||||
we may include fancier, custom `Expect` formulations for your convenience.
|
||||
|
||||
Secondly, you'll now have the option to convert a `RequestBuilder a` into an
|
||||
`Http.Request a` or send it directly using `HttpBuilder.send`, which has the
|
||||
same signature as `Http.send`. This helps to keep your builder pipelines clean
|
||||
while also leaving you the option to get out an `Http.Request` if you need.
|
||||
|
||||
Long story short, `HttpBuilder` is _just_ about building requests, just like
|
||||
when it started out. The platform covers the rest.
|
||||
|
||||
Here's the list of all changes:
|
||||
|
||||
#### Removals
|
||||
- `url`: use `withQueryParams` instead to add query params to your url
|
||||
- `withBody`: use one of the more specific `with*Body` functions instead
|
||||
- `withMultipartBody`: string multipart bodies are the only type supported by
|
||||
`elm-lang/http` currently, sojust use `withMultipartStringBody` instead
|
||||
- `withMimeType`: the first parameter to `withStringBody` will set your MIME
|
||||
type automatically. Alternatively, set a header with `withHeader`
|
||||
- `withCacheBuster`: since we're giving up control of the send process, we can't
|
||||
chain on a `Task` to get the current time
|
||||
- `withZeroStatusAllowed`: we don't control the send process. You can handle
|
||||
this yourself under the `Http.BadStatus` error when you deal with errors in
|
||||
your send msg tagger.
|
||||
- `BodyReader`: see intro
|
||||
- `stringReader`: see intro
|
||||
- `jsonReader`: see intro
|
||||
- `unitReader`: see intro
|
||||
- `Error`: since we don't control the send process we don't need this
|
||||
- `Response`: same as `Error`
|
||||
- `toSettings`: `Http.Settings` doesn't exist anymore, it was moved under
|
||||
`Http.Request`
|
||||
- `Request`: since we expect you'll need to import `Http` now anyway, you can
|
||||
just import this from `Http`
|
||||
- `Settings`: see `toSettings`
|
||||
|
||||
|
||||
#### Breaking Changes
|
||||
- `RequestBuilder` -> `RequestBuilder a`, where the type parameter is the
|
||||
expected type of the returned payload.
|
||||
- `get`, `post`, etc. return `RequestBuilder ()`. The default is to make no
|
||||
attempt to decode anything, so it is `()`. You can use `withExpect` to attach
|
||||
an `Http.Expect MyType`, which will turn it into a `RequestBuilder MyType`.
|
||||
- `toRequest` returns an `Http.Request a`
|
||||
- `send` wraps `Http.send`, [read up on it to see how it works](http://package.elm-lang.org/packages/elm-lang/http/1.0.0/Http#send).
|
||||
|
||||
|
||||
#### Additions
|
||||
- `withExpect`: attach an `Http.Expect` to the request
|
||||
- `withQueryParams`: decorate the URL with query params
|
||||
|
||||
A sincere thank you to @evancz, @rtfeldman, @bogdanp, and @knewter for time and
|
||||
discussions that helped me make the decisions that led to these changes!
|
||||
|
||||
And a shoutout to @prikhi for taking the time to update the existing API for
|
||||
0.18 and publishing it as [priki/elm-http-builder](http://package.elm-lang.org/packages/prikhi/elm-http-builder/latest).
|
||||
74
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/CODE_OF_CONDUCT.md
vendored
Normal file
74
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/CODE_OF_CONDUCT.md
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as
|
||||
contributors and maintainers pledge to making participation in our project and
|
||||
our community a harassment-free experience for everyone, regardless of age, body
|
||||
size, disability, ethnicity, gender identity and expression, level of experience,
|
||||
nationality, personal appearance, race, religion, or sexual identity and
|
||||
orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment
|
||||
include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or
|
||||
advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic
|
||||
address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable
|
||||
behavior and are expected to take appropriate and fair corrective action in
|
||||
response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or
|
||||
reject comments, commits, code, wiki edits, issues, and other contributions
|
||||
that are not aligned to this Code of Conduct, or to ban temporarily or
|
||||
permanently any contributor for other behaviors that they deem inappropriate,
|
||||
threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces
|
||||
when an individual is representing the project or its community. Examples of
|
||||
representing a project or community include using an official project e-mail
|
||||
address, posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event. Representation of a project may be
|
||||
further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported by contacting the project team at opensource@lukewestby.com. All
|
||||
complaints will be reviewed and investigated and will result in a response that
|
||||
is deemed necessary and appropriate to the circumstances. The project team is
|
||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
||||
Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good
|
||||
faith may face temporary or permanent repercussions as determined by other
|
||||
members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
||||
available at [http://contributor-covenant.org/version/1/4][version]
|
||||
|
||||
[homepage]: http://contributor-covenant.org
|
||||
[version]: http://contributor-covenant.org/version/1/4/
|
||||
21
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/LICENSE
vendored
Normal file
21
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/LICENSE
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Luke Westby
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
58
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/README.md
vendored
Normal file
58
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/README.md
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
# elm-http-builder
|
||||
|
||||
[](https://travis-ci.org/lukewestby/elm-http-builder)
|
||||
|
||||
Chainable functions for building HTTP requests.
|
||||
|
||||
**Need help? Join the #http-builder channel in the [Elm Slack](https://elmlang.herokuapp.com)!**
|
||||
|
||||
|
||||
> Thanks to @fredcy, @rileylark, and @etaque for the original discussion of the
|
||||
API, and to @knewter for pairing and discussion on the 0.18 upgrade.
|
||||
|
||||
## Example
|
||||
|
||||
```elm
|
||||
import Time
|
||||
import Http
|
||||
import HttpBuilder exposing (..)
|
||||
import Json.Decode as Decode
|
||||
import Json.Encode as Encode
|
||||
|
||||
|
||||
itemsDecoder : Decode.Decoder (List String)
|
||||
itemsDecoder =
|
||||
Decode.list Decode.string
|
||||
|
||||
|
||||
itemEncoder : String -> Encode.Value
|
||||
itemEncoder item =
|
||||
Encode.object
|
||||
[ ("item", Encode.string item) ]
|
||||
|
||||
|
||||
handleRequestComplete : Result Http.Error (List String) -> Msg
|
||||
handleRequestComplete result =
|
||||
-- Handle the result
|
||||
|
||||
{-| addItem will send a post request to
|
||||
`"http://example.com/api/items?hello=world"` with the given JSON body, a
|
||||
custom header, and cookies included. It'll try to decode with `itemsDecoder`.
|
||||
-}
|
||||
addItem : String -> Cmd Msg
|
||||
addItem item =
|
||||
HttpBuilder.post "http://example.com/api/items"
|
||||
|> withQueryParams [ ("hello", "world") ]
|
||||
|> withHeader "X-My-Header" "Some Header Value"
|
||||
|> withJsonBody (itemEncoder item)
|
||||
|> withTimeout (10 * Time.second)
|
||||
|> withExpect (Http.expectJson itemsDecoder)
|
||||
|> withCredentials
|
||||
|> send handleRequestComplete
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
I'm happy to receive any feedback and ideas for about additional features. Any
|
||||
input and pull requests are very welcome and encouraged. If you'd like to help
|
||||
or have ideas, get in touch with me at @luke in the elmlang Slack!
|
||||
17
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/elm-package.json
vendored
Normal file
17
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/elm-package.json
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"version": "5.2.0",
|
||||
"summary": "Extra functions for more easily building HTTP requests",
|
||||
"repository": "https://github.com/lukewestby/elm-http-builder.git",
|
||||
"license": "MIT",
|
||||
"source-directories": [
|
||||
"src"
|
||||
],
|
||||
"exposed-modules": [
|
||||
"HttpBuilder"
|
||||
],
|
||||
"dependencies": {
|
||||
"elm-lang/core": "5.0.0 <= v < 6.0.0",
|
||||
"elm-lang/http": "1.0.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.18.0 <= v < 0.19.0"
|
||||
}
|
||||
454
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/src/HttpBuilder.elm
vendored
Normal file
454
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/src/HttpBuilder.elm
vendored
Normal file
@@ -0,0 +1,454 @@
|
||||
module HttpBuilder
|
||||
exposing
|
||||
( RequestBuilder
|
||||
, delete
|
||||
, get
|
||||
, head
|
||||
, options
|
||||
, patch
|
||||
, post
|
||||
, put
|
||||
, send
|
||||
, toRequest
|
||||
, toTask
|
||||
, trace
|
||||
, withBearerToken
|
||||
, withBody
|
||||
, withCacheBuster
|
||||
, withCredentials
|
||||
, withExpect
|
||||
, withExpectJson
|
||||
, withExpectString
|
||||
, withHeader
|
||||
, withHeaders
|
||||
, withJsonBody
|
||||
, withMultipartStringBody
|
||||
, withQueryParams
|
||||
, withStringBody
|
||||
, withTimeout
|
||||
, withUrlEncodedBody
|
||||
)
|
||||
|
||||
{-| Extra helpers for more easily building Http requests that require greater
|
||||
configuration than what is provided by `elm-http` out of the box.
|
||||
|
||||
|
||||
# Start a request
|
||||
|
||||
@docs RequestBuilder, get, post, put, patch, delete, options, trace, head
|
||||
|
||||
|
||||
# Configure request properties
|
||||
|
||||
@docs withHeader, withHeaders, withBody, withStringBody, withJsonBody
|
||||
@docs withMultipartStringBody, withUrlEncodedBody, withTimeout, withCredentials
|
||||
@docs withQueryParams, withExpect, withCacheBuster, withExpectJson
|
||||
@docs withExpectString, withBearerToken
|
||||
|
||||
|
||||
# Make the request
|
||||
|
||||
@docs toRequest, toTask, send
|
||||
|
||||
-}
|
||||
|
||||
-- where
|
||||
|
||||
import Http
|
||||
import Json.Decode exposing (Decoder)
|
||||
import Json.Encode as Encode
|
||||
import Maybe exposing (Maybe(..))
|
||||
import Result exposing (Result(Err, Ok))
|
||||
import String
|
||||
import Task exposing (Task)
|
||||
import Time exposing (Time)
|
||||
|
||||
|
||||
{-| A type for chaining request configuration
|
||||
-}
|
||||
type alias RequestBuilder a =
|
||||
{ method : String
|
||||
, headers : List Http.Header
|
||||
, url : String
|
||||
, body : Http.Body
|
||||
, expect : Http.Expect a
|
||||
, timeout : Maybe Time
|
||||
, withCredentials : Bool
|
||||
, queryParams : List ( String, String )
|
||||
, cacheBuster : Maybe String
|
||||
}
|
||||
|
||||
|
||||
requestWithMethodAndUrl : String -> String -> RequestBuilder ()
|
||||
requestWithMethodAndUrl method url =
|
||||
{ method = method
|
||||
, url = url
|
||||
, headers = []
|
||||
, body = Http.emptyBody
|
||||
, expect = Http.expectStringResponse (\_ -> Ok ())
|
||||
, timeout = Nothing
|
||||
, withCredentials = False
|
||||
, queryParams = []
|
||||
, cacheBuster = Nothing
|
||||
}
|
||||
|
||||
|
||||
{-| Start building a GET request with a given URL
|
||||
|
||||
get "https://example.com/api/items/1"
|
||||
|
||||
-}
|
||||
get : String -> RequestBuilder ()
|
||||
get =
|
||||
requestWithMethodAndUrl "GET"
|
||||
|
||||
|
||||
{-| Start building a POST request with a given URL
|
||||
|
||||
post "https://example.com/api/items"
|
||||
|
||||
-}
|
||||
post : String -> RequestBuilder ()
|
||||
post =
|
||||
requestWithMethodAndUrl "POST"
|
||||
|
||||
|
||||
{-| Start building a PUT request with a given URL
|
||||
|
||||
put "https://example.com/api/items/1"
|
||||
|
||||
-}
|
||||
put : String -> RequestBuilder ()
|
||||
put =
|
||||
requestWithMethodAndUrl "PUT"
|
||||
|
||||
|
||||
{-| Start building a PATCH request with a given URL
|
||||
|
||||
patch "https://example.com/api/items/1"
|
||||
|
||||
-}
|
||||
patch : String -> RequestBuilder ()
|
||||
patch =
|
||||
requestWithMethodAndUrl "PATCH"
|
||||
|
||||
|
||||
{-| Start building a DELETE request with a given URL
|
||||
|
||||
delete "https://example.com/api/items/1"
|
||||
|
||||
-}
|
||||
delete : String -> RequestBuilder ()
|
||||
delete =
|
||||
requestWithMethodAndUrl "DELETE"
|
||||
|
||||
|
||||
{-| Start building a OPTIONS request with a given URL
|
||||
|
||||
options "https://example.com/api/items/1"
|
||||
|
||||
-}
|
||||
options : String -> RequestBuilder ()
|
||||
options =
|
||||
requestWithMethodAndUrl "OPTIONS"
|
||||
|
||||
|
||||
{-| Start building a TRACE request with a given URL
|
||||
|
||||
trace "https://example.com/api/items/1"
|
||||
|
||||
-}
|
||||
trace : String -> RequestBuilder ()
|
||||
trace =
|
||||
requestWithMethodAndUrl "TRACE"
|
||||
|
||||
|
||||
{-| Start building a HEAD request with a given URL
|
||||
|
||||
head "https://example.com/api/items/1"
|
||||
|
||||
-}
|
||||
head : String -> RequestBuilder ()
|
||||
head =
|
||||
requestWithMethodAndUrl "HEAD"
|
||||
|
||||
|
||||
{-| Add a single header to a request
|
||||
|
||||
get "https://example.com/api/items/1"
|
||||
|> withHeader "Content-Type" "application/json"
|
||||
|
||||
-}
|
||||
withHeader : String -> String -> RequestBuilder a -> RequestBuilder a
|
||||
withHeader key value builder =
|
||||
{ builder | headers = Http.header key value :: builder.headers }
|
||||
|
||||
|
||||
{-| Add many headers to a request
|
||||
|
||||
get "https://example.com/api/items/1"
|
||||
|> withHeaders [("Content-Type", "application/json"), ("Accept", "application/json")]
|
||||
|
||||
-}
|
||||
withHeaders : List ( String, String ) -> RequestBuilder a -> RequestBuilder a
|
||||
withHeaders headerPairs builder =
|
||||
{ builder
|
||||
| headers = List.map (uncurry Http.header) headerPairs ++ builder.headers
|
||||
}
|
||||
|
||||
|
||||
{-| Add a bearer token to a request
|
||||
|
||||
get "https://example.com/api/items/1"
|
||||
|> withBearerToken "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYSJ9.MvhYYpYBuN1rUaV0GGnQGvr889zY0xSc20Lnt8nMTfE"
|
||||
|
||||
-}
|
||||
withBearerToken : String -> RequestBuilder a -> RequestBuilder a
|
||||
withBearerToken value builder =
|
||||
{ builder | headers = Http.header "Authorization" ("Bearer " ++ value) :: builder.headers }
|
||||
|
||||
|
||||
{-| Add an Http.Body to the request
|
||||
|
||||
post "https://example.com/api/save-text"
|
||||
|> withBody (Http.stringBody "text/plain" "Hello!")
|
||||
|
||||
-}
|
||||
withBody : Http.Body -> RequestBuilder a -> RequestBuilder a
|
||||
withBody body builder =
|
||||
{ builder | body = body }
|
||||
|
||||
|
||||
{-| Convenience function for adding a string body to a request
|
||||
|
||||
post "https://example.com/api/items/1"
|
||||
|> withStringBody "application/json" """{ "sortBy": "coolness", "take": 10 }"""
|
||||
|
||||
-}
|
||||
withStringBody : String -> String -> RequestBuilder a -> RequestBuilder a
|
||||
withStringBody contentType value =
|
||||
withBody <| Http.stringBody contentType value
|
||||
|
||||
|
||||
{-| Convenience function for adding a JSON body to a request
|
||||
|
||||
params = Json.Encode.object
|
||||
[ ("sortBy", Json.Encode.string "coolness")
|
||||
, ("take", Json.Encode.int 10)
|
||||
]
|
||||
|
||||
post "https://example.com/api/items/1"
|
||||
|> withJsonBody params
|
||||
|
||||
-}
|
||||
withJsonBody : Encode.Value -> RequestBuilder a -> RequestBuilder a
|
||||
withJsonBody value =
|
||||
withBody <| Http.jsonBody value
|
||||
|
||||
|
||||
{-| Convenience function for adding multipart bodies composed of String, String
|
||||
key-value pairs. Since `Http.stringData` is currently the only `Http.Data`
|
||||
creator having this function removes the need to use the `Http.Data` type in
|
||||
your type signatures.
|
||||
|
||||
post "https://example.com/api/items/1"
|
||||
|> withMultipartStringBody [("user", JS.encode user)]
|
||||
|
||||
-}
|
||||
withMultipartStringBody : List ( String, String ) -> RequestBuilder a -> RequestBuilder a
|
||||
withMultipartStringBody partPairs =
|
||||
withBody <| Http.multipartBody <| List.map (uncurry Http.stringPart) partPairs
|
||||
|
||||
|
||||
{-| Convenience function for adding url encoded bodies
|
||||
|
||||
post "https://example.com/api/whatever"
|
||||
|> withUrlEncodedBody [("user", "Luke"), ("pwd", "secret")]
|
||||
|
||||
-}
|
||||
withUrlEncodedBody : List ( String, String ) -> RequestBuilder a -> RequestBuilder a
|
||||
withUrlEncodedBody =
|
||||
joinUrlEncoded >> withStringBody "application/x-www-form-urlencoded"
|
||||
|
||||
|
||||
{-| Set the `timeout` setting on the request
|
||||
|
||||
get "https://example.com/api/items/1"
|
||||
|> withTimeout (10 * Time.second)
|
||||
|
||||
-}
|
||||
withTimeout : Time -> RequestBuilder a -> RequestBuilder a
|
||||
withTimeout timeout builder =
|
||||
{ builder | timeout = Just timeout }
|
||||
|
||||
|
||||
{-| Set the `withCredentials` flag on the request to True. Works via
|
||||
[`XMLHttpRequest#withCredentials`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials)
|
||||
|
||||
get "https://example.com/api/items/1"
|
||||
|> withCredentials
|
||||
|
||||
-}
|
||||
withCredentials : RequestBuilder a -> RequestBuilder a
|
||||
withCredentials builder =
|
||||
{ builder | withCredentials = True }
|
||||
|
||||
|
||||
{-| Choose an `Expect` for the request
|
||||
|
||||
get "https://example.com/api/items/1"
|
||||
|> withExpect (Http.expectJson itemsDecoder)
|
||||
|
||||
-}
|
||||
withExpect : Http.Expect b -> RequestBuilder a -> RequestBuilder b
|
||||
withExpect expect builder =
|
||||
{ builder | expect = expect }
|
||||
|
||||
|
||||
{-| Choose a Json `Expect` for the request
|
||||
|
||||
get "https://example.com/api/items/1"
|
||||
|> withExpectJson itemsDecoder
|
||||
|
||||
-}
|
||||
withExpectJson : Decoder b -> RequestBuilder a -> RequestBuilder b
|
||||
withExpectJson decoder builder =
|
||||
{ builder | expect = Http.expectJson decoder }
|
||||
|
||||
|
||||
{-| Choose a String `Expect` for the request
|
||||
|
||||
get "https://example.com/api/items/1"
|
||||
|> withExpectString
|
||||
|
||||
-}
|
||||
withExpectString : RequestBuilder a -> RequestBuilder String
|
||||
withExpectString builder =
|
||||
{ builder | expect = Http.expectString }
|
||||
|
||||
|
||||
{-| Add some query params to the url for the request
|
||||
|
||||
get "https://example.com/api/items/1"
|
||||
|> withQueryParams [("hello", "world"), ("foo", "bar")]
|
||||
|> withQueryParams [("baz", "qux")]
|
||||
-- sends a request to https://example.com/api/items/1?hello=world&foo=bar&baz=qux
|
||||
|
||||
-}
|
||||
withQueryParams : List ( String, String ) -> RequestBuilder a -> RequestBuilder a
|
||||
withQueryParams queryParams builder =
|
||||
{ builder | queryParams = builder.queryParams ++ queryParams }
|
||||
|
||||
|
||||
{-| Send the request with a Time based cache buster added to the URL.
|
||||
You provide a key for an extra query param, and when the request is sent that
|
||||
query param will be given a value with the current timestamp.
|
||||
|
||||
type Msg
|
||||
= Items (Result Http.Error String)
|
||||
|
||||
get "https://example.com/api/items"
|
||||
|> withExpect (Http.expectString)
|
||||
|> withCacheBuster "cache_buster"
|
||||
|> send Items
|
||||
|
||||
-- makes a request to https://example.com/api/items?cache_buster=1481633217383
|
||||
|
||||
-}
|
||||
withCacheBuster : String -> RequestBuilder a -> RequestBuilder a
|
||||
withCacheBuster paramName builder =
|
||||
{ builder | cacheBuster = Just paramName }
|
||||
|
||||
|
||||
{-| Extract the Http.Request component of the builder in case you want to use it
|
||||
directly. **This function is lossy** and will discard some of the extra stuff
|
||||
that HttpBuilder allows you to do.
|
||||
|
||||
Things that will be lost:
|
||||
|
||||
- Attaching a cache buster to requests using `withCacheBuster`
|
||||
|
||||
-}
|
||||
toRequest : RequestBuilder a -> Http.Request a
|
||||
toRequest builder =
|
||||
let
|
||||
encodedParams =
|
||||
joinUrlEncoded builder.queryParams
|
||||
|
||||
fullUrl =
|
||||
if String.isEmpty encodedParams then
|
||||
builder.url
|
||||
else
|
||||
builder.url ++ "?" ++ encodedParams
|
||||
in
|
||||
Http.request
|
||||
{ method = builder.method
|
||||
, url = fullUrl
|
||||
, headers = builder.headers
|
||||
, body = builder.body
|
||||
, expect = builder.expect
|
||||
, timeout = builder.timeout
|
||||
, withCredentials = builder.withCredentials
|
||||
}
|
||||
|
||||
|
||||
{-| Convert the RequestBuilder to a Task with all options applied. `toTask`
|
||||
differs from `toRequest` in that it retains all extra behavior allowed by
|
||||
HttpBuilder, including
|
||||
|
||||
- Attaching a cache buster to requests using `withCacheBuster`
|
||||
|
||||
-}
|
||||
toTask : RequestBuilder a -> Task Http.Error a
|
||||
toTask builder =
|
||||
case builder.cacheBuster of
|
||||
Just paramName ->
|
||||
toTaskWithCacheBuster paramName builder
|
||||
|
||||
Nothing ->
|
||||
toTaskPlain builder
|
||||
|
||||
|
||||
toTaskPlain : RequestBuilder a -> Task Http.Error a
|
||||
toTaskPlain builder =
|
||||
Http.toTask <| toRequest builder
|
||||
|
||||
|
||||
toTaskWithCacheBuster : String -> RequestBuilder a -> Task Http.Error a
|
||||
toTaskWithCacheBuster paramName builder =
|
||||
let
|
||||
request timestamp =
|
||||
builder
|
||||
|> withQueryParams [ ( paramName, toString timestamp ) ]
|
||||
|> toTaskPlain
|
||||
in
|
||||
Time.now |> Task.andThen request
|
||||
|
||||
|
||||
{-| Send the request
|
||||
-}
|
||||
send : (Result Http.Error a -> msg) -> RequestBuilder a -> Cmd msg
|
||||
send tagger builder =
|
||||
builder
|
||||
|> toTask
|
||||
|> Task.attempt tagger
|
||||
|
||||
|
||||
joinUrlEncoded : List ( String, String ) -> String
|
||||
joinUrlEncoded args =
|
||||
String.join "&" (List.map queryPair args)
|
||||
|
||||
|
||||
queryPair : ( String, String ) -> String
|
||||
queryPair ( key, value ) =
|
||||
queryEscape key ++ "=" ++ queryEscape value
|
||||
|
||||
|
||||
queryEscape : String -> String
|
||||
queryEscape =
|
||||
Http.encodeUri >> replace "%20" "+"
|
||||
|
||||
|
||||
replace : String -> String -> String -> String
|
||||
replace old new =
|
||||
String.split old >> String.join new
|
||||
2
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/tests/.gitignore
vendored
Normal file
2
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/tests/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/elm-stuff/
|
||||
/tests.js
|
||||
13
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/tests/Native/Polyfills.js
vendored
Normal file
13
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/tests/Native/Polyfills.js
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
var _user$project$Native_Polyfills = (function () {
|
||||
global.FormData = function () { this._data = []; };
|
||||
Object.defineProperty(global.FormData.prototype, 'append', {
|
||||
value: function () {
|
||||
this._data.push(Array.prototype.slice.call(arguments));
|
||||
},
|
||||
enumerable: false,
|
||||
});
|
||||
|
||||
return {
|
||||
enabled: true,
|
||||
};
|
||||
}());
|
||||
110
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/tests/Tests.elm
vendored
Normal file
110
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/tests/Tests.elm
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
module Tests exposing (..)
|
||||
|
||||
import Time
|
||||
import Http exposing (Request)
|
||||
import Json.Decode as Decode
|
||||
import Json.Encode as Encode
|
||||
import Test exposing (Test, test, describe)
|
||||
import Expect
|
||||
import HttpBuilder exposing (..)
|
||||
import Native.Polyfills
|
||||
|
||||
|
||||
polyfillsEnabled : Bool
|
||||
polyfillsEnabled =
|
||||
Native.Polyfills.enabled
|
||||
|
||||
|
||||
testDecoder : Decode.Decoder String
|
||||
testDecoder =
|
||||
Decode.at [ "hello" ] Decode.string
|
||||
|
||||
|
||||
alwaysEmptyOk : a -> Result x ()
|
||||
alwaysEmptyOk _ =
|
||||
Ok ()
|
||||
|
||||
|
||||
expectNothing : Http.Expect ()
|
||||
expectNothing =
|
||||
Http.expectStringResponse alwaysEmptyOk
|
||||
|
||||
|
||||
defaultBuilder : RequestBuilder ()
|
||||
defaultBuilder =
|
||||
{ method = "POST"
|
||||
, url = "example.com"
|
||||
, expect = expectNothing
|
||||
, timeout = Nothing
|
||||
, withCredentials = False
|
||||
, headers = []
|
||||
, body = Http.emptyBody
|
||||
, queryParams = []
|
||||
, cacheBuster = Nothing
|
||||
}
|
||||
|
||||
|
||||
all : Test
|
||||
all =
|
||||
describe "HttpBuilder"
|
||||
[ test "polyfills are set up" <|
|
||||
\() -> polyfillsEnabled |> Expect.equal True
|
||||
, test "Request Building" <|
|
||||
\() ->
|
||||
let
|
||||
actual =
|
||||
get "http://example.com"
|
||||
|> withBearerToken "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYSJ9.MvhYYpYBuN1rUaV0GGnQGvr889zY0xSc20Lnt8nMTfE"
|
||||
|> withHeader "Test" "Header"
|
||||
|> withHeaders [ ( "OtherTest", "Header" ) ]
|
||||
|> withStringBody "text/plain" """{ "test": "body" }"""
|
||||
|> withTimeout (10 * Time.second)
|
||||
|> withCredentials
|
||||
|> withQueryParams [ ( "hello", "world" ) ]
|
||||
|> withCacheBuster "cb"
|
||||
|> withExpect expectNothing
|
||||
|
||||
expected =
|
||||
{ method = "GET"
|
||||
, url = "http://example.com"
|
||||
, queryParams = [ ( "hello", "world" ) ]
|
||||
, body = Http.stringBody "text/plain" """{ "test": "body" }"""
|
||||
, timeout = Just (10 * Time.second)
|
||||
, expect = expectNothing
|
||||
, withCredentials = True
|
||||
, headers =
|
||||
[ Http.header "OtherTest" "Header"
|
||||
, Http.header "Test" "Header"
|
||||
, Http.header "Authorization" "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYSJ9.MvhYYpYBuN1rUaV0GGnQGvr889zY0xSc20Lnt8nMTfE"
|
||||
]
|
||||
, cacheBuster = Just "cb"
|
||||
}
|
||||
in
|
||||
Expect.equal expected actual
|
||||
, describe "with*Body functions"
|
||||
[ test "withStringBody applies string directly" <|
|
||||
\() ->
|
||||
post "example.com"
|
||||
|> withStringBody "text/plain" "hello"
|
||||
|> withExpect expectNothing
|
||||
|> Expect.equal { defaultBuilder | body = Http.stringBody "text/plain" "hello" }
|
||||
, test "withJsonBody applies a Json.Value as a string" <|
|
||||
\() ->
|
||||
post "example.com"
|
||||
|> withJsonBody (Encode.string "hello")
|
||||
|> withExpect expectNothing
|
||||
|> Expect.equal { defaultBuilder | body = Http.jsonBody (Encode.string "hello") }
|
||||
, test "withUrlEncodedBody encodes pairs as url components" <|
|
||||
\() ->
|
||||
post "example.com"
|
||||
|> withUrlEncodedBody [ ( "hello", "w orld" ) ]
|
||||
|> withExpect expectNothing
|
||||
|> Expect.equal { defaultBuilder | body = Http.stringBody "application/x-www-form-urlencoded" "hello=w+orld" }
|
||||
, test "withMultipartStringBody passes through to Http.multipart" <|
|
||||
\() ->
|
||||
post "example.com"
|
||||
|> withMultipartStringBody [ ( "hello", "world" ) ]
|
||||
|> withExpect expectNothing
|
||||
|> Expect.equal { defaultBuilder | body = Http.multipartBody [ Http.stringPart "hello" "world" ] }
|
||||
]
|
||||
]
|
||||
18
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/tests/elm-package.json
vendored
Normal file
18
part3/elm-stuff/packages/lukewestby/elm-http-builder/5.2.0/tests/elm-package.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "Sample Elm Test",
|
||||
"repository": "https://github.com/user/project.git",
|
||||
"license": "BSD-3-Clause",
|
||||
"source-directories": [
|
||||
".",
|
||||
"../src"
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"native-modules": true,
|
||||
"dependencies": {
|
||||
"elm-community/elm-test": "4.2.0 <= v < 5.0.0",
|
||||
"elm-lang/core": "5.0.0 <= v < 6.0.0",
|
||||
"elm-lang/http": "1.0.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.18.0 <= v < 0.19.0"
|
||||
}
|
||||
Reference in New Issue
Block a user