client

package module
v0.0.0-...-5ed193e Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2022 License: MIT Imports: 10 Imported by: 0

README

go-http-client

Build codecov Scrutinizer Code Quality Go Report Card GitHub contributors GitHub license PkgGoDev

This package provides a generic go client template for an HTTP API

Installation

go-http-client is compatible with modern Go releases in module mode, with Go installed:

go get github.com/NdoleStudio/go-http-client

Alternatively the same can be achieved if you use import in a package:

import "github.com/NdoleStudio/go-http-client"

Implemented

Usage

Initializing the Client

An instance of the client can be created using New().

package main

import (
	"github.com/NdoleStudio/go-http-client"
)

func main()  {
	statusClient := client.New(client.WithDelay(200))
}
Error handling

All API calls return an error as the last return object. All successful calls will return a nil error.

status, response, err := statusClient.Status.Ok(context.Background())
if err != nil {
    //handle error
}
Status Codes
GET /200: OK
status, response, err := statusClient.Status.Ok(context.Background())

if err != nil {
    log.Fatal(err)
}

log.Println(status.Description) // OK

Testing

You can run the unit tests for this client from the root directory using the command below:

go test -v

License

This project is licensed under the MIT License - see the LICENSE file for details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Status *statusService
	// contains filtered or unexported fields
}

Client is the campay API client. Do not instantiate this client with Client{}. Use the New method instead.

func New

func New(options ...Option) *Client

New creates and returns a new campay.Client from a slice of campay.ClientOption.

type HTTPStatus

type HTTPStatus struct {
	Code        int    `json:"code"`
	Description string `json:"description"`
}

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is options for constructing a client

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL set's the base url for the flutterwave API

func WithDelay

func WithDelay(delay int) Option

WithDelay sets the delay in milliseconds before a response is gotten. The delay must be > 0 for it to be used.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient sets the underlying HTTP client used for API requests. By default, http.DefaultClient is used.

type Response

type Response struct {
	HTTPResponse *http.Response
	Body         *[]byte
}

Response captures the http response

func (*Response) Error

func (r *Response) Error() error

Error ensures that the response can be decoded into a string inc ase it's an error response

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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