Help us shape the future of Go! Take the 2026 Go Developer Survey

rest

package module
v0.0.0-...-b85c2e6 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2018 License: BSD-3-Clause Imports: 12 Imported by: 4

README

go-rest-api

A fairly generic HTTP API.

Supports both HTTP and TLS (https). Encoding schemes supported: json/xml.

Importing

import(
    "github.com/sky-uk/go-rest-api"
    "net/http"
)

Usage

Get a REST Client object
    client := rest.Client{
        URL: url,       // mandatory
        User: user, 
        Password: password, 
        IgnoreSSL: ignoreSSL, 
        Debug: debug, 
        Headers: headers,
        Timeout: 30     // in seconds
    } 

    // for a simple http client...
    client := rest.Client{URL: url}

Perform a request
    // Prepare a request...
    api := rest.NewBaseAPI(
        http.MethodGet,         // request method
        "/",                    // request path
        nil,                    // request payload object
        new(string),            // (pointer to) response payload object
        nil,                    // (pointer to) error object
    )

    // Perform the request...
    err := client.Do(api)
    if err != nil {
        // handle errors....
    }
Getting the response object

    // example (json payload)
    type JSONFoo struct {
	    Fields map[string]string `json:"fields"`
    }

    // Prepare a request...
    api := rest.NewRestAPI(
        http.MethodGet,         // request method
        "/",                    // request path
        nil,                    // request payload object
        new(JSONFoo),           // (pointer to) response payload object
        nil,                    // (pointer to) error object
    )

    //...Perform request...
    // Get response object...
    respObj := *api.ResponseObject().(*JSONFoo)

More examples for the supported encodings in the client_test.go module.

Getting response status code
    status := api.StatusCode()
Getting the raw response as a byte stream
    raw := api.RawResponse()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseAPI

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

BaseAPI - Base API struct.

func NewBaseAPI

func NewBaseAPI(
	method string,
	endpoint string,
	requestObject interface{},
	responseObject interface{},
	errorObject interface{},
) *BaseAPI

NewBaseAPI - Returns a new object of the BaseAPI.

func (*BaseAPI) Endpoint

func (b *BaseAPI) Endpoint() string

Endpoint - Returns the Endpoint url string.

func (*BaseAPI) Error

func (b *BaseAPI) Error() error

Error - Returns the err the api.

func (*BaseAPI) ErrorObject

func (b *BaseAPI) ErrorObject() interface{}

ErrorObject - Returns the ErrorObject interface.

func (*BaseAPI) Method

func (b *BaseAPI) Method() string

Method - Returns the Method string, i.e. GET, PUT, POST.

func (*BaseAPI) RawResponse

func (b *BaseAPI) RawResponse() []byte

RawResponse - Returns the rawResponse object as byte type.

func (*BaseAPI) RequestObject

func (b *BaseAPI) RequestObject() interface{}

RequestObject - Returns the request object of the BaseAPI

func (*BaseAPI) ResponseObject

func (b *BaseAPI) ResponseObject() interface{}

ResponseObject - Returns the ResponseObject interface.

func (*BaseAPI) SetError

func (b *BaseAPI) SetError(err error)

SetError - Sets the err on api object.

func (*BaseAPI) SetErrorObject

func (b *BaseAPI) SetErrorObject(res interface{})

SetErrorObject - Sets the errorObject

func (*BaseAPI) SetRawResponse

func (b *BaseAPI) SetRawResponse(rawResponse []byte)

SetRawResponse - Sets the rawResponse on api object.

func (*BaseAPI) SetResponseObject

func (b *BaseAPI) SetResponseObject(res interface{})

SetResponseObject - Sets the responseObject

func (*BaseAPI) SetStatusCode

func (b *BaseAPI) SetStatusCode(statusCode int)

SetStatusCode - Sets the statusCode from api object.

func (*BaseAPI) StatusCode

func (b *BaseAPI) StatusCode() int

StatusCode - Returns the status code of the api.

type Client

type Client struct {
	URL        string
	User       string
	Password   string
	IgnoreSSL  bool
	Debug      bool
	Headers    map[string]string
	Timeout    time.Duration // in seconds
	StatusCode int
}

Client struct.

func (*Client) Do

func (restClient *Client) Do(api *BaseAPI) error

Do - makes the API call.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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