client

package
v0.33.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// DateFormat only yields year, month, day.
	DateFormat = "2006-01-02"
	// TimeFormat only yields hour, minute, seconds in 24-h format.
	TimeFormat = "15:04:05"
	// DateTimeFormat combines DateFormat with TimeFormat separated by space.
	DateTimeFormat = DateFormat + " " + TimeFormat
)

Variables

View Source
var (
	// ErrInvalidCredentials is an error that indicates an authentication error due to missing or invalid credentials.
	ErrInvalidCredentials = errors.New("invalid credentials")
)

Functions

func DecodeResult

func DecodeResult(buf io.Reader, result any) error

DecodeResult takes a buffer, decodes the intermediate JSONRPCResponse and then the contained "result" field into "result".

Types

type Client

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

Client is the base struct that holds information required to talk to Odoo

type ClientOptions

type ClientOptions struct {
	// UseDebugLogger sets the http.Transport field of the internal http client with a transport implementation that logs the raw contents of requests and responses.
	// The logger is retrieved from the request's context via klog.FromContext.
	// The log level used is '2'.
	// Any "password":"..." byte content is replaced with a placeholder to avoid leaking credentials.
	// Still, this should not be called in production as other sensitive information might be leaked.
	// This method is meant to be called before any requests are made (for example after setting up the Client).
	UseDebugLogger bool
}

ClientOptions configures the Odoo client.

type Date

type Date time.Time

Date is an Odoo-specific format of a timestamp

func (*Date) IsZero

func (d *Date) IsZero() bool

IsZero returns true if Date is nil or Time.IsZero()

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Date) String

func (d *Date) String() string

String formats date using DateTimeFormat.

func (Date) ToTime

func (d Date) ToTime() time.Time

ToTime returns a time.Time representation.

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Filter

type Filter any

Filter to use in queries, usually in the format of [predicate, operator, value], eg ["employee_id.user_id.id", "=", 123]

type JSONRPCError

type JSONRPCError struct {
	Message string         `json:"message,omitempty"`
	Code    int            `json:"code,omitempty"`
	Data    map[string]any `json:"data,omitempty"`
}

JSONRPCError holds error information.

type JSONRPCRequest

type JSONRPCRequest struct {
	// ID should be a randomly generated value, either as a string or int.
	// The server will return this value in the response.
	ID string `json:"id,omitempty"`

	// JSONRPC is always set to "2.0"
	JSONRPC string `json:"jsonrpc,omitempty"`

	// Method to call, usually just "call"
	Method string `json:"method,omitempty"`

	// Params includes the actual request payload.
	Params any `json:"params,omitempty"`
}

JSONRPCRequest represents a generic json-rpc request

func NewJSONRPCRequest

func NewJSONRPCRequest(params any) *JSONRPCRequest

NewJSONRPCRequest returns a JSON RPC request with its protocol fields populated:

* "id" will be set to a random UUID * "jsonrpc" will be set to "2.0" * "method" will be set to "call" * "params" will be set to whatever was passed in

func (*JSONRPCRequest) Encode

func (r *JSONRPCRequest) Encode() (io.Reader, error)

Encode encodes the request as JSON in a buffer and returns the buffer.

type JSONRPCResponse

type JSONRPCResponse struct {
	// ID that was sent with the request
	ID string `json:"id,omitempty"`
	// JSONRPC is always set to "2.0"
	JSONRPC string `json:"jsonrpc,omitempty"`
	// Result payload
	Result *json.RawMessage `json:"result,omitempty"`

	// Optional error field
	Error *JSONRPCError `json:"error,omitempty"`
}

JSONRPCResponse holds the JSONRPC response.

type Method

type Method string

Method identifies the type of write operation.

const (
	// MethodWrite is used to update existing records.
	MethodWrite Method = "write"
	// MethodCreate is used to create new records.
	MethodCreate Method = "create"
	// MethodRead is used to read records.
	MethodRead Method = "read"
	// MethodDelete is used to delete existing records.
	MethodDelete Method = "unlink"
)

