http

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 14 Imported by: 5

README

HTTP

http defines an HTTP client implementation. It is a thin wrapper around the Go standard package net/http but in Python requests style.

Functions

get(url,params={},headers={},auth=(),timeout=0,allow_redirects=True,verify=True) response

Perform an HTTP GET request, returning a response.

Parameters
name type description
url string URL to request.
headers dict optional. dictionary of headers to add to request.
auth tuple optional. (username,password) tuple for HTTP Basic authorization.
timeout float optional. how many seconds to wait for the server to send all the data before giving up. 0 means no timeout.
allow_redirects bool optional. whether to follow redirects.
verify bool optional. whether to verify the server's SSL certificate.
put(url,params={},headers={},body="",form_body={},form_encoding="",json_body={},auth=(),timeout=0,allow_redirects=True,verify=True) response

Perform an HTTP PUT request, returning a response.

Parameters
name type description
url string URL to request.
headers dict optional. dictionary of headers to add to request.
body string optional. raw string body to provide to the request.
form_body dict optional. dict of values that will be encoded as form data.
form_encoding string optional. application/x-www-form-url-encoded (default) or multipart/form-data.
json_body any optional. JSON data to supply as a request. handy for working with JSON-API's.
auth tuple optional. (username,password) tuple for HTTP Basic authorization.
timeout float optional. how many seconds to wait for the server to send all the data before giving up. 0 means no timeout.
allow_redirects bool optional. whether to follow redirects.
verify bool optional. whether to verify the server's SSL certificate.
post(url,params={},headers={},body="",form_body={},form_encoding="",json_body={},auth=(),timeout=0,allow_redirects=True,verify=True) response

Perform an HTTP POST request, returning a response.

Parameters
name type description
url string URL to request.
headers dict optional. dictionary of headers to add to request.
body string optional. raw string body to provide to the request.
form_body dict optional. dict of values that will be encoded as form data.
form_encoding string optional. application/x-www-form-url-encoded (default) or multipart/form-data.
json_body any optional. JSON data to supply as a request. handy for working with JSON-API's.
auth tuple optional. (username,password) tuple for HTTP Basic authorization.
timeout float optional. how many seconds to wait for the server to send all the data before giving up. 0 means no timeout.
allow_redirects bool optional. whether to follow redirects.
verify bool optional. whether to verify the server's SSL certificate.
postForm(url,params={},headers={},form_body={},form_encoding="",auth=(),timeout=0,allow_redirects=True,verify=True) response

Perform an HTTP POST request with form data, returning a response.

Parameters
name type description
url string URL to request.
headers dict optional. dictionary of headers to add to request.
form_body dict optional. dict of values that will be encoded as form data.
form_encoding string optional. application/x-www-form-url-encoded (default) or multipart/form-data.
auth tuple optional. (username,password) tuple for HTTP Basic authorization.
timeout float optional. how many seconds to wait for the server to send all the data before giving up. 0 means no timeout.
allow_redirects bool optional. whether to follow redirects.
verify bool optional. whether to verify the server's SSL certificate.
delete(url,params={},headers={},body="",form_body={},form_encoding="",json_body={},auth=(),timeout=0,allow_redirects=True,verify=True) response

Perform an HTTP DELETE request, returning a response.

Parameters
name type description
url string URL to request.
headers dict optional. dictionary of headers to add to request.
body string optional. raw string body to provide to the request.
form_body dict optional. dict of values that will be encoded as form data.
form_encoding string optional. application/x-www-form-url-encoded (default) or multipart/form-data
json_body any optional. JSON data to supply as a request. handy for working with JSON-API's.
auth tuple optional. (username,password) tuple for HTTP Basic authorization.
timeout float optional. how many seconds to wait for the server to send all the data before giving up. 0 means no timeout.
allow_redirects bool optional. whether to follow redirects.
verify bool optional. whether to verify the server's SSL certificate.
patch(url,params={},headers={},body="",form_body={},form_encoding="",json_body={},auth=(),timeout=0,allow_redirects=True,verify=True) response

Perform an HTTP PATCH request, returning a response.

Parameters
name type description
url string URL to request.
headers dict optional. dictionary of headers to add to request.
body string optional. raw string body to provide to the request.
form_body dict optional. dict of values that will be encoded as form data.
form_encoding string optional. application/x-www-form-url-encoded (default) or multipart/form-data.
json_body any optional. JSON data to supply as a request. handy for working with JSON-API's.
auth tuple optional. (username,password) tuple for HTTP Basic authorization.
timeout float optional. how many seconds to wait for the server to send all the data before giving up. 0 means no timeout.
allow_redirects bool optional. whether to follow redirects.
verify bool optional. whether to verify the server's SSL certificate.
options(url,params={},headers={},body="",form_body={},form_encoding="",json_body={},auth=(),timeout=0,allow_redirects=True,verify=True) response

Perform an HTTP OPTIONS request, returning a response.

Parameters
name type description
url string URL to request.
headers dict optional. dictionary of headers to add to request.
body string optional. raw string body to provide to the request.
form_body dict optional. dict of values that will be encoded as form data.
form_encoding string optional. application/x-www-form-url-encoded (default) or multipart/form-data.
json_body any optional. JSON data to supply as a request. handy for working with JSON-API's.
auth tuple optional. (username,password) tuple for HTTP Basic authorization.
timeout float optional. how many seconds to wait for the server to send all the data before giving up. 0 means no timeout.
allow_redirects bool optional. whether to follow redirects.
verify bool optional. whether to verify the server's SSL certificate.

Types

response

The result of performing a HTTP request.

Fields

name type description
url string the URL that was ultimately requested (may change after redirects).
status_code int response status code (for example: 200 == OK).
headers dict dictionary of response headers.
encoding string transfer encoding. example: "octet-stream" or "application/json".

Methods

body() string

output response body as a string

json() object

attempt to parse response body as json, returning a JSON-decoded result.

Documentation

Overview

Package http defines a module for doing http operations in Starlark.

Migrated from: https://github.com/qri-io/starlib/tree/master/http

Index

Constants

View Source
const ModuleName = "http"

ModuleName defines the expected name for this Module when used in starlark's load() function, eg: load('http', 'get')

Variables

View Source
var (
	// UserAgent is the default user agent for http requests, override with a custom value before calling LoadModule.
	UserAgent = "Starlet-http-client/" + itn.StarletVersion
	// TimeoutSecond is the default timeout in seconds for http requests, override with a custom value before calling LoadModule.
	TimeoutSecond = 30
	// SkipInsecureVerify controls whether to skip TLS verification, override with a custom value before calling LoadModule.
	SkipInsecureVerify = false
	// DisableRedirect controls whether to follow redirects, override with a custom value before calling LoadModule.
	DisableRedirect = false
	// Client is the http client used to create the http module, override with a custom client before calling LoadModule.
	Client *http.Client
	// Guard is a global RequestGuard used in LoadModule, override with a custom implementation before calling LoadModule.
	Guard RequestGuard
)

Functions

func AsString

func AsString(x starlark.Value) (string, error)

AsString unquotes a starlark string value

func ConvertServerRequest added in v0.0.11

func ConvertServerRequest(r *http.Request) *starlarkstruct.Struct

ConvertServerRequest converts a http.Request to a Starlark struct for use in Starlark scripts on the server side.

func LoadModule

func LoadModule() (starlark.StringDict, error)

LoadModule creates an http Module

Types

type ExportedServerResponse added in v0.0.11

type ExportedServerResponse struct {
	StatusCode int         // StatusCode is the status code of the response.
	Header     http.Header // Header is the header of the response, a map of string to list of strings. Content-Type is set automatically.
	Data       []byte      // Data is the data of the response, usually the body content.
}

ExportedServerResponse is a struct to export the response data to Go.

type Module

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

Module joins http tools to a dataset, allowing dataset to follow along with http requests

func (*Module) StringDict

func (m *Module) StringDict() starlark.StringDict

StringDict returns all module methods in a starlark.StringDict

func (*Module) Struct

func (m *Module) Struct() *starlarkstruct.Struct

Struct returns this module's methods as a starlark Struct

type RequestGuard

type RequestGuard interface {
	Allowed(thread *starlark.Thread, req *http.Request) (*http.Request, error)
}

RequestGuard controls access to http by checking before making requests if Allowed returns an error the request will be denied

type Response

type Response struct {
	http.Response
}

Response represents an HTTP response, wrapping a go http.Response with starlark methods

func (*Response) HeadersDict

func (r *Response) HeadersDict() *starlark.Dict

HeadersDict flops

func (*Response) JSON

func (r *Response) JSON(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

JSON attempts to parse the response body as JSON

func (*Response) Struct

func (r *Response) Struct() *starlarkstruct.Struct

Struct turns a response into a *starlark.Struct

func (*Response) Text

func (r *Response) Text(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

Text returns the raw data as a string

type ServerResponse added in v0.0.11

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

ServerResponse is a Starlark struct to save info in Starlark scripts to modify http.ResponseWriter outside on the server side.

func NewServerResponse added in v0.0.11

func NewServerResponse() *ServerResponse

NewServerResponse creates a new ServerResponse.

func (*ServerResponse) Export added in v0.0.11

Export dumps the response data to a struct for later use in Go.

func (*ServerResponse) Struct added in v0.0.11

func (r *ServerResponse) Struct() *starlarkstruct.Struct

Struct returns a Starlark struct for use in Starlark scripts.

func (*ServerResponse) Write added in v0.0.11

func (r *ServerResponse) Write(w http.ResponseWriter) (err error)

Write writes the response to http.ResponseWriter.

Jump to

Keyboard shortcuts

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