go_networking

package module
v0.0.0-...-43bc6bd Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2017 License: MIT Imports: 15 Imported by: 0

README

Latest Tag Twitter Go Report Card

go-networking

go-networking is a networking library. It's built on top of go's http package and designed to create a common interface for simple RESTful services.

Installation

import "github.com/RadicalApp/go-networking"

Usage

Simplest way to use go-networking create an instance of connection and call GET(), POST() on it.

Connection
GET
	urlString := "https://jsonplaceholder.typicode.com/posts"
	params := networking.NewParams()
	connection := networking.NewConnection(urlString, params)

	connection.OnReceived = func(response []byte) {
		fmt.Println("Response: !!! ", string(response))
	}
	connection.OnError = func (err error) {
		t.Error("Error in GET request for url: ", urlString)
		t.Fail()
	}

	connection.GET()
Parameters
Params

Params is a common interface for all types of parameters. int, string or file.

Use FileWrapperBuilder class to initialize a file parameter using file url or file bytes.

Credits

go-networking is owned and maintained by the Radical App, LLC

go-networking was created in order to cater towards the development of Dust App which uses RESTful service for all activities.

Main contributors are:

Thanks for JSONPlaceholder for having test APIs.

If you wish to contribute to the project please follow the guidelines.

License

go-networking is released under the MIT license. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {

	// Functions to handle connection states
	OnStarted      OnStarted
	OnReceived     OnReceived
	OnClosed       OnClosed
	OnError        OnError
	OnProgress     OnProgress
	OnStateChanged OnStateChanged
	// contains filtered or unexported fields
}

`Connection` a struct to help connect to the real world

func NewConnection

func NewConnection(urlString string, params *Params) *Connection

NewConnection creates a new connection with the appropriate Dust auth headers.

func (*Connection) GET

func (c *Connection) GET()

Helper to send GET request

func (*Connection) Get

func (c *Connection) Get(completion func(Response, error))

Helper to send GET request with completion

func (*Connection) POST

func (c *Connection) POST()

Helper to send POST request

func (*Connection) Post

func (c *Connection) Post(completion func(Response, error))

Helper to send POST request with completion

func (*Connection) PutHeader

func (c *Connection) PutHeader(key, value string)

Add header, like Authorization or custom

func (*Connection) SetBasicAuth

func (c *Connection) SetBasicAuth(username string, password string)

Add basic authorization to the headers

func (*Connection) SetContentType

func (c *Connection) SetContentType(contentType string)

func (*Connection) SetMethod

func (c *Connection) SetMethod(method HTTP_METHOD)

Set method type. E.g.: GET, POST

func (*Connection) SetNumberOfRetries

func (c *Connection) SetNumberOfRetries(number int)

Set number of retries for a HTTP request

func (*Connection) SetTimeout

func (c *Connection) SetTimeout(timeoutInSeconds time.Duration)

Set timeout for the connection

func (*Connection) UPLOAD

func (c *Connection) UPLOAD()

Helper to send UPLOAD (type of POST request) request This should be used while POST'ing MULTIPART-FORM

func (*Connection) Upload

func (c *Connection) Upload(completion func(Response, error))

Helper to send UPLOAD (type of POST request) request with completion This should be used while POST'ing MULTIPART-FORM

type ConnectionBuilder

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

ConnectionBuilder is used to help build a `Connection`.

func (*ConnectionBuilder) Build

func (cb *ConnectionBuilder) Build() Connection

Build a `Connection`

func (*ConnectionBuilder) SetMethod

func (cb *ConnectionBuilder) SetMethod(method HTTP_METHOD) *ConnectionBuilder

Set HTTP Method type for a HTTP request

func (*ConnectionBuilder) SetParams

func (cb *ConnectionBuilder) SetParams(params *Params) *ConnectionBuilder

Set `Params` for a HTTP request

func (*ConnectionBuilder) SetUrlString

func (cb *ConnectionBuilder) SetUrlString(urlString string) *ConnectionBuilder

Set url string for a HTTP request

type ConnectionState

type ConnectionState string
const (
	CONNECTION_STATE_DISCONNECTED ConnectionState = "Disconnected"
	CONNECTION_STATE_CONNECTING   ConnectionState = "Connecting"
	CONNECTION_STATE_CONNECTED    ConnectionState = "Connected"
)

type FileWrapper

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

FileWrapper contains all necessary file data for upload.

func NewFileWrapper

func NewFileWrapper() FileWrapper

type FileWrapperBuilder

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

FileWrapperBuilder implements builder pattern to construct a FileWrapper

func NewFileWrapperBuilder

func NewFileWrapperBuilder() *FileWrapperBuilder

func (*FileWrapperBuilder) Build

func (fwb *FileWrapperBuilder) Build() FileWrapper

func (*FileWrapperBuilder) SetContentType

func (fwb *FileWrapperBuilder) SetContentType(contentType string) *FileWrapperBuilder

func (*FileWrapperBuilder) SetData

func (fwb *FileWrapperBuilder) SetData(data []byte) *FileWrapperBuilder

func (*FileWrapperBuilder) SetName

func (fwb *FileWrapperBuilder) SetName(name string) *FileWrapperBuilder

func (*FileWrapperBuilder) SetPath

func (fwb *FileWrapperBuilder) SetPath(path string) *FileWrapperBuilder

type HTTP_METHOD

type HTTP_METHOD string
const (
	HTTP_METHOD_GET    HTTP_METHOD = "GET"
	HTTP_METHOD_POST   HTTP_METHOD = "POST"
	HTTP_METHOD_UPLOAD HTTP_METHOD = "UPLOAD"
)

type IdealResponse

type IdealResponse interface {
	// contains filtered or unexported methods
}

Future implementation for offline usage

type OnClosed

type OnClosed func()

type OnError

type OnError func(err error)

type OnProgress

type OnProgress func(progress int)

type OnReceived

type OnReceived func(response Response)

type OnStarted

type OnStarted func()

Connection States

type OnStateChanged

type OnStateChanged func(state ConnectionState)

type Params

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

`Params` a wrapper struct to hold URL/ File/ Post parameters for HTTP Connection

func NewParams

func NewParams() *Params

Constructor for `Params`

func QueryParams

func QueryParams(stringParams map[string]string) *Params

func (*Params) Encoded

func (p *Params) Encoded() string

func (*Params) PutFile

func (p *Params) PutFile(key string, value FileWrapper)

PutFile add a key/val pair to the RequestParams, where value is a FileWrapper.

func (*Params) PutInt

func (p *Params) PutInt(key string, value int)

PutInt add a key/val pair to the RequestParams, where value is an int.

func (*Params) PutObject

func (p *Params) PutObject(key string, value interface{})

Put an object which will be converted to JSON object while `POST` request.

func (*Params) PutString

func (p *Params) PutString(key, value string)

PutString add a key/val pair to the RequestParams, where value is a string.

type Response

type Response struct {
	Data     []byte
	Request  http.Request
	Response http.Response
}

`Response` a struct that defines the response for a particular connection

func (*Response) IsEmpty

func (r *Response) IsEmpty() bool

Check if a `Response` is empty

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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