requests

package
v3.12.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthKeyHeader

func AuthKeyHeader(key *rsa.PublicKey) map[string][]string

AuthKeyHeader is a header prototype including an encoded RSA PublicKey

Example
headers, err := json.Marshal(AuthKeyHeader(&privateKey.PublicKey))
if err != nil {
	fmt.Printf("error: %s", err.Error())
	return
}

fmt.Println(string(headers))
Output:

{"X-Yoti-Auth-Key":["MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpTiICtL+ujx8D0FquVWIaXg+ajJadN5hsTlGUXymiFAunSZjLjTsoGfSPz8PJm6pG9ax1Qb+R5UsSgTRTcpZTps2RLRWr5oPfD66bz4l38QXPSvfg5o+5kNxyCb8QANitF7Ht/DcpsGpL7anruHg/RgCLCBFRaGAodfuJCCM9zwIDAQAB"]}

func Execute

func Execute(httpClient HttpClient, request *http.Request, httpErrorMessages ...map[int]string) (response *http.Response, err error)

Execute makes a request to the specified endpoint, with an optional payload

func JSONHeaders

func JSONHeaders() map[string][]string

JSONHeaders is a header prototype for JSON based requests

Example
jsonHeaders, err := json.Marshal(JSONHeaders())
if err != nil {
	fmt.Printf("error: %s", err.Error())
	return
}

fmt.Println(string(jsonHeaders))
Output:

{"Accept":["application/json"],"Content-Type":["application/json"]}

func MergeHeaders

func MergeHeaders(headers ...map[string][]string) map[string][]string

MergeHeaders merges two or more header prototypes together from left to right

Example
left := map[string][]string{"A": {"Value Of A"}}
right := map[string][]string{"B": {"Value Of B"}}

merged := MergeHeaders(left, right)
fmt.Println(merged["A"])
fmt.Println(merged["B"])
Output:

[Value Of A]
[Value Of B]

Types

type HttpClient

type HttpClient interface {
	Do(*http.Request) (*http.Response, error)
}

HttpClient is a mockable HTTP Client Interface

type SignedRequest

type SignedRequest struct {
	Key        *rsa.PrivateKey
	HTTPMethod string
	BaseURL    string
	Endpoint   string
	Headers    map[string][]string
	Params     map[string]string
	Body       []byte
	Error      error
}

SignedRequest is a builder for constructing a http.Request with Yoti signing

Example (GenerateDigest)
msg := &SignedRequest{
	HTTPMethod: http.MethodPost,
	Body:       []byte("simple message body"),
}
fmt.Println(msg.generateDigest("endpoint"))
Output:

POST&endpoint&c2ltcGxlIG1lc3NhZ2UgYm9keQ==

func (SignedRequest) Request

func (msg SignedRequest) Request() (request *http.Request, err error)

Request builds a http.Request with signature headers

func (SignedRequest) WithPemFile

func (msg SignedRequest) WithPemFile(in []byte) SignedRequest

WithPemFile loads the private key from a PEM file reader

Example
msg := SignedRequest{}.WithPemFile([]byte(`
-----BEGIN RSA PRIVATE KEY-----
` + exampleKey + `
-----END RSA PRIVATE KEY-----`))
fmt.Println(AuthKeyHeader(&msg.Key.PublicKey))
Output:

map[X-Yoti-Auth-Key:[MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpTiICtL+ujx8D0FquVWIaXg+ajJadN5hsTlGUXymiFAunSZjLjTsoGfSPz8PJm6pG9ax1Qb+R5UsSgTRTcpZTps2RLRWr5oPfD66bz4l38QXPSvfg5o+5kNxyCb8QANitF7Ht/DcpsGpL7anruHg/RgCLCBFRaGAodfuJCCM9zwIDAQAB]]

Jump to

Keyboard shortcuts

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