Giter Site home page Giter Site logo

cobalamin / elm-http-builder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lukewestby/elm-http-builder

0.0 2.0 0.0 95 KB

elm-http-builder

Home Page: http://package.elm-lang.org/packages/lukewestby/elm-http-builder/latest

License: MIT License

Shell 0.06% Elm 8.75% JavaScript 91.19%

elm-http-builder's Introduction

elm-http-builder

ICARE Build Status

Chainable functions for building HTTP requests and composable functions for handling responses.

Need help? Join the #http channel in the Elm Slack!

Thanks to @fredcy, @rileylark, and @etaque for the original discussion of the API

Example

In this example, we expect a successful response to be JSON array of strings, like:

["hello", "world", "this", "is", "the", "best", "json", "ever"]

and an error response might have a body which just includes text, such as the following for a 404 error:

Not Found.

We'll use HttpBuilder.jsonReader and a Json.Decode.Decoder to parse the successful response body and HttpBuilder.stringReader to accept a string body on error without trying to parse JSON.

import Task exposing (Task)
import Time
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) ]


addItem : String -> Task (HttpBuilder.Error String) (HttpBuilder.Response (List String))
addItem item =
  HttpBuilder.post "http://example.com/api/items"
    |> withJsonBody (itemEncoder item)
    |> withHeader "Content-Type" "application/json"
    |> withTimeout (10 * Time.second)
    |> withCredentials
    |> send (jsonReader itemsDecoder) stringReader

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_dot_js on Twitter or @luke in the elmlang Slack!

elm-http-builder's People

Contributors

ccapndave avatar cobalamin avatar decentral1se avatar ento avatar ir4y avatar istar-eldritch avatar rudle avatar shamrin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.