Created
May 18, 2017 09:16
-
-
Save Ahrengot/fb549f034e94de9b08b1d9a20935cfd2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Request.CaseData exposing (put) | |
import Data.Case exposing (CaseData) | |
import Json.Decode as Decode exposing (Decoder) | |
import Json.Decode.Pipeline as Pipeline exposing (decode, required) | |
import Json.Encode as Encode exposing (Value) | |
import Http | |
import HttpBuilder | |
type alias Agency = | |
{ name : String | |
} | |
type alias WpAuthor = | |
{ id : Int | |
, name : String | |
} | |
type alias ContactPerson = | |
{ name : String | |
, phone : String | |
, email : String | |
} | |
-- HTTP | |
put : String -> String -> CaseData -> Http.Request CaseData | |
put apiUrl nonce data = | |
apiUrl | |
|> HttpBuilder.put | |
|> HttpBuilder.withHeader "X-WP-NONCE" nonce | |
|> HttpBuilder.withBody (Http.jsonBody <| dataEncoder nonce data) | |
|> HttpBuilder.withExpect (Http.expectJson dataDecoder) | |
|> HttpBuilder.toRequest | |
-- JSON decoders | |
dataDecoder : Decoder CaseData | |
dataDecoder = | |
decode CaseData | |
|> required "id" Decode.int | |
|> required "title" Decode.string | |
|> required "wpAuthor" wpAuthorDecoder | |
|> required "contactPerson" contactDecoder | |
|> required "agencies" (Decode.list agencyDecoder) | |
|> required "author" Decode.string | |
|> required "client" Decode.string | |
wpAuthorDecoder : Decoder WpAuthor | |
wpAuthorDecoder = | |
decode WpAuthor | |
|> required "id" Decode.int | |
|> required "name" Decode.string | |
contactDecoder : Decoder ContactPerson | |
contactDecoder = | |
decode ContactPerson | |
|> required "name" Decode.string | |
|> required "phone" Decode.string | |
|> required "email" Decode.string | |
agencyDecoder : Decoder Agency | |
agencyDecoder = | |
decode Agency | |
|> required "name" Decode.string | |
-- JSON encoders | |
dataEncoder : String -> CaseData -> Value | |
dataEncoder nonce data = | |
Encode.object | |
[ ( "id", Encode.int data.id ) | |
, ( "nonce", Encode.string nonce ) | |
, ( "title", Encode.string data.title ) | |
, ( "author", Encode.string data.author ) | |
, ( "client", Encode.string data.client ) | |
, ( "wpAuthor", wpAuthorEncoder data.wpAuthor ) | |
, ( "contactPerson", contactEncoder data.contactPerson ) | |
, ( "agencies", Encode.list (List.map agenciesEncoder data.agencies) ) | |
] | |
wpAuthorEncoder : WpAuthor -> Value | |
wpAuthorEncoder { id, name } = | |
Encode.object | |
[ ( "id", Encode.int id ) | |
, ( "name", Encode.string name ) | |
] | |
contactEncoder : ContactPerson -> Value | |
contactEncoder { name, phone, email } = | |
Encode.object | |
[ ( "name", Encode.string name ) | |
, ( "phone", Encode.string phone ) | |
, ( "email", Encode.string email ) | |
] | |
agenciesEncoder : { name : String } -> Value | |
agenciesEncoder { name } = | |
Encode.object | |
[ ( "name", Encode.string name ) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment