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) Accept(contentTypes ...string) *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) IsContentTypeEmpty() bool
- func (t *Transaction) JSON(value any) *Transaction
- func (t *Transaction) Query(name string, value string) *Transaction
- func (t *Transaction) Response(success any, failure any) *Transaction
- func (t *Transaction) Send() error
- func (t *Transaction) Use(middleware ...Middleware) *Transaction
- func (t *Transaction) XML(value any) *Transaction
Constants ¶
const Accept = "Accept"
Accept is the string used in the HTTP header to request a response be encoded as a MIME type
const ContentType = "Content-Type"
ContentType is the string used in the HTTP header to designate a MIME type
const ContentTypeActivityPub = "application/activity+json"
ContentTypeActivityPub is the standard MIME type for ActivityPub content
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 ContentTypeJSONFeed = "application/feed+json"
ContentTypeJSONFeed is the standard MIME Type for JSON Feed content https://en.wikipedia.org/wiki/JSON_Feed
const ContentTypeJSONLD = "application/ld+json"
ContentTypeJSONLD is the standard MIME Type for JSON-LD content https://en.wikipedia.org/wiki/JSON-LD
const ContentTypeJSONResourceDescriptor = "application/jrd+json"
ContentTypeJSONResourceDescriptor is the standard MIME Type for JSON Resource Descriptor content which is used by WebFinger: https://datatracker.ietf.org/doc/html/rfc7033#section-10.2
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) error // Config is executed on the Transaction, before it is compiled into an http.Request object Request func(*http.Request) error // Request is executed on the http.Request, before it is sent to the remote server Response func(*http.Response, *[]byte) 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 any // Other data to send in the body. Encoding determined by header["Content-Type"] SuccessObject any // Object to parse the response into -- IF the status code is successful FailureObject any // 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) Accept ¶ added in v0.10.1
func (t *Transaction) Accept(contentTypes ...string) *Transaction
Accept sets the Content-Type header of the HTTP request.
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) IsContentTypeEmpty ¶ added in v0.10.1
func (t *Transaction) IsContentTypeEmpty() bool
func (*Transaction) JSON ¶
func (t *Transaction) JSON(value any) *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 any, failure any) *Transaction
Response sets the objects for parsing HTTP success and failure responses
func (*Transaction) Send ¶
func (t *Transaction) Send() 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 any) *Transaction
XML sets the request body, to be encoded as XML.