cinp

package module
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: Apache-2.0 Imports: 13 Imported by: 3

README

CInP client for Go
==================

Install
-------

::

  go get github.com/cinp/go


Usage Example
-------------

::
  package main

  import (
      "fmt"

      cinp "github.com/cinp/go"
  )

  func main() {
    host := "http://localhost"
    proxy := nil
    expedtedAPIVersion := "0.1"
    username := "bob"
    password := "supersecret"

    client, err = cinp.NewCInP(host, "/api/v1/", proxy)
    if err != nil {
        return nil, err
    }

    APIVersion, err := getAPIVersion("/api/v1/")
    if err != nil {
        return nil, err
    }

    if APIVersion != expedtedAPIVersion {
        return nil, fmt.Errorf("API version mismatch.  Got '%s', expected '%s'", APIVersion, expedtedAPIVersion)
    }

    args := map[string]interface{}{
            "username": username,
            "password": password,
    }
    result := ""
    err := client.call("/api/v1/Auth/Auth(login)", &args, &result)
    if err != nil {
        return nil, err
    }

    client.setHeader("Auth-Id", username)
    client.setHeader("Auth-Token", result)

    # do stuff like
    #client.get("/api/")

    args = map[string]interface{}{}
    result = ""
    err := client.call("/api/v1/Auth/Auth(logout)", &args, &result)
    if err != nil {
        return nil, err
    }
  }

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MappedObjectType = reflect.TypeOf((*MappedObject)(nil)).Elem()

MappedObjectType is the type used for the MappedObject which is used if a uri is not found in the type table

Functions

func BoolAddr added in v1.2.4

func BoolAddr(v bool) *bool

func IntAddr added in v1.2.4

func IntAddr(v int) *int

func StringAddr added in v1.2.4

func StringAddr(v string) *string

convenience functions

Types

type BaseObject

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

BaseObject is

func (*BaseObject) GetURI added in v1.2.0

func (baseobject *BaseObject) GetURI() string

GetURI return the URI of the object

func (*BaseObject) SetURI added in v1.2.0

func (baseobject *BaseObject) SetURI(uri string)

SetURI sets the URI of the object

type CInP

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

CInP client struct

func NewCInP

func NewCInP(log *slog.Logger, host string, rootPath string, proxy string) (*CInP, error)

NewCInP creates a new cinp instance

func (*CInP) Call

func (cinp *CInP) Call(ctx context.Context, uri string, args *map[string]interface{}, result interface{}) error

Call calls an object/class method from the URI, if the Multi-Object header is set on the result, this will error out

func (*CInP) CallMulti

func (cinp *CInP) CallMulti(ctx context.Context, uri string, args *map[string]interface{}) (*map[string]map[string]interface{}, error)

CallMulti calls an object/class method from the URI, forces the Muti-Object header

func (*CInP) ClearHeader added in v1.1.5

func (cinp *CInP) ClearHeader(name string)

ClearHeader sets a request header

func (*CInP) Create

func (cinp *CInP) Create(ctx context.Context, uri string, object Object) (*Object, error)

Create an object with the values

func (*CInP) Delete

func (cinp *CInP) Delete(ctx context.Context, object Object) error

Delete the object

func (*CInP) DeleteURI

func (cinp *CInP) DeleteURI(ctx context.Context, uri string) error

DeleteURI the object(s) from the URI

func (*CInP) Describe

func (cinp *CInP) Describe(ctx context.Context, uri string) (*Describe, string, error)

Describe the URI

func (*CInP) Get

func (cinp *CInP) Get(ctx context.Context, uri string) (*Object, error)

Get gets an object from the URI, if the Multi-Object header is set on the result, this will error out

func (*CInP) GetURI added in v1.2.4

func (cinp *CInP) GetURI() *URI

GetURI get the uri

func (*CInP) List

func (cinp *CInP) List(ctx context.Context, uri string, filterName string, filterValues map[string]interface{}, position int, count int) ([]string, int, int, int, error)

List objects

func (*CInP) ListIds

func (cinp *CInP) ListIds(ctx context.Context, uri string, filterName string, filterValues map[string]interface{}, chunkSize int) <-chan string

ListIds List Objects and return in a channel

func (*CInP) ListObjects

func (cinp *CInP) ListObjects(ctx context.Context, uri string, objectType reflect.Type, filterName string, filterValues map[string]interface{}, chunkSize int) <-chan *Object

ListObjects List Objects and return in a channel

func (*CInP) RegisterType

func (cinp *CInP) RegisterType(uri string, objectType reflect.Type)

RegisterType registeres the type to use for a url

func (*CInP) SetHeader added in v1.1.1

func (cinp *CInP) SetHeader(name string, value string)

SetHeader sets a request header

func (*CInP) Update

func (cinp *CInP) Update(ctx context.Context, object Object) (*Object, error)

Update sends the values of the object to be updated, if the Multi-Object header is set on the result, this will error out. NOTE: the updated values the server sends back will be pushed into the object

func (*CInP) UpdateMulti

func (cinp *CInP) UpdateMulti(ctx context.Context, uri string, values *map[string]interface{}, result *map[string]Object) error

UpdateMulti update the objects with the values, forces the Muti-Object header

type CInPClient added in v1.2.2

