simpleresty

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2020 License: MIT Imports: 6 Imported by: 18

README

Actions Status Go Report Card License

A simple wrapper around go-resty.

Background

Having used go-resty to create clients for various service APIs, I noticed a common set of methods I would define in each API client. I extracted those methods/function and moved them into this separate library. This way, all my clients could benefit from using a single library to interface with the APIs.

I have embedded resty.Client into simpleresty.Client so all of resty's functions/methods are available to the user. In fact, simpleresty.New() returns a resty.Client.

Example

type GithubAPIResponse struct {
    CurrentUserURL string `json:"current_user_url,omitempty"`
}

c := simpleresty.New()
c.SetBaseURL("https://api.github.com/")

var result *GithubAPIResponse
response, getErr := c.Get("", result, nil)

fmt.Println(getErr)

fmt.Println(response.StatusCode)
fmt.Println(result.CurrentUserURL)

Documentation

Index

Constants

View Source
const (
	GetMethod    = "GET"
	PostMethod   = "POST"
	PutMethod    = "PUT"
	DeleteMethod = "DELETE"
	PatchMethod  = "PATCH"
)

Variables

This section is empty.

Functions

func AddQueryParams

func AddQueryParams(baseURL string, opts ...interface{}) (string, error)

AddQueryParams takes a slice of opts and adds each field as escaped URL query parameters to a base URL string.

Each element in opts must be a struct whose fields contain "url" tags.

Based on: https://github.com/google/go-github/blob/master/github/github.go#L226

Types

type Client

type Client struct {
	*resty.Client
	// contains filtered or unexported fields
}

Client represents a SimpleResty client. It embeds the resty.client so users have access to its methods.

func New

func New() *Client

New function creates a new SimpleResty client.

func (*Client) ConstructRequest

func (c *Client) ConstructRequest(r, body interface{}) *resty.Request

ConstructRequest creates a new request.

func (*Client) Delete

func (c *Client) Delete(url string, r, body interface{}) (*Response, error)

Delete executes a HTTP DELETE request.

func (*Client) Dispatch

func (c *Client) Dispatch(request *resty.Request) (*Response, error)

Dispatch method is a wrapper around the send method which performs the HTTP request using the method and URL already defined.

func (*Client) Get

func (c *Client) Get(url string, r, body interface{}) (*Response, error)

Get executes a HTTP GET request.

func (*Client) Patch

func (c *Client) Patch(url string, r, body interface{}) (*Response, error)

Patch executes a HTTP PATCH request.

func (*Client) Post

func (c *Client) Post(url string, r, body interface{}) (*Response, error)

Post executes a HTTP POST request.

func (*Client) Put

func (c *Client) Put(url string, r, body interface{}) (*Response, error)

Put executes a HTTP PUT request.

func (*Client) RequestURL

func (c *Client) RequestURL(template string, args ...interface{}) string

RequestURL appends the template argument to the base URL and returns the full request URL.

func (*Client) RequestURLWithQueryParams

func (c *Client) RequestURLWithQueryParams(url string, opts ...interface{}) (string, error)

RequestURLWithQueryParams first constructs the request URL and then appends any URL encoded query parameters.

This function operates nearly the same as RequestURL

func (*Client) SetBaseURL

func (c *Client) SetBaseURL(url string)

SetBaseURL sets the base url for the client.

type Response

type Response struct {
	RequestURL    string
	RequestMethod string
	Status        string
	StatusCode    int
	Body          string
	RawHTTP       *http.Response
}

Response represents the response after executing a HTTP request.

Jump to

Keyboard shortcuts

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