simpleresty

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 12 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/functions I would define in each API client. I extracted those methods/functions 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

package main

import (
	"fmt"
	"github.com/davidji99/simpleresty"
)

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

func main() {
	c := simpleresty.New()

	var result *GithubAPIResponse
	response, getErr := c.Get("https://api.github.com", &result, nil)
	if getErr != nil {
		panic(getErr)
	}

	fmt.Println(response.StatusCode) // Returns 200
	fmt.Println(result.CurrentUserURL) // Returns 'https://api.github.com/user'
}

Additional examples can be found in the /examples folder.

You can also check out rollrest-go, which uses this library to implement an API rest client for Rollbar.

Proxy

simpleresty respects any proxy URLs set in your environment in this order of preference:

  1. HTTPS_PROXY
  2. https_proxy
  3. HTTP_PROXY
  4. http_proxy

Only a single value from one of the above four environment variables will be used to set the proxy URL on the Client.

simpleresty will also respect domains (not IP addresses or CIDR ranges) defined by the NO_PROXY or no_proxy environment variable. Multiple domains must be separated by a comma.

Go-Resty

As this pkg is a thin wrapper around go-resty, all of its methods are available to use in this package. Please refer to go-resty's documentation for more information.

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

func OAuth added in v0.2.2

func OAuth(clientID, clientSecret string, endpoint oauth2.Endpoint) (*oauth2.Token, error)

func OAuthClientCredentials added in v0.2.2

func OAuthClientCredentials(clientID, clientSecret, tokenURL string) (*oauth2.Token, error)

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 with base url set to empty string.

Users can set the base string later in their code.

func NewWithBaseURL added in v0.2.3

func NewWithBaseURL(url string) *Client

NewWithBaseURL creates a new simpleresty client with base url set.

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 {
	// Request representss the response's original request.
	Request *resty.Request

	// RequestURL is the request URL.
	RequestURL string

	// RequestMethod is the request method such as GET.
	RequestMethod string

	// Request body is the request body in JSON string format.
	RequestBody string

	// Resp represents the entire HTTP response.
	Resp *resty.Response

	// Status is the response status in string format such as '200 OK'.
	Status string

	// StatusCode is response status in integer format such as 200.
	StatusCode int

	// Body is the response body in JSON String format.
	Body string
}

Response represents the response after executing a HTTP request.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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