Giter Site home page Giter Site logo

httpclient's Introduction

httpclient

  • simple library to facilitate calls to http endpoints

two methods to call

  • function with parameters
  • method with structure

two options for results

  • raw response (*http.Response)
  • response body decoded to json

Examples

see examples directory

Full Response

equivalent of

curl 'https://api.example.com' -H 'Authorization: Bearer API_TOKEN'

function

func main() {
    token := os.Getenv("API_TOKEN")
    response, err := httpclient.GetResponse("", http.MethodGet, "https://api.example.com", token)
    if err !=nil {
        log.Fatal(err)
    }
    defer response.Body.Close()
    bytes, err := io.ReadAll(response.Body)
    log.Println(response.StatusCode, string(bytes))
}

method

func main() {
    token := os.Getenv("API_TOKEN")
    endpoint := httpclient.Endpoint {
        URL: "https://api.clustercat.com",
	    Method: http.MethodGet    
	    Authorization: token,
    }
    response, err := endpoint.GetResponse()
    defer response.Body.Close()
    bytes, err := io.ReadAll(response.Body)
    log.Println(response.StatusCode, string(bytes))
}

JSON response

equivalent of

curl 'https://api.example.com/login' -H 'Authorization: Bearer API_TOKEN' - H 'Content-Type: application/json' -d '{"name":"admin","pass":"password"}'

function

func main() {
    token := os.Getenv("API_TOKEN")
    data := struct {
        Name: "admin",
        Pass: "password",
    }
    response := struct {
        Language_Pref: "EN",
        JWT: "some string",
    }
    response, err := httpclient.GetJSON(data, http.MethodPOST, "https://api.example.com/api/login", "Bearer " + token)
    if err !=nil {
        log.Fatal(err)
    }
    log.Println(response)
}

method

func main() {
    token := os.Getenv("API_TOKEN")
    data := struct {
        Name: "admin",
        Pass: "password",
    }
    response := struct {
        Language_Pref: "EN",
        JWT: "some string",
    }
    endpoint := httpclient.Endpoint {
        URL: "https://api.example.com",
	    Method: http.MethodPOST
	    Authorization: token,
        Route: "/api/login",
        Data: data,
        Response: response,
    }
    jsonEndpoint := httpclient.JSONEndpoint[struct {Language_Pref string JWT string}] {
        endpoint,
        response,
    }
    answer, err := httpclient.JSON(response)
    if err !=nil {
        log.Fatal(err)
    }
    log.Println(response)
    ```

httpclient's People

Contributors

mattkasun avatar dependabot[bot] avatar

Stargazers

Tobias Cudnik avatar  avatar

Watchers

 avatar

Forkers

syonfox cryply

httpclient's Issues

refactor JSONEndpoint struct

  • don't use composite struct (makes for messy calling code)
  • don't use comparable constraint (won't work for struct containing slices)

url query

support application/x-www-form-urlencoded in addition to application/json

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.