request

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 6 Imported by: 1

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Request added in v0.0.6

type Request struct {
	Method  string
	URL     string
	Headers map[string]string
	Body    io.Reader
}

Request HTTP request

func NewRequest added in v0.1.0

func NewRequest(method, url string, headers map[string]string, body io.Reader) (req *Request)

NewRequest initalises new http request

func (*Request) Do added in v0.1.0

func (request *Request) Do(ctx context.Context, client *http.Client) (*Response, error)

Do initiates http request

type Response

type Response struct {
	StatusCode int
	URL        *url.URL
	Body       []byte
}

Response HTTP Response

func Delete added in v0.0.6

func Delete(ctx context.Context, url string, headers map[string]string, data io.Reader) (*Response, error)

Delete sends DELETE request

func Get added in v0.0.6

func Get(ctx context.Context, url string, headers map[string]string) (*Response, error)

Get sends GET request

func Post added in v0.0.6

func Post(ctx context.Context, url string, headers map[string]string, data io.Reader) (*Response, error)

Post sends POST request

func Put added in v0.0.6

func Put(ctx context.Context, url string, headers map[string]string, data io.Reader) (*Response, error)

Put sends PUT request

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL