odoo

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DateFormat     = "2006-01-02"
	TimeFormat     = "15:04:05"
	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 interface{}) error

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

func IsWithinTimeRange added in v1.5.0

func IsWithinTimeRange(date, from, to time.Time) bool

IsWithinTimeRange returns true if the date is between the given times. The date is considered in the range if from and to equal to the date respectively.

func LocalizeTime added in v1.6.0

func LocalizeTime(tm time.Time, loc *time.Location) time.Time

LocalizeTime returns the same time but with a different location. As opposed to time.In(loc), the returned time is not just updated with the location.

func Midnight added in v1.6.2

func Midnight(tm time.Time) time.Time

Midnight returns a new time object in midnight (most recently past).

func ParseDate added in v1.5.0

func ParseDate(value string) (time.Time, error)

ParseDate parses the given value in DateFormat in UTC.

func ParseDateTime added in v1.5.0

func ParseDateTime(value string) (time.Time, error)

ParseDateTime parses the given value in DateTimeFormat in UTC.

Types

type Client

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

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

func NewClient

func NewClient(baseURL string, options ClientOptions) (*Client, error)

NewClient returns a new Client.

func (Client) Login

func (c Client) Login(ctx context.Context, options LoginOptions) (*Session, error)

Login tries to authenticate the user against Odoo. It returns a session if authentication was successful. An error is returned if

  • the credentials were wrong,
  • encoding or sending the request,
  • or decoding the request failed.

type ClientOptions added in v0.8.0

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 logr.FromContextOrDiscard.
	// 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 added in v0.2.0

type Date struct {
	time.Time
}

Date is an Odoo-specific format of a timestamp

func MustParseDate added in v0.8.0

func MustParseDate(value string) Date

MustParseDate parses the given value in DateFormat or panics if it fails.

func MustParseDateTime added in v0.8.0

func MustParseDateTime(value string) Date

MustParseDateTime parses the given value in DateTimeFormat or panics if it fails.

func NewDate added in v1.5.0

func NewDate(year int, month time.Month, day, hour, minute, second int, loc *time.Location) Date

NewDate returns a new Date.

func (*Date) MarshalJSON added in v0.2.0

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

func (*Date) UnmarshalJSON added in v0.2.0

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

type Filter

type Filter interface{}

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

type JSONRPCError added in v0.8.0

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

JSONRPCError holds error information.

type JSONRPCRequest added in v0.8.0

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 interface{} `json:"params,omitempty"`
}

JSONRPCRequest represents a generic json-rpc request

func NewJSONRPCRequest added in v0.8.0

func NewJSONRPCRequest(params interface{}) *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 added in v0.8.0

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

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

type JSONRPCResponse added in v0.8.0

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 List added in v0.11.0

type List[T any] struct {
	Items []T `json:"records,omitempty"`
}

List contains a slice of T.

func (*List[T]) Len added in v0.11.0

func (l *List[T]) Len() int

Len returns the length of List.Items. This is a pure utility/shortcut function for `len(l.Items)` with nil check.

type LoginOptions added in v0.8.0

type LoginOptions struct {
	DatabaseName string `json:"db,omitempty"`
	Username     string `json:"login,omitempty"`
	Password     string `json:"password,omitempty"`
}

LoginOptions contains all necessary authentication parameters.

type Method added in v0.8.0

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 added in v0.8.0

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 interface{}) error
	// CreateGenericModel accepts a payload and executes a query to create the new data record.
	CreateGenericModel(ctx context.Context, model string, data interface{}) (int, error)
	// UpdateGenericModel accepts a payload and executes a query to update an existing data record.
	UpdateGenericModel(ctx context.Context, model string, id int, data interface{}) 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 interface{}, into interface{}) error
}

QueryExecutor runs queries against Odoo API.

type SearchReadModel added in v0.8.0

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 added in v0.8.0

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

Open returns a new Session by trying to log in. 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 RestoreSession added in v0.8.0

func RestoreSession(client *Client, sessionID string, userID int) *Session

RestoreSession restores a Session based on existing Session.SessionID and Session.UID. It's not validated if the session is valid.

func (*Session) CreateGenericModel added in v0.8.0

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

CreateGenericModel implements QueryExecutor.

func (*Session) DeleteGenericModel added in v0.8.0

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

DeleteGenericModel implements QueryExecutor.

func (*Session) ExecuteQuery added in v0.8.0

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

ExecuteQuery implements QueryExecutor.

func (*Session) SearchGenericModel added in v0.8.0

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

SearchGenericModel implements QueryExecutor.

func (*Session) UpdateGenericModel added in v0.8.0

func (s *Session) UpdateGenericModel(ctx context.Context, model string, id int, data interface{}) error

UpdateGenericModel implements QueryExecutor.

type TimeZone added in v1.3.0

type TimeZone struct {
	*time.Location
}

TimeZone represents a time zone in Odoo.

func NewTimeZone added in v1.5.0

func NewTimeZone(loc *time.Location) *TimeZone

func (*TimeZone) IsEmpty added in v1.3.0

func (tz *TimeZone) IsEmpty() bool

IsEmpty returns true if the location is nil.

func (*TimeZone) LocationOrDefault added in v1.3.0

func (tz *TimeZone) LocationOrDefault(def *time.Location) *time.Location

LocationOrDefault returns the location if it's defined, or given default it not.

func (*TimeZone) MarshalJSON added in v1.4.0

func (tz *TimeZone) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*TimeZone) UnmarshalJSON added in v1.3.0

func (tz *TimeZone) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type WriteModel added in v0.8.0

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 []interface{} `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]interface{}{}`.
	KWArgs map[string]interface{} `json:"kwargs"`
}

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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