util

package
v0.0.0-...-b5648fb Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2019 License: Apache-2.0 Imports: 7 Imported by: 20

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallAPI

func CallAPI(method, url string, content *[]byte, h ...string) (*http.Response, error)

CallAPI sends an HTTP request using "method" to "url". For uploading / sending file, caller needs to set the "content". Otherwise, set it to zero length []byte. If Header fields need to be set, then set it in "h". "h" needs to be even numbered, i.e. pairs of field name and the field content.

fileContent, err := ioutil.ReadFile("fileName.ext");

resp, err := CallAPI("PUT", "http://domain/hello/", &fileContent, "Name", "world")

is similar to: curl -X PUT -H "Name: world" -T fileName.ext http://domain/hello/

func CheckHTTPResponseStatusCode

func CheckHTTPResponseStatusCode(resp *http.Response) error

CheckHTTPResponseStatusCode compares http response header StatusCode against expected statuses. Primary function is to ensure StatusCode is in the 20x (return nil). Ok: 200. Created: 201. Accepted: 202. No Content: 204. Partial Content: 206. Otherwise return error message.

func Delete

func Delete(url string, token string, client http.Client) (err error)

Delete sends an Http Request with using the "DELETE" method and with an "X-Auth-Token" header set to the specified token value. The request is made by the specified client.

func GetJSON

func GetJSON(url string, token string, client http.Client, val interface{}) (err error)

GetJSON sends an Http Request with using the "GET" method and with an "Accept" header set to "application/json" and the authenication token set to the specified token value. The request is made by the specified client. The val interface should be a pointer to the structure that the json response should be decoded into.

func PostJSON

func PostJSON(url string, token string, client http.Client, inputValue interface{}, outputValue interface{}) (err error)

PostJSON sends an Http Request with using the "POST" method and with a "Content-Type" header with application/json and X-Auth-Token" header set to the specified token value. The inputValue is encoded to json and sent in the body of the request. The response json body is decoded into the outputValue. If the response does sends an invalid or error status code then an error will be returned. If the Content-Type value of the response is not "application/json" an error is returned.

Types

type RFC8601DateTime

type RFC8601DateTime struct {
	time.Time
}

RFC8601DateTime is a type for decoding and encoding json date times that follow RFC 8601 format. The type currently decodes and encodes with exactly precision to seconds. If more formats of RFC8601 need to be supported additional work will be needed.

func NewDateTime

func NewDateTime(input string) (val RFC8601DateTime, err error)

NewDateTime creates a new RFC8601DateTime taking a string as input. It must follow the "2006-01-02T15:04:05" pattern.

func (RFC8601DateTime) MarshalJSON

func (r RFC8601DateTime) MarshalJSON() ([]byte, error)

MarshalJSON converts a RFC8601DateTime to a []byte.

func (*RFC8601DateTime) UnmarshalJSON

func (r *RFC8601DateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON converts the bytes give to a RFC8601DateTime Errors will occur if the bytes when converted to a string don't match the format "2006-01-02T15:04:05".

Jump to

Keyboard shortcuts

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