rip

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: MIT Imports: 10 Imported by: 3

README

rip

rip is a minimal api client framework i just want use to REST in peace.

Installation

go get -u github.com/iwpnd/rip

Usage

package blogclient

import (
  "fmt"
  "encoding/json"

  "github.com/iwpnd/rip"
  )

type BlogPost struct {
    Id string
    Content string
  }

type BlogApiClient struct {
    *rip.Client
}

func NewBlogApiClient(host string, options rip.ClientOptions) (*BlogApiClient, error) {
    c, err := rip.NewClient(host, options)
    if err != nil {
        return &BlogApiClient{}, err
    }
    return &BlogApiClient{c}, nil
}

func (c *BlogApiClient) GetById(id string) (*BlogPost, error) {
    req, err := c.NR()

    req.SetHeader("Accept", "application/json")
    req.SetParams(Params{"id": id})

    res, err := req.Execute("GET", "/blog/:id")
    if err != nil {
        return &BlogPost{}, err
    }

    b := &BlogPost{}
	  err = rip.Unmarshal(res.Header().Get("Content-Type"), res.Body(), r)
	  if err != nil {
		    return &BlogPost, err
	  }

    return b
}

func (c *BlogApiClient) Create(post BlogPost) (*BlogPost, error) {
    b, err := json.Marshal(post)
    if err != nil {
        return &BlogPost, err
    }

    req, err := c.NR()

    req.SetHeaders(Headers{
      "Content-Type": "application/json",
      "Accept": "application/json",
    })
    req.SetBody(b)

    res, err := req.Execute("POST", "/blog")
    if err != nil {
        return &Response{}, err
    }

    b := &BlogPost{}
	  err = rip.Unmarshal(res.Header().Get("Content-Type"), res.Body(), r)
	  if err != nil {
		    return &BlogPost, err
	  }

    return b
}

License

MIT

Maintainer

Benjamin Ramser - @iwpnd

Project Link: https://github.com/iwpnd/rip

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClientMissing = errors.New("use .NR() to create a new request instead")

ErrClientMissing occurs when Request is instantiated without Client.NR()

Functions

func IsJSON

func IsJSON(ct string) bool

IsJSON helper to determine content type

func Unmarshal

func Unmarshal(ct string, b []byte, d interface{}) error

Unmarshal helper

Types

type Client

type Client struct {
	Options ClientOptions
	// contains filtered or unexported fields
}

Client ...

func NewClient

func NewClient(host string, options ClientOptions) (*Client, error)

NewClient creates a new Client

func (*Client) NR

func (c *Client) NR() *Request

NR creates a new request

type ClientOptions

type ClientOptions struct {
	Header  Header
	Timeout int
}

ClientOptions ...

type Header = map[string]string

Header ...

type Params

type Params = map[string]interface{}

Params ...

type Query

type Query = map[string]interface{}

Query ...

type Request

type Request struct {
	Body   interface{}
	Header http.Header
	Params Params
	Path   string
	Query  url.Values
	Result interface{} // NOTE: can I pass struct here to unmarshal resp body to?
	URL    string

	RawRequest *http.Request
	// contains filtered or unexported fields
}

Request ...

func (*Request) Execute

func (r *Request) Execute(method, path string) (*Response, error)

Execute executes a given request using a method on a given path

func (*Request) SetBody

func (r *Request) SetBody(body interface{}) *Request

SetBody to set a request body

func (*Request) SetHeader

func (r *Request) SetHeader(key, value string) *Request

SetHeader to set a single header

func (*Request) SetHeaders

func (r *Request) SetHeaders(header Header) *Request

SetHeaders to set multiple header as map[string]string

func (*Request) SetParams

func (r *Request) SetParams(params Params) *Request

SetParams to replace in path

func (*Request) SetQuery

func (r *Request) SetQuery(query Query) *Request

SetQuery to set query parameters

type Response

type Response struct {
	Request     *Request
	RawResponse *http.Response
	// contains filtered or unexported fields
}

Response ...

func (*Response) Body

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

Body returns Body as byte array

func (*Response) Header

func (r *Response) Header() http.Header

Header method returns the response headers

func (*Response) IsError

func (r *Response) IsError() bool

IsError returns true if StatusCode > 399

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess returns true if 199 < StatusCode < 300

func (*Response) RawBody

func (r *Response) RawBody() io.ReadCloser

RawBody returns raw response body. be sure to close

func (*Response) Status

func (r *Response) Status() string

Status returns the response status

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode returns the response status code

func (*Response) String

func (r *Response) String() string

String method returns the body of the server response as String.

Jump to

Keyboard shortcuts

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