client

package
v2.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2022 License: MIT Imports: 10 Imported by: 1

README

shared | client

A simple http client that make client calls quicker and lighter while still allowing for customization. Intended on being used with the shared/config package. The package only allows for the following request methods: GET, POST, PUT, DELETE with or without contexts.

How To Use

Here is an example of what a config JSON file could look like.

{
    "clients": {
        "myApp1": {
            "headers": {
                "content-type": [
                    "application/json"
                ]
            },
            "health": "/ping",
            "timeout": 10,
            "url": "https://www.test.com"
        },
        "myApp2": {
            "headers": {
                "content-type": [
                    "application/json"
                ]
            },
            "health": "/v2/health",
            "timeout": 10,
            "url": "https://www.fake.com"
        }
    }
}

Below is an example on how to we use the configs from above to create two clients.

package main

import (
    "fmt"

    "github.com/jobaldw/shared/v2/client"
    "github.com/jobaldw/shared/v2/config"
)

func main() {
    newStruct := config.Clients{}

    if err := config.Unmarshal(&newStruct); err != nil {
        // handle error
    }

    // because the built-in clients configs have a map of string to clients, we can loop through
    // each client within the config. You may also use the built-in client config allowing for only
    // one client.
    clients := make(map[string]*client.Client)
    for k, v := range newStruct.Clients {
        newClient, err := client.New(v)
        if err != nil {
            // handle error
        }
        clients[k] = newClient
        
        // you now have the ability to make http requests with the given client.
        fmt.Println(newClient)
    }
}

OUTPUT

$ go run main.go 
&{/ping 0xc00007f1d0 map[content-type:[application/json]] https://www.test.com}
&{/v2/health 0xc00007f290 map[content-type:[application/json]] https://www.fake.com}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {

	// Key-value pairs in an HTTP header.
	Headers http.Header

	// A parsed URL (technically a URI)
	URL *url.URL
	// contains filtered or unexported fields
}

A simple client that will also handle http requests. Uses the net/http and net/url packages.

func New

func New(conf config.Client) (*Client, error)

New

Creates a new client from the shared config.Client() struct.
* @param conf: client configurations

func (*Client) Delete

func (c *Client) Delete(path string, params map[string][]string) (*Response, error)

Delete

Makes a DELETE request to the client with a background context.Context().
* @param path: client path that request will send to
* @param parameters: query parameters

func (*Client) DeleteWithContext

func (c *Client) DeleteWithContext(ctx context.Context, path string, params map[string][]string) (*Response, error)

DeleteWithContext

Makes a DELETE request to the client with any passed in context.Context().
* @param ctx: context used to handle any cancellations
* @param path: client path that request will send to
* @param parameters: query parameters

func (*Client) Get

func (c *Client) Get(path string, params map[string][]string) (*Response, error)

Get

Makes a GET request to the client with a background context.Context().
* @param path: client path that request will send to
* @param parameters: query parameters

func (*Client) GetWithContext

func (c *Client) GetWithContext(ctx context.Context, path string, params map[string][]string) (*Response, error)

GetWithContext

Makes a GET request to the client with any passed in context.Context().
* @param ctx: context used to handle any cancellations
* @param path: client path that request will send to
* @param parameters: query parameters

func (*Client) IsReady

func (c *Client) IsReady(ctx context.Context) (bool, error)

IsReady

Uses the clients health endpoint to determine if its up and running.
* @param path: client path that request will send to

func (*Client) Post

func (c *Client) Post(path string, params map[string][]string, body interface{}) (*Response, error)

Post

Makes a POST request to the client with any passed in context.Context().
* @param path: client path that request will send to
* @param parameters: query parameters
* @param body: request body

func (*Client) PostWithContext

func (c *Client) PostWithContext(ctx context.Context, path string, params map[string][]string, body interface{}) (*Response, error)

PostWithContext

Makes a POST request to the client with any passed in context.Context().
* @param ctx: context used to handle any cancellations
* @param path: client path that request will send to
* @param parameters: query parameters
* @param body: request body

func (*Client) Put

func (c *Client) Put(path string, params map[string][]string, body interface{}) (*Response, error)

Put

Makes a PUT request to the client with any passed in context.Context().
* @param path: client path that request will send to
* @param parameters: query parameters
* @param body: request body

func (*Client) PutWithContext

func (c *Client) PutWithContext(ctx context.Context, path string, params map[string][]string, body interface{}) (*Response, error)

PutWithContext

Makes a PUT request to the client with any passed in context.Context().
* @param ctx: context used to handle any cancellations
* @param path: client path that request will send to
* @param parameters: query parameters
* @param body: request body

type Response

type Response struct {
	// http status test. Example "200 OK"
	Status string

	// http status code. Example 200
	StatusCode int

	// the request that was received by a server or to be sent by a client
	Request *http.Request
	// contains filtered or unexported fields
}

The response from an HTTP request.

func (*Response) GetBody

func (r *Response) GetBody() io.Reader

GetBody

Returns the response body in its i/o reader stream.

func (*Response) GetBodyBytes

func (r *Response) GetBodyBytes() []byte

GetBodyBytes

Reads the response body i/o and converts it to a byte array.

func (*Response) GetBodyString

func (r *Response) GetBodyString() string

GetBodyString

Parses the response body i/o interface into a string.

func (*Response) IsSuccessful

func (r *Response) IsSuccessful() bool

IsSuccessful

Checks if the response status code is 200 level.

Jump to

Keyboard shortcuts

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