api_client_go

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: Apache-2.0 Imports: 20 Imported by: 2

README

Api Client Library

Repository in development process, do not use in production environments.

This repository contains the library to communicate with Nuvla API Server.

The module provides multiple clients depending on the type of operations you want to perform. The clients are built using the Composition pattern, so that the base client is extended with the specific operations required for the client.

Client Name Description Dev. Status
NuvlaClient Contains generic HTTP methods implemented.
- Login
- Get
- Post
- Put
- Remove
- Search
This is the base client which is then imported in all the other clients using the Composition pattern
Pre-production
User Client Extends the above mentioned client and provides utilities to execute operations related (and allowed) to the user. - Add/Remove resource - Get resource - Search resource - LogIn Then it also has wrappers to access the lower level HTTP operations from NuvlaClient for special operations and non-covered resources Pre-production
NuvlaEdge Client Following the same pattern, creates a client with specific operations NuvlaEdge requires:
- Activate
- Commission
- Telemetry
- GetResources (Retrieves all the accessible resources from NuvlaEdge)
Pre-production
Deployment Client Client to control Deployment resources and related operations Pre-production
Job Client Client to control and retrieve Job resources and related operations Pre-production

Usage

Basic usage for generic NuvlaClient:

package main

import (
	"fmt"
	nuvla "github.com/nuvla/api-client-go/"
)

func main() {
	// Create a new client
	client := nuvla.NewNuvlaClient("https://nuvla.io", false, false)

	// Login
	client.LoginApiKeys("api-key", "api-secret")

	// Get a resource
	res, _ := client.Get("nuvlabox/nuvlabox-id")
	fmt.Println("Resource: ", res)
	
	// Post a resource
	client.Post("resource-endpoint", "resource-data")

	// Put a resource
	client.Put("resource-endpoint", "resource-data", "data-to-delete")

	// Remove a resource
	client.Remove("resource-id")
}

Generic usage for NuvlaClient with detailed configuration:

package main

import (
	"fmt"
	nuvla "github.com/nuvla/api-client-go/"
)

