common

package
v0.0.0-...-96598f4 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package common provides a basic HTTP client to perform HTTP calls. It supports Basic and OAuth authentication methods.

Index

Constants

View Source
const Version = "0.1.0"

Variables

This section is empty.

Functions

func ConvertSliceItems

func ConvertSliceItems[T any](input any) ([]T, error)

func IsValidUUID

func IsValidUUID(u string) bool

IsValidUUID checks that u is a valid UUID4 string

Types

type AuthType

type AuthType int
const (
	NoAuth AuthType = iota + 1
	CustomerAuth
	UserAuth
	ApplicationAuth
)

func (AuthType) Choices

func (at AuthType) Choices() []string

func (AuthType) String

func (at AuthType) String() string

type Client

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

Client holds the configuration (url, auth) and wraps http Requests

func NewClient

func NewClient(serverUrl string, auth *ClientAuth) *Client

NewClient configures and returns a new Client

func (*Client) Call

func (c *Client) Call(method, path string, params map[string]interface{}) (
	*http.Response, error)

Performs a HTTP Call using Client configuration `params` map may contain the following keys:

  • "_data" is the data to be sent in the request body.
  • For "application/json" content type, data is serialized as JSON
  • For "application/x-www-form-urlencoded" content type, data is serialized as a form
  • "_rawResponse" is a boolean that indicates if the response should be returned as is
  • "Content-Type" is the content type of the request, it defaults to "application/json"
  • any other key-value pairs which doesn't start wiht a '_' are added as request headers

func (*Client) Delete

func (c *Client) Delete(path string) (*http.Response, error)

Delete wraps call to perform a HTTP DELETE call

func (*Client) Get

func (c *Client) Get(path string) (*http.Response, error)

Get wraps call to perform a HTTP GET call

func (*Client) GetAuth

func (c *Client) GetAuth() *ClientAuth

func (*Client) Patch

func (c *Client) Patch(path string, params map[string]interface{}) (
	*http.Response, error)

Patch wraps call to perform a HTTP PATCH call

func (*Client) Post

func (c *Client) Post(path string, params map[string]interface{}) (
	*http.Response, error)

Post wraps call to perform a HTTP POST call

func (*Client) Put

func (c *Client) Put(path string, params map[string]interface{}) (
	*http.Response, error)

Put wraps call to perform a HTTP PUT call

type ClientAuth

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

ClientAuth keeps the authentication details - Basic vs Bearer (OAuth)

func GetFakeAuth

func GetFakeAuth() *ClientAuth

func NewClientAuth

func NewClientAuth(data map[string]interface{}) *ClientAuth

NewClientAuth returns a new ClientAuth with auth set to NoAuth

func (*ClientAuth) GetAccessToken

func (ca *ClientAuth) GetAccessToken() string

func (*ClientAuth) GetAccessTokenExpire

func (ca *ClientAuth) GetAccessTokenExpire() int

func (*ClientAuth) GetApplicationId

func (ca *ClientAuth) GetApplicationId() string

func (*ClientAuth) GetAuthType

func (ca *ClientAuth) GetAuthType() AuthType

func (*ClientAuth) GetCustomerId

func (ca *ClientAuth) GetCustomerId() string

func (*ClientAuth) GetRefreshToken

func (ca *ClientAuth) GetRefreshToken() string

func (*ClientAuth) SetApplicationAuth

func (ca *ClientAuth) SetApplicationAuth(id, secret string) error

func (*ClientAuth) SetCustomerAuth

func (ca *ClientAuth) SetCustomerAuth(id, key string) error

Set ClientAuth authType to BasicAuth removing tokens

func (*ClientAuth) SetNoAuth

func (ca *ClientAuth) SetNoAuth()

Set ClientAuth authType to NoAuth removing other attributes

func (*ClientAuth) SetUserAuth

func (ca *ClientAuth) SetUserAuth(accessToken string, tokenExpire int,
	refreshToken string) error

func (*ClientAuth) SwitchBack

func (ca *ClientAuth) SwitchBack()

func (*ClientAuth) SwitchTo

func (ca *ClientAuth) SwitchTo(authType AuthType)

func (*ClientAuth) Update

func (ca *ClientAuth) Update(data map[string]interface{}) error

Jump to

Keyboard shortcuts

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