type CInPClient interface {
	SetHeader(name string, value string)
	ClearHeader(name string)
	RegisterType(uri string, objectType reflect.Type)
	Describe(ctx context.Context, uri string) (*Describe, string, error)
	List(ctx context.Context, uri string, filterName string, filterValues map[string]interface{}, position int, count int) ([]string, int, int, int, error)
	ListIds(ctx context.Context, uri string, filterName string, filterValues map[string]interface{}, chunkSize int) <-chan string
	ListObjects(ctx context.Context, uri string, objectType reflect.Type, filterName string, filterValues map[string]interface{}, chunkSize int) <-chan *Object
	Get(ctx context.Context, uri string) (*Object, error)
	Create(ctx context.Context, uri string, object Object) (*Object, error)
	Update(ctx context.Context, object Object) (*Object, error)
	UpdateMulti(ctx context.Context, uri string, values *map[string]interface{}, result *map[string]Object) error
	Delete(ctx context.Context, object Object) error
	DeleteURI(ctx context.Context, uri string) error
	Call(ctx context.Context, uri string, args *map[string]interface{}, result interface{}) error
	CallMulti(ctx context.Context, uri string, args *map[string]interface{}) (*map[string]map[string]interface{}, error)
	GetURI() *URI
}

type Describe

type Describe struct {
	Name string `json:"name"`
	Doc  string `json:"doc"`
	Path string `json:"path"`
	// Namespace
	APIVersion  string   `json:"api-version"`
	MultiURIMax int      `json:"multi-uri-max"`
	Namespaces  []string `json:"namespaces"`
	Models      []string `json:"models"`
	// Model
	Constants         map[string]string           `json:"constants"`
	Fields            []FieldParamater            `json:"fields"`
	Actions           []string                    `json:"actions"`
	NotAllowedMethods []string                    `json:"not-allowed-methods"`
	ListFilters       map[string][]FieldParamater `json:"list-filters"`
	// Actions
	ReturnType FieldParamater   `json:"return-type"`
	Static     bool             `json:"static"`
	Paramaters []FieldParamater `json:"paramaters"`
}

Describe struct definiation

type FieldParamater

type FieldParamater struct {
	Name           string        `json:"name"`
	Doc            string        `json:"doc"`
	Path           string        `json:"path"`
	Type           string        `json:"type"`
	Length         int           `json:"length"`
	URI            string        `json:"uri"`
	AllowedSchemes []string      `json:"allowed_schemes"`
	Choices        []interface{} `json:"choices"`
	IsArray        bool          `json:"is_array"`
	Default        interface{}   `json:"default"`
	Mode           string        `json:"mode"`
	Required       bool          `json:"required"`
}

FieldParamater defines a Field or Paramater from the describe

type InvalidRequest

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

InvalidRequest is a error that is returned when the request is Invalid, this needs to be updated to match the rust/python clients

func (*InvalidRequest) Error

func (e *InvalidRequest) Error() string

type InvalidSession

type InvalidSession struct{}

InvalidSession is a error that is returned when the AuthId and AuthToken do not specifiy a valid session

func (*InvalidSession) Error

func (e *InvalidSession) Error() string

type MappedObject

type MappedObject struct {
	BaseObject
	Data map[string]interface{}
}

MappedObject for generic Object Manipluation

func (*MappedObject) AsMap

func (mo *MappedObject) AsMap(isCreate bool) *map[string]interface{}

AsMap exports the Object's Data as a map

type NotAuthorized

type NotAuthorized struct{}

NotAuthorized is a error that is returned when the Session is not Authorized to make the request

func (*NotAuthorized) Error

func (e *NotAuthorized) Error() string

type NotFound

type NotFound struct{}

NotFound is a error that is returned when the request referes to a namespace/model/object/action that does not exist

func (*NotFound) Error

func (e *NotFound) Error() string

type Object

type Object interface {
	GetURI() string
	SetURI(string)
}

Object used for handeling objects

type ReaderForLogging added in v1.2.1

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

func NewReaderForLogging added in v1.2.1

func NewReaderForLogging(cap int) *ReaderForLogging

func (*ReaderForLogging) LogValue added in v1.2.1

func (r *ReaderForLogging) LogValue() []byte

func (*ReaderForLogging) Write added in v1.2.1

func (r *ReaderForLogging) Write(buff []byte) (int, error)

Write to buffer, if we are full ignore the more data

type ServerError

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

ServerError is a error that is returned when the request causes a ServerError

func (*ServerError) Error

func (e *ServerError) Error() string

type URI

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

URI is for parsing and checking CNiP URIs

func NewURI

func NewURI(rootPath string) (*URI, error)

NewURI creates and initilizes a URI instance

func (*URI) Build

func (u *URI) Build(namespace []string, model string, action string, ids []string) string

Build constructs a URI from the paramaters, NOTE: if model is "", ids and action are skiped

func (*URI) ExtractIds

func (u *URI) ExtractIds(uriList []string) ([]string, error)

ExtractIds extract the id(s) from a list of URI

func (*URI) Split

func (u *URI) Split(uri string) ([]string, string, string, []string, bool, error)

Split the uri into it's parts

func (*URI) UpdateIDs

func (u *URI) UpdateIDs(uri string, ids []string) (string, error)

UpdateIDs update/set the id(s) in the uri

Jump to

Keyboard shortcuts

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