api_client_go

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

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
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 BuildDirectoryStructureIfNotExists

func BuildDirectoryStructureIfNotExists(path string) error

func FileExists

func FileExists(filePath string) bool

func FileExistsAndNotEmpty

func FileExistsAndNotEmpty(filePath string) bool

func ReAuthenticateSession added in v0.2.0

func ReAuthenticateSession(opts *SessionOptions)

func ReadBytesFromFile added in v0.5.0

func ReadBytesFromFile(path string) ([]byte, error)

func ReadJSONFromFile added in v0.5.0

func ReadJSONFromFile(path string, data interface{}) error

func WithOutCompressSession added in v0.2.0

func WithOutCompressSession(opts *SessionOptions)

func WithoutPersistCookie added in v0.2.0

func WithoutPersistCookie(opts *SessionOptions)

func WriteBytesToFile added in v0.5.0

func WriteBytesToFile(b []byte, path string) error

func WriteIndentedJSONToFile added in v0.5.0

func WriteIndentedJSONToFile(data interface{}, path string) error

Types

type ClientOpts added in v0.2.0

type ClientOpts struct {
	*SessionOptions

	Credentials types.LogInParams
}

func NewClientOpts added in v0.2.0

func NewClientOpts(credentials types.LogInParams, opts ...SessionOptFunc) *ClientOpts

type NuvlaClient

type NuvlaClient struct {
	// Session params
	*NuvlaSession
	// contains filtered or unexported fields
}

func NewNuvlaClient

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

func NewNuvlaClientFromOpts

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

func (*NuvlaClient) Delete

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

func (*NuvlaClient) Edit

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

func (*NuvlaClient) Get

func (nc *NuvlaClient) Get(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(resourceId, operation string, data map[string]interface{}) (*http.Response, error)

func (*NuvlaClient) Post

func (nc *NuvlaClient) Post(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(uri string, data map[string]interface{}, selectFields []string) (*http.Response, 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(reqInput *types.RequestOpts) (*http.Response, error)

func (*NuvlaSession) String

func (s *NuvlaSession) String() string

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