Documentation
¶
Overview ¶
Package remote provides a simple and clean API for making HTTP requests to remote servers.
Package remote provides a simple and clean API for making HTTP requests to remote servers.
Index ¶
- Constants
- type ErrorReport
- type Middleware
- type Transaction
- func (t *Transaction) Body(value string) *Transaction
- func (t *Transaction) ContentType(value string) *Transaction
- func (t *Transaction) ErrorReport() ErrorReport
- func (t *Transaction) Form(name string, value string) *Transaction
- func (t *Transaction) Header(name string, value string) *Transaction
- func (t *Transaction) JSON(value interface{}) *Transaction
- func (t *Transaction) Query(name string, value string) *Transaction
- func (t *Transaction) Response(success interface{}, failure interface{}) *Transaction
- func (t *Transaction) Send() *derp.Error
- func (t *Transaction) Use(middleware ...Middleware) *Transaction
- func (t *Transaction) XML(value interface{}) *Transaction
Constants ¶
const ContentType = "Content-Type"
ContentType is the string used in the HTTP header to designate a MIME type
const ContentTypeForm = "application/x-www-form-urlencoded"
ContentTypeForm is the standard MIME Type for Form encoded content
const ContentTypeHTML = "text/html"
ContentTypeHTML is the standard MIME type for HTML content
const ContentTypeJSON = "application/json"
ContentTypeJSON is the standard MIME Type for JSON content
const ContentTypePlain = "text/plain"
ContentTypePlain is the default plaintext MIME type
const ContentTypeXML = "application/xml"
ContentTypeXML is the standard MIME Type for XML content
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrorReport ¶
type ErrorReport struct {
URL string `json:"url"`
Request struct {
Method string `json:"method"`
Header map[string]string `json:"header"`
Body string `json:"body"`
} `json:"request"`
Response struct {
StatusCode int `json:"statusCode"`
Status string `json:"status"`
Header http.Header `json:"header"`
Body string `json:"body"`
} `json:"response"`
}
ErrorReport includes all the data returned by a transaction if it throws an error for any reason.
type Middleware ¶
type Middleware struct {
Config func(*Transaction) *derp.Error // Config is executed on the Transaction, before it is compiled into an http.Request object
Request func(*http.Request) *derp.Error // Request is executed on the http.Request, before it is sent to the remote server
Response func(*http.Response, *[]byte) *derp.Error // Response is executed on the http.Response, before it is parsed into a success or failure object.
}
Middleware is a decorator that can modify the request before it is sent to the remote HTTP server, or modify the response after it is returned by the remote HTTP server.
type Transaction ¶
type Transaction struct {
Client *http.Client // HTTP client to use to execute the request. This may be overridden or updated by the calling program.
Method string // HTTP method to use when sending the request
URLValue string // URL of the remote server to call
HeaderValues map[string]string // HTTP Header values to send in the request
QueryString url.Values // Query String to append to the URL
FormData url.Values // (if set) Form data to pass to the remote server as x-www-form-urlencoded
BodyObject interface{} // Other data to send in the body. Encoding determined by header["Content-Type"]
SuccessObject interface{} // Object to parse the response into -- IF the status code is successful
FailureObject interface{} // Object to parse the response into -- IF the status code is NOT successful
Middleware []Middleware // Middleware to execute on the request/response
RequestObject *http.Request // HTTP request that is delivered to the remote server
ResponseObject *http.Response // HTTP response that is returned from the remote server
}
Transaction represents a single HTTP request/response to a remote HTTP server.
func Delete ¶
func Delete(url string) *Transaction
Delete creates a new HTTP request to the designated URL, using the DELETE method.
func Get ¶
func Get(url string) *Transaction
Get creates a new HTTP request to the designated URL, using the GET method
func Patch ¶
func Patch(url string) *Transaction
Patch creates a new HTTP request to the designated URL, using the PATCH method
func Post ¶
func Post(url string) *Transaction
Post creates a new HTTP request to the designated URL, using the POST method
func Put ¶
func Put(url string) *Transaction
Put creates a new HTTP request to the designated URL, using the PUT method
func (*Transaction) Body ¶
func (t *Transaction) Body(value string) *Transaction
Body sets the request body, to be encoded as plain text
func (*Transaction) ContentType ¶
func (t *Transaction) ContentType(value string) *Transaction
ContentType sets the Content-Type header of the HTTP request.
func (*Transaction) ErrorReport ¶
func (t *Transaction) ErrorReport() ErrorReport
ErrorReport generates a data dump of the current state of the HTTP transaction. This is used when reporting errors via derp, to provide insights into what went wrong.
func (*Transaction) Form ¶
func (t *Transaction) Form(name string, value string) *Transaction
Form adds a name/value pair to the form data to be sent to the remote server.
func (*Transaction) Header ¶
func (t *Transaction) Header(name string, value string) *Transaction
Header sets a designated header value in the HTTP request.
func (*Transaction) JSON ¶
func (t *Transaction) JSON(value interface{}) *Transaction
JSON sets the request body, to be encoded as JSON.
func (*Transaction) Query ¶
func (t *Transaction) Query(name string, value string) *Transaction
Query sets a name/value pair in the URL query string.
func (*Transaction) Response ¶
func (t *Transaction) Response(success interface{}, failure interface{}) *Transaction
Response sets the objects for parsing HTTP success and failure responses
func (*Transaction) Send ¶
func (t *Transaction) Send() *derp.Error
Send executes the transaction, sending the request to the remote server.
func (*Transaction) Use ¶
func (t *Transaction) Use(middleware ...Middleware) *Transaction
Use lets you add middleware to the transaction. Middleware is able to modify transaction data before and after it is sent to the remote server.
func (*Transaction) XML ¶
func (t *Transaction) XML(value interface{}) *Transaction
XML sets the request body, to be encoded as XML.