apik

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 12 Imported by: 0

README

APIK

Go Go Report Card codecov

This package is a wrapper over the standard net/http package and is intended to simplify working with http requests.

Examples


package main

import (
	"context"
	"fmt"

	"github.com/niklak/apik"
	"github.com/niklak/apik/reqopt"
)

type httpBinResponse struct {
	URL     string              `json:"url"`
	Form    map[string][]string `json:"form"`
	Headers map[string]string   `json:"headers"`
}

func main() {

	// Creating a client with a base URL, that will be common for all requests
	client := apik.New(apik.WithBaseUrl("https://httpbin.org"))

	// Creating a POST request
	req := apik.NewRequest(
		context.Background(),
		"/post",
		reqopt.Method("POST"),
		reqopt.AddFormField("k", "v1"),
		reqopt.AddFormField("k", "v2"),
		reqopt.Header("User-Agent", "apik/0.1"),
	)

	result := new(httpBinResponse)
	resp, err := client.JSON(req, result)

	if err != nil {
		panic(err)
	}

	fmt.Printf("status code: %d\n", resp.Raw.StatusCode)

	fmt.Printf("response: %#v\n", result)
}



Documentation

Index

Constants

This section is empty.

Variables

View Source
var NewRequest = request.NewRequest

NewRequest is an alias for request.NewRequest

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...ClientOption) *Client

New creates a new Client with the given options

func (*Client) Do

func (c *Client) Do(req *Request) (resp *http.Response, err error)

Do sends an http.Request built from Request and returns an http.Response

func (*Client) Fetch

func (c *Client) Fetch(req *request.Request, result any) (resp *Response, err error)

Fetch sends an http.Request built from Request and returns a Response, containing the http.Response and the result of the request. The result can be a *string, a *[]byte or an io.Writer. If the result is nil, then result will be set as a *bytes.Buffer.

func (*Client) JSON

func (c *Client) JSON(req *request.Request, result any) (resp *Response, err error)

JSON sends an http.Request built from Request and returns a Response, containing the http.Response and the result of the request. The result must be a pointer to entity that can be decoded from json body.

type ClientOption

type ClientOption func(*Client)

ClientOption is a function that modifies a Client

func WithBaseUrl

func WithBaseUrl(baseURL string) ClientOption

WithBaseUrl sets the base url for the http.Client

func WithCookieJar

func WithCookieJar(jar http.CookieJar) ClientOption

WithCookieJar sets the cookie jar for the http.Client Also CookieJar can be set with `WithHttpClient` option.

func WithCookies

func WithCookies(cookies []*http.Cookie) ClientOption

WithCookies sets the cookies for the http.Client

func WithHeader

func WithHeader(key, value string) ClientOption

WithHeader adds a key-value pair in the http.Header for the http.Client

func WithHeaders

func WithHeaders(header http.Header) ClientOption

WithHeaders sets an http.Header for the http.Client

func WithHttpClient

func WithHttpClient(hc *http.Client) ClientOption

WithHttpClient sets the http.Client to use

func WithTimeout

func WithTimeout(t time.Duration) ClientOption

WithTimeout sets the timeout for the http.Client

func WithTrace

func WithTrace() ClientOption

WithTrace enables tracing for the http.Request

type Request

type Request = request.Request

Request is an alias for request.Request

type Response

type Response struct {
	Raw     *http.Response
	Result  any
	Request *Request
}

Response represents a wrapper around http.Response with the result of the request

Directories

Path Synopsis
examples
internal
jar

Jump to

Keyboard shortcuts

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