nap

package module
v0.0.0-...-9c42b62 Latest Latest
Warning

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

Go to latest
Published: May 1, 2020 License: MIT Imports: 9 Imported by: 1

README

Nap Library

This workshop is a CLI project that encapsulates resources and actions for interacting with REST API's. Basically, this library allows us to build and call REST API's by shaping and acting like a bunch of cohesive layers that present an abstraction of GoLang's standard HTTP package, without worrying about the mechanics of the Req/Res cycle.

It also features on calling REST API endpoints, authentication (basic and by tokens) and response callbacks

Components

The NAP library acts as a client, this is the interface for the resources. The REST resources are stored in the API structure and the API endpoints are the functionality involved.

HTTP Requests

Golang have a standard library for handling HTTP requests (send and receive). The main goal of the workshop is preparing a layer for REST API's components, resolving HTTP Headers for authentication and encryption, and for Marshal and Unmarshal processes (data serialization and data structure population).

Dependencies

Check the glide.yaml file for it.

Usage

I assume you already have Go installed, with the GOPATH set.

Clone this repository inside your src directory and install it using the following command

go install ./cmd/...

Then, inside cmd/nap directory just, nap []

For help: nap -help

Available commands
  1. nap -help: this command will display the help structure for available commands
  2. nap -list: this shows all the commands available to use
  3. nap get [url]: (new) the get command can also receive an optional POST payload in the request (body structure)

Resource

The baseURL used for this example is the https://httpbin.org website.

Running tests

For doing this, check the go test -v command.

This will check and evaluate all the test files for the project, referenced by _test.go

Credits

License

Documentation

Overview

working code for APIs

auth logic

callback router

client information

resources

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API struct {
	BaseURL       string
	Resources     map[string]*RestResource // names for the resources (queries)
	DefaultRouter *CBRouter                // pointer to the CBRouter (dynamic, if a resource have it)
	Client        *Client                  // Client library (wrapper HTTP client)
}

func NewAPI

func NewAPI(baseURL string) *API

implementing a new API

func (*API) AddResource

func (api *API) AddResource(name string, res *RestResource)

adding resources

func (*API) Call

func (api *API) Call(name string, params map[string]string, payload interface{}) error

adding API calls - payload JSON

func (*API) ResourceNames

func (api *API) ResourceNames() []string

func (*API) SetAuth

func (api *API) SetAuth(auth Authentication)

setting up to the client (if needed)

type AuthBasic

type AuthBasic struct {
	Username string
	Password string
}

func NewAuthBasic

func NewAuthBasic(username, password string) *AuthBasic

func (*AuthBasic) AuthorizationHeader

func (a *AuthBasic) AuthorizationHeader() string

type AuthToken

type AuthToken struct {
	Token string // random string
}

func NewAuthToken

func NewAuthToken(token string) *AuthToken

func (*AuthToken) AuthorizationHeader

func (a *AuthToken) AuthorizationHeader() string

type Authentication

type Authentication interface {
	AuthorizationHeader() string // basic <base64-encoded- string>
}

type CBRouter

type CBRouter struct {
	Routers       map[int]RouterFunc // int = statusCode <> function execution
	DefaultRouter RouterFunc
}

func NewRouter

func NewRouter() *CBRouter

NewRouter returns a new router

func (*CBRouter) CallFunc

func (r *CBRouter) CallFunc(resp *http.Response) error

func (*CBRouter) RegisterFunc

func (r *CBRouter) RegisterFunc(status int, fn RouterFunc)

type Client

type Client struct {
	Client   *http.Client
	AuthInfo Authentication // interface
}

func NewClient

func NewClient() *Client

func (*Client) ProcessRequest

func (c *Client) ProcessRequest(baseURL string, res *RestResource, params map[string]string, payload interface{}) error

processing request

func (*Client) SetAuth

func (c *Client) SetAuth(auth Authentication)

type RestResource

type RestResource struct {
	Endpoint string    // URI
	Method   string    // HTTP verb
	Router   *CBRouter // for dynamic resources (re-usage instead of recreation)
}

func NewResource

func NewResource(endpoint, method string, router *CBRouter) *RestResource

func (*RestResource) RenderEndpoint

func (r *RestResource) RenderEndpoint(params map[string]string) string

returning the endpoint string

type RouterFunc

type RouterFunc func(resp *http.Response) error // return a function (toggle if necessary)

Directories

Path Synopsis
cmd
nap

Jump to

Keyboard shortcuts

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