type QueryExecutor

type QueryExecutor interface {
	// SearchGenericModel accepts a SearchReadModel and unmarshal the response into the given pointer.
	// Depending on the JSON fields returned a custom json.Unmarshaler needs to be written since Odoo sets undefined fields to `false` instead of null.
	SearchGenericModel(ctx context.Context, model SearchReadModel, into any) error
	// CreateGenericModel accepts a payload and executes a query to create the new data record.
	CreateGenericModel(ctx context.Context, model string, data any) (int, error)
	// UpdateGenericModel accepts a payload and executes a query to update an existing data record.
	UpdateGenericModel(ctx context.Context, model string, ids []int, data any) error
	// DeleteGenericModel accepts a model identifier and data records IDs as payload and executes a query to delete multiple existing data records.
	// At least one ID is required.
	DeleteGenericModel(ctx context.Context, model string, ids []int) error
	// ExecuteQuery runs a generic JSONRPC query with the given model as payload and deserializes the response.
	ExecuteQuery(ctx context.Context, path string, model any, into any) error
}

QueryExecutor runs queries against Odoo API.

type SearchReadModel

type SearchReadModel struct {
	Model  string   `json:"model,omitempty"`
	Domain []Filter `json:"domain,omitempty"`
	Fields []string `json:"fields,omitempty"`
	Limit  int      `json:"limit,omitempty"`
	Offset int      `json:"offset,omitempty"`
}

SearchReadModel is used as "params" in requests to "dataset/search_read" endpoints.

type Session

type Session struct {
	// SessionID is the session SessionID.
	// Is always set, no matter the authentication outcome.
	SessionID string `json:"session_id,omitempty"`
	// UID is the user's ID as an int, or the boolean `false` if authentication failed.
	UID int `json:"uid,omitempty"`
	// contains filtered or unexported fields
}

Session information

func Open

func Open(ctx context.Context, baseURL string, options ClientOptions) (*Session, error)

Open returns a new client and tries to log in to create a session. The URL must be in the format of `https://user:pass@host[:port]/db-name`. It returns error if baseURL is not parseable with url.Parse or if the login failed.

func (*Session) CreateGenericModel

func (s *Session) CreateGenericModel(ctx context.Context, model string, data any) (int, error)

CreateGenericModel implements QueryExecutor.

func (*Session) DeleteGenericModel

func (s *Session) DeleteGenericModel(ctx context.Context, model string, ids []int) error

DeleteGenericModel implements QueryExecutor.

func (*Session) ExecuteQuery

func (s *Session) ExecuteQuery(ctx context.Context, path string, model any, into any) error

ExecuteQuery implements QueryExecutor.

func (*Session) SearchGenericModel

func (s *Session) SearchGenericModel(ctx context.Context, model SearchReadModel, into any) error

SearchGenericModel implements QueryExecutor.

func (*Session) UpdateGenericModel

func (s *Session) UpdateGenericModel(ctx context.Context, model string, ids []int, data any) error

UpdateGenericModel implements QueryExecutor.

type WriteModel

type WriteModel struct {
	Model  string `json:"model"`
	Method Method `json:"method"`
	// Args contains the record to create or update.
	// If Method is MethodCreate, then the slice may contain a single entity without an ID parameter.
	// Example:
	//  Args[0] = {Name: "New Name"}
	// If Method is MethodWrite, then the first item has to be an array of the numeric ID of the existing record.
	// Example:
	//  Args[0] = [221]
	//  Args[1] = {Name: "Updated Name"}
	Args []any `json:"args"`
	// KWArgs is an additional object required to be non-nil, otherwise the request simply fails.
	// In most cases it's enough to set it to `map[string]any{}`.
	KWArgs map[string]any `json:"kwargs"`
}

WriteModel is used as "params" in requests to "dataset/create", "dataset/write" or "dataset/unlink" endpoints.

Directories

Path Synopsis
Code generated by MockGen.
Code generated by MockGen.

Jump to

Keyboard shortcuts

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