abstract

package
v0.0.0-...-d330e10 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2024 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Constants

View Source
const (
	APIKeyHeaderKey        = "x-api-key"
	AuthorizationHeaderKey = "Authorization"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AbstractClient

type AbstractClient interface {
	Path() string
	GetIPAddress() (string, error)
	Login(username, password string) error
	SetAPIKey(key string)
	SetToken(key string)
	GetToken() string
	GetURL(protocol ...string) string
	IsAuthenticated() bool
	NewRequest(method, path string, body io.Reader) (*http.Request, error)

	Get(ctx context.Context, path string) (*http.Response, error)
	Post(ctx context.Context, path string, body []byte) (*http.Response, error)
	Put(ctx context.Context, path string, body []byte) (*http.Response, error)
	Patch(ctx context.Context, path string, body []byte) (*http.Response, error)
	Delete(ctx context.Context, path string) (*http.Response, error)

	DialWebsocket(endpoint string) (*websocket.Conn, *http.Response, error)
	WriteToWebsocket(message []byte) error
	ReadFromWebsocket() (int, []byte, error)
	CloseWebsocketConnection() error
}

type Body

type Body struct {
	Origin string `json:"origin"`
}

type Config

type Config struct {
	ServiceConfig  urlfinder.ServiceConfig
	APIKey         string
	Token          string
	TimeoutSeconds int
}

Config is a struct that holds the configuration for the ServiceClient

type MockAbstractClient

type MockAbstractClient struct {
	Lock sync.Mutex

	NumCalledGetIPAddress int
	GetIPAddressError     error

	NumCalledGetURL int

	NumCalledLogin int
	LoginError     error

	NumCalledSetAPIKey int

	NumCalledSetToken int
	Token             string

	NumCalledGetToken int

	NumCalledIsAuthenticated int

	NumCalledActiveUserID int

	NumCalledGet int
	GetError     error

	NumCalledPost int
	PostError     error

	NumCalledPut int
	PutError     error

	NumCalledPatch int
	PatchError     error

	NumCalledDelete int
	DeleteError     error

	NumCalledDialWebsocket int
	DialWebsocketError     error

	NumCalledWriteToWebsocketError int
	WriteToWebsocketError          error

	NumCalledReadFromWebsocket int
	ReadFromWebsocketError     error

	NumCalledCloseWebsocket int
	CloseWebsocketError     error

	Response []byte
}

MockAbstractClient is a mock implementation of the AbstractClient interface

func (*MockAbstractClient) ActiveUserID

func (m *MockAbstractClient) ActiveUserID() int

ActiveUserID returns 1

func (*MockAbstractClient) CloseWebsocketConnection

func (m *MockAbstractClient) CloseWebsocketConnection() error

CloseWebsocketConnection increments the number of times it was called and returns an error if provided

func (*MockAbstractClient) Delete

func (m *MockAbstractClient) Delete(ctx context.Context, path string) (*http.Response, error)

Delete increments the number of times it was called and returns an error if provided

func (*MockAbstractClient) DialWebsocket

func (m *MockAbstractClient) DialWebsocket(endpoint string) (*websocket.Conn, *http.Response, error)

DialWebsocket increments the number of times it was called and returns an error if provided

func (*MockAbstractClient) Get

func (m *MockAbstractClient) Get(ctx context.Context, path string) (*http.Response, error)

Get increments the number of times it was called and returns an error if provided

func (*MockAbstractClient) GetIPAddress

func (m *MockAbstractClient) GetIPAddress() (string, error)

GetIPAddress returns an error if provided or localhost

func (*MockAbstractClient) GetToken

func (m *MockAbstractClient) GetToken() string

GetToken returns the token

func (*MockAbstractClient) GetURL

func (m *MockAbstractClient) GetURL(protocol ...string) string

GetURL returns a random URL

func (*MockAbstractClient) IsAuthenticated

func (m *MockAbstractClient) IsAuthenticated() bool

IsAuthenticated returns false

func (*MockAbstractClient) Login

func (m *MockAbstractClient) Login(username, password string) error

Login sets the token to a fake JWT

func (*MockAbstractClient) NewRequest

func (m *MockAbstractClient) NewRequest(method, path string, body io.Reader) (*http.Request, error)

NewRequest returns a new request

func (*MockAbstractClient) Patch

func (m *MockAbstractClient) Patch(ctx context.Context, path string, body []byte) (*http.Response, error)

Patch increments the number of times it was called and returns an error if provided

func (*MockAbstractClient) Path

func (m *MockAbstractClient) Path() string

Path returns "/api"

func (*MockAbstractClient) Post

func (m *MockAbstractClient) Post(ctx context.Context, path string, body []byte) (*http.Response, error)

Post increments the number of times it was called and returns an error if provided

func (*MockAbstractClient) Put

func (m *MockAbstractClient) Put(ctx context.Context, path string, body []byte) (*http.Response, error)

Put increments the number of times it was called and returns an error if provided

func (*MockAbstractClient) ReadFromWebsocket

func (m *MockAbstractClient) ReadFromWebsocket() (int, []byte, error)

ReadFromWebsocket increments the number of times it was called and returns an error if provided

func (*MockAbstractClient) SetAPIKey

func (m *MockAbstractClient) SetAPIKey(key string)

SetAPIKey increments the number of times it was called

func (*MockAbstractClient) SetToken

func (m *MockAbstractClient) SetToken(token string)

SetToken sets the token to the provided value

func (*MockAbstractClient) WriteToWebsocket

func (m *MockAbstractClient) WriteToWebsocket(message []byte) error

WriteToWebsocket increments the number of times it was called and returns an error if provided

type Response

type Response struct {
	Message string `json:"message"`
	Error   string `json:"error"`
}

Response is a struct that represents a basic response from the server.

type ServiceClient

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

ServiceClient is a struct that handles generic http client operations

func NewClient

func NewClient(config Config) *ServiceClient

NewClient creates a new ServiceClient given a config struct

func (*ServiceClient) Client

func (a *ServiceClient) Client() *http.Client

Client returns the resty client

func (*ServiceClient) CloseWebsocketConnection

func (a *ServiceClient) CloseWebsocketConnection() error

CloseWebsocketConnection closes the websocket connection.

func (*ServiceClient) Delete

func (a *ServiceClient) Delete(ctx context.Context, path string) (*http.Response, error)

Delete makes a DELETE request to the URL

func (*ServiceClient) DialWebsocket

func (a *ServiceClient) DialWebsocket(endpoint string) (*websocket.Conn, *http.Response, error)

DialWebsocket creates a new websocket connection to the given endpoint.

func (*ServiceClient) Get

func (a *ServiceClient) Get(ctx context.Context, path string) (*http.Response, error)

Get makes a GET request to the URL

func (*ServiceClient) GetAPIKey

func (a *ServiceClient) GetAPIKey() string

GetAPIKey returns the token of the service client

func (*ServiceClient) GetAuthHeader

func (a *ServiceClient) GetAuthHeader() http.Header

GetAuthHeader gets the current auth header of the service client

func (*ServiceClient) GetIPAddress

func (a *ServiceClient) GetIPAddress() (string, error)

GetIPAddress returns the IP address of the client using a get request to httpbin.org/ip

func (*ServiceClient) GetToken

func (a *ServiceClient) GetToken() string

GetToken returns the token of the service client

func (*ServiceClient) GetURL

func (a *ServiceClient) GetURL(protocolInput ...string) string

GetURL returns the url of the service client for the given protocol

func (*ServiceClient) GetURLWithPath

func (a *ServiceClient) GetURLWithPath(path string, protocolInput ...string) string

GetURLWithPath returns the url of the service client with the given path appended

func (*ServiceClient) IsAuthenticated

func (a *ServiceClient) IsAuthenticated() bool

IsAuthenticated returns true if the client is authenticated

func (*ServiceClient) NewRequest

func (a *ServiceClient) NewRequest(method, path string, body io.Reader) (*http.Request, error)

NewRequest returns a new resty request

func (*ServiceClient) Patch

func (a *ServiceClient) Patch(ctx context.Context, path string, body []byte) (*http.Response, error)

Patch makes a PATCH request to the URL

func (*ServiceClient) Path

func (a *ServiceClient) Path() string

Path returns the path of this service client

func (*ServiceClient) Post

func (a *ServiceClient) Post(ctx context.Context, path string, body []byte) (*http.Response, error)

Post makes a POST request to the URL

func (*ServiceClient) Put

func (a *ServiceClient) Put(ctx context.Context, path string, body []byte) (*http.Response, error)

Put makes a PUT request to the URL

func (*ServiceClient) ReadFromWebsocket

func (a *ServiceClient) ReadFromWebsocket() (int, []byte, error)

ReadFromWebsocket reads a message from the websocket connection.

func (*ServiceClient) RoundTrip

func (a *ServiceClient) RoundTrip(req *http.Request) (*http.Response, error)

func (*ServiceClient) SetAPIKey

func (a *ServiceClient) SetAPIKey(key string)

SetAPIKey sets the token of the service client

func (*ServiceClient) SetHeader

func (a *ServiceClient) SetHeader(key string, value string)

SetHeader sets a header of the service client

func (*ServiceClient) SetToken

func (a *ServiceClient) SetToken(token string)

SetToken sets the token of the service client

func (*ServiceClient) WriteToWebsocket

func (a *ServiceClient) WriteToWebsocket(message []byte) error

WriteToWebsocket writes a message to the websocket connection.

Jump to

Keyboard shortcuts

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