request/README.md

3.2 KiB

Request

simple golang package for making API requests

Documentation

Documentation for the project can be found on pkg.go.dev

Installation

With a correctly configured golang toolchain:

go get -u git.0cd.xyz/michael/request

Examples

Requesting and encoding structured data:

type Topics struct {
    TopicList struct {
        Topics []struct {
            ID         int    `json:"id"`
            Title      string `json:"title"`
            PostsCount int    `json:"posts_count"`
            CreatedAt  string `json:"created_at"`
            Slug       string `json:"slug"`
        } `json:"topics"`
    } `json:"topic_list"`
}

func topics(ctx context.Context) (topics *Topics, err error) {
    headers := map[string]string{
        "Accept":       "application/json",
        "Content-Type": "application/json",
    }
    resp, err := request.NewRequest(http.MethodGet, "https://forum.0cd.xyz/t/nixos-vfio-pcie-passthrough/277", headers, nil).Do(ctx, &http.Client{})
    if err != nil {
        return nil, err
    }
    err = json.Unmarshal(resp.Body, &topics)
    if err != nil {
        return nil, err
    }
    return topics, nil
}

Requesting and encoding unstructured data:

users := make(map[string]interface{})
headers := map[string]string{
    "Accept":       "application/json",
    "Content-Type": "application/json",
}

resp, err := request.Get(ctx, "https://reqres.in/api/users", headers, nil)
if err != nil {
    log.Println(err)
    return
}

err = json.Unmarshal(resp.Body, &users)
if err != nil {
    log.Println(err)
    return
}

fmt.Println(users["data"])

Sending requests with data using post, put, etc:

headers := map[string]string{
    "Accept":       "application/json",
    "Content-Type": "application/json",
}

req := `{
    "on": true,
    "bri": 255
}`

_, err := request.NewRequest(http.MethodPut, "http://10.0.40.2/api/<apikey>/lights/4/state", headers, bytes.NewBuffer([]byte(req))).Do(ctx, &http.Client{})
if err != nil {
    log.Println(err)
}

License

MIT License Copyright (c) 2020 Michael Lindman

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 (including the next
paragraph) 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.