odoo

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2022 License: BSD-3-Clause Imports: 14 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".

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 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 (Date) IsWithinMonth added in v0.2.0

func (d Date) IsWithinMonth(year, month int) bool

func (*Date) IsZero added in v0.4.0

func (d *Date) IsZero() bool

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

func (Date) MarshalJSON added in v0.2.0

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

func (*Date) String added in v0.2.0

func (d *Date) String() string

func (Date) ToTime added in v0.2.0

func (d Date) ToTime() time.Time

func (*Date) UnmarshalJSON added in v0.2.0

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

func (Date) WithLocation added in v0.5.2

func (d Date) WithLocation(loc *time.Location) Date

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

type LeaveType struct {
	ID   float64
	Name string
}

LeaveType describes the "leave type" from Odoo.

Example raw values returned from Odoo:

  • `false` (if no specific reason given)
  • `[4, "Unpaid"]`
  • `[5, "Military Service"]`
  • `[7, "Special Occasions"]`
  • `[9, "Public Holiday"]`
  • `[16, "Legal Leaves 2020"]`
  • `[17, "Legal Leaves 2021"]`

func (LeaveType) MarshalJSON added in v0.2.0

func (leaveType LeaveType) MarshalJSON() ([]byte, error)

func (*LeaveType) String added in v0.2.0

func (leaveType *LeaveType) String() string

func (*LeaveType) UnmarshalJSON added in v0.2.0

func (leaveType *LeaveType) UnmarshalJSON(b []byte) error

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