func main() {
	// Create a new client
	// Create options using NewSessionOpts method creates them with default values 
	clientOps := nuvla.NewSessionOpts(&nuvla.SessionOpts{})
	client := nuvla.NewNuvlaClientFromOpts(clientOps)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReAuthenticateSession added in v0.2.0

func ReAuthenticateSession(opts *SessionOptions)

func SanitiseEndpoint added in v0.7.11

func SanitiseEndpoint(endpoint string) string

func WithOutCompressSession added in v0.2.0

func WithOutCompressSession(opts *SessionOptions)

func WithoutPersistCookie added in v0.2.0

func WithoutPersistCookie(opts *SessionOptions)

Types

type NuvlaClient

type NuvlaClient struct {
	// Session params
	*NuvlaSession
	SessionOpts SessionOptions
	Credentials types.LogInParams
}

func NewNuvlaClient

func NewNuvlaClient(cred types.LogInParams, opts *SessionOptions) *NuvlaClient

func NewNuvlaClientFromOpts

func NewNuvlaClientFromOpts(cred types.LogInParams, opts ...SessionOptFunc) *NuvlaClient

func (*NuvlaClient) Add added in v0.7.0

func (nc *NuvlaClient) Add(ctx context.Context, resourceType resources.NuvlaResourceType, data map[string]interface{}) (*types.NuvlaID, error)

Add creates a new resource of the given type and returns its ID

func (*NuvlaClient) BulkOperation added in v0.7.7

func (nc *NuvlaClient) BulkOperation(ctx context.Context, resourceId string, operation string, data []map[string]interface{}) (*http.Response, error)

func (*NuvlaClient) BulkPost added in v0.7.7

func (nc *NuvlaClient) BulkPost(ctx context.Context, endpoint string, data []map[string]interface{}) (*http.Response, error)

func (*NuvlaClient) Delete

func (nc *NuvlaClient) Delete(ctx context.Context, resourceId string) (*http.Response, error)

func (*NuvlaClient) Edit

func (nc *NuvlaClient) Edit(ctx context.Context, resourceId string, data map[string]interface{}, toSelect []string) (*http.Response, error)

func (*NuvlaClient) Get

func (nc *NuvlaClient) Get(ctx context.Context, resourceId string, selectFields []string) (*types.NuvlaResource, error)

func (*NuvlaClient) IsAuthenticated

func (nc *NuvlaClient) IsAuthenticated() bool

func (*NuvlaClient) LoginApiKeys

func (nc *NuvlaClient) LoginApiKeys(key string, secret string) error

func (*NuvlaClient) LoginUser

func (nc *NuvlaClient) LoginUser(username string, password string) error

func (*NuvlaClient) Logout

func (nc *NuvlaClient) Logout() error

func (*NuvlaClient) Operation

func (nc *NuvlaClient) Operation(ctx context.Context, resourceId, operation string, data map[string]interface{}) (*http.Response, error)

func (*NuvlaClient) Post

func (nc *NuvlaClient) Post(ctx context.Context, endpoint string, data map[string]interface{}) (*http.Response, error)

Post executes the post http method Data can be any type, but it will be marshaled into JSON

func (*NuvlaClient) Put

func (nc *NuvlaClient) Put(ctx context.Context, uri string, data interface{}, selectFields []string) (*http.Response, error)

func (*NuvlaClient) Search added in v0.6.0

func (nc *NuvlaClient) Search(ctx context.Context, resourceType string, opts *SearchOptions) (*resources.NuvlaResourceCollection, error)

type NuvlaCookies

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

func NewNuvlaCookies

func NewNuvlaCookies(cookieFile string, endpoint string) *NuvlaCookies

NewNuvlaCookies creates a new instance of the NuvlaCookies struct. It takes two parameters: cookieFile and endpoint.

Parameters:

  • cookieFile (string): This is the path to the file where jar will be saved or loaded from.
  • endpoint (string): This is the URL endpoint for which the jar are relevant.

The function does the following: 1. Creates a new NuvlaCookies instance and sets the cookieFile field. 2. Parses the endpoint string into a url.URL object and sets the endpoint field of the NuvlaCookies instance. 3. Checks if the cookieFile exists and is not empty. If it is, it creates a new cookiejar.Jar, attempts to load jar from the cookieFile into the cookiejar.Jar, and sets the jar field of the NuvlaCookies instance.

Returns:

  • A pointer to the newly created NuvlaCookies instance.

Example:

jar := client.NewNuvlaCookies("/path/to/jar.txt", "http://example.com")
In this example, a new NuvlaCookies instance is created. The jar relevant to the "http://example.com" endpoint will be saved to or loaded from the "/path/to/jar.txt" file.

func (*NuvlaCookies) Save

func (c *NuvlaCookies) Save() error

func (*NuvlaCookies) SaveIfNeeded

func (c *NuvlaCookies) SaveIfNeeded(newCookie http.CookieJar) error

SaveIfNeeded jar if needed

type NuvlaSession

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

func NewNuvlaSession

func NewNuvlaSession(sessionAttrs *SessionOptions) *NuvlaSession

func (*NuvlaSession) GetSessionOpts added in v0.5.0

func (s *NuvlaSession) GetSessionOpts() SessionOptions

func (*NuvlaSession) NeedToLogin

func (s *NuvlaSession) NeedToLogin() bool

func (*NuvlaSession) Request

func (s *NuvlaSession) Request(ctx context.Context, reqInput *types.RequestOpts) (*http.Response, error)

func (*NuvlaSession) String

func (s *NuvlaSession) String() string

type SearchOptions added in v0.6.0

type SearchOptions struct {
	First       int      `json:"first"`
	Last        int      `json:"last"`
	Filter      string   `json:"filter"`
	Fields      string   `json:"fields"`
	OrderBy     string   `json:"orderby"`
	Select      []string `json:"select"`
	Aggregation string   `json:"aggregation"`
}

func NewDefaultSearchOptions added in v0.6.0

func NewDefaultSearchOptions() *SearchOptions

type SessionOptFunc added in v0.2.0

type SessionOptFunc func(*SessionOptions)

func WithAuthHeader added in v0.2.0

func WithAuthHeader(authHeader string) SessionOptFunc

func WithCookieFile added in v0.2.0

func WithCookieFile(cookieFile string) SessionOptFunc

func WithDebugSession added in v0.2.0

func WithDebugSession(flag bool) SessionOptFunc

func WithEndpoint added in v0.2.0

func WithEndpoint(endpoint string) SessionOptFunc

func WithInsecureSession added in v0.2.0

func WithInsecureSession(flag bool) SessionOptFunc

type SessionOptions

type SessionOptions struct {
	Endpoint       string `json:"endpoint"`
	Insecure       bool   `json:"insecure"`
	ReAuthenticate bool   `json:"re-authenticate"`
	PersistCookie  bool   `json:"persist-cookie"`
	CookieFile     string `json:"cookie-file"`
	AuthHeader     string `json:"auth-header"`
	Compress       bool   `json:"compress"`
	Debug          bool   `json:"debug"`
}

func DefaultSessionOpts added in v0.2.0

func DefaultSessionOpts() *SessionOptions

func NewSessionOpts

func NewSessionOpts(opts *SessionOptions) *SessionOptions

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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