godooj

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2020 License: MIT Imports: 11 Imported by: 0

README

godooj

Golang library to connect to Odoo via JSON-RPC

Install

go get github.com/ausuwardi/godooj

Use

Create $HOME/.odoo.toml

[servers.testing]
server = "http://localhost:8069"
database = "demo"
user = "admin"
password = "admin"

[servers.production]
server = "https://demo.odoo.com"
database = "demo"
user = "admin"
password = "secret"

Then in your go code, use the library to connect:

package main

import (
    "log"

    odoo "github.com/ausuwardi/godooj"
)

func main() {
    client, err := odoo.ClientConnect("testing")
    if err != nil {
        log.Error("Error connecting to Odoo")
        return
    }

    records, err := client.SearchRead(
        "product.product",
        odoo.List{
            odoo.List{"active", "=", true},
            odoo.List{"name", "like", "apple"},
        },
        []string{"id", "name", "price"},
    )
    if err != nil {
        log.Error("Error retrieving data")
        return
    }

    for _, rec := range records {
        id, _ := odoo.IntField(rec, "id")
        name, _ := odoo.StringField(rec, "name")
        price, _ := odoo.FloatField(rec, "price")

        log.Infof("Product %d - %s - %f", id, name, price)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FloatField

func FloatField(rec interface{}, name string) (float64, error)

FloatField gets float64 value from a record

func IntField

func IntField(rec interface{}, name string) (int, error)

IntField gets int value from a record

func One2manyField

func One2manyField(rec interface{}, name string) ([]int, error)

One2manyField gets one2many []int from a record

func StringField

func StringField(rec interface{}, name string) (string, error)

StringField gets int value from a record

func Val2Float

func Val2Float(f interface{}) (float64, bool)

Val2Float converts a field value to float64

func Val2Int

func Val2Int(f interface{}) (int, bool)

Val2Int converts a field value to int

func Val2One2many

func Val2One2many(f interface{}) []int

Val2One2many converts field value to []int

func Val2String

func Val2String(f interface{}) (string, bool)

Val2String converts a field value to string

Types

type AuthError

type AuthError struct {
	Code    int            `json:"code,omitempty"`
	Message string         `json:"message,omitempty"`
	Data    *AuthErrorData `json:"data,omitempty"`
}

AuthError struct holds data for an authentication error response

type AuthErrorData

type AuthErrorData struct {
	Name          OString       `json:"name,omitempty"`
	Debug         OString       `json:"debug,omitempty"`
	Message       OString       `json:"message,omitempty"`
	Arguments     []interface{} `json:"arguments,omitempty"`
	ExceptionType OString       `json:"exception_type,omitempty"`
}

AuthErrorData struct holds data from an authentication error response

type AuthParams

type AuthParams struct {
	DB       string `json:"db"`
	Login    string `json:"login"`
	Password string `json:"password"`
}

AuthParams struct holds authentication parameters required for an auth request

type AuthRequest

type AuthRequest struct {
	ID      string     `json:"id"`
	JSONRPC string     `json:"jsonrpc"`
	Method  string     `json:"method"`
	Params  AuthParams `json:"params"`
}

AuthRequest struct used to hold data for an authentication request

type AuthResponse

type AuthResponse struct {
	ID      OString     `json:"id"`
	JSONRPC OString     `json:"jsonrpc"`
	Result  *AuthResult `json:"result"`
	Error   *AuthError  `json:"error"`
}

AuthResponse struct holds response data returned from Odoo server

type AuthResult

type AuthResult struct {
	UID                OInt                   `json:"uid"`
	IsSystem           bool                   `json:"is_system"`
	IsAdmin            bool                   `json:"is_admin"`
	UserContext        map[string]interface{} `json:"user_context"`
	DB                 OString                `json:"db"`
	ServerVersion      OString                `json:"server_version"`
	ServerVersionInfo  []interface{}          `json:"server_version_info"`
	Name               OString                `json:"name"`
	UserName           OString                `json:"username"`
	PartnerDisplayName OString                `json:"partner_display_name"`
	CompanyID          OInt                   `json:"company_id"`
	PartnerID          OInt                   `json:"partner_id"`
	WebBaseURL         OString                `json:"web.base.url"`
}

AuthResult struct hold authentication data returned after successfull login

type Client

type Client struct {
	Auth    *AuthResponse
	Context map[string]interface{}
	// contains filtered or unexported fields
}

Client holds client connection and the server metadata

func ClientConnect

func ClientConnect(serverName string) (*Client, error)

ClientConnect creates a new client from a named server configuration

func Connect

func Connect(baseURL, db, login, password string) (*Client, error)

Connect attempts to connect to Odoo server and returns the client or error

func (*Client) Call

func (client *Client) Call(model, method string, args []interface{}, kwargs map[string]interface{}) (interface{}, error)

Call calls remote Odoo method

func (*Client) Create

func (client *Client) Create(model string, values map[string]interface{}) (int, error)

Create creates a new record and returns the new record ID or error

func (*Client) Delete

func (client *Client) Delete(model string, ids []int) (bool, error)

Delete deletes records

func (*Client) GetBaseURL

func (client *Client) GetBaseURL() string

GetBaseURL gets client's connection base URL

func (*Client) GetServerVersion

func (client *Client) GetServerVersion() string

GetServerVersion gets Odoo server's version

func (*Client) IsValid

func (client *Client) IsValid() bool

IsValid checks if a Client is in a valid state

func (*Client) Read

func (client *Client) Read(model string, ids []int, fields []string) ([]interface{}, error)

Read reads records for the specified model with specified IDs The read will include only specified fields, or if empty will return all fields. Beware that retrieving all fields will normally take longer time than selective fields, depending on the model's schema complexity

func (*Client) Search

func (client *Client) Search(model string, domain []interface{}) ([]int, error)

Search searches records in the specified model, matching the criteria specified in domain, which is similar to domain in Odoo python syntax

func (*Client) SearchRead

func (client *Client) SearchRead(model string, domain []interface{}, fields []string) ([]interface{}, error)

SearchRead is a convenience method that search and read in one go

func (*Client) WithContext

func (client *Client) WithContext(ctx map[string]interface{}) *Client

WithContext returns a new Client with added context

func (*Client) Write

func (client *Client) Write(model string, ids []int, values map[string]interface{}) (bool, error)

Write updates records

type List

type List []interface{}

List is a shortcut to []interface{}

type Many2oneFK

type Many2oneFK struct {
	ID   int
	Name string
}

Many2oneFK struct holds data for a Many2one field returned from Odoo

func Many2oneField

func Many2oneField(rec interface{}, name string) (*Many2oneFK, error)

Many2oneField gets Many2one struct from a record

func Val2Many2one

func Val2Many2one(f interface{}) *Many2oneFK

Val2Many2one converts field value to Many2oneFK

type ODateTime

type ODateTime time.Time

ODateTime is Odoo datetime

func (*ODateTime) UnmarshalJSON

func (t *ODateTime) UnmarshalJSON(b []byte) error

UnmarshalJSON parses ODateTime from JSON

type OFloat

type OFloat float64

OFloat is Odoo Float

func (*OFloat) UnmarshalJSON

func (f *OFloat) UnmarshalJSON(b []byte) error

UnmarshalJSON parses float value from JSON

type OInt

type OInt int

OInt is Odoo Int

func (*OInt) UnmarshalJSON

func (i *OInt) UnmarshalJSON(b []byte) error

UnmarshalJSON parses Int value from JSON

type OMany2one

type OMany2one struct {
	ID   int
	Name string
}

OMany2one is a struct that hold Many2one value from Odoo

func (*OMany2one) UnmarshalJSON

func (m *OMany2one) UnmarshalJSON(b []byte) error

UnmarshalJSON parses Many2one struct from JSON

type OString

type OString string

OString os Odoo String

func (*OString) UnmarshalJSON

func (s *OString) UnmarshalJSON(b []byte) error

UnmarshalJSON parses OString from JSON

type OdooConfig

type OdooConfig struct {
	ServerAddress string
	Database      string
	Login         string
	Password      string
}

OdooConfig holds Odoo connection configuration data

func LoadOdooConfig

func LoadOdooConfig(serverName string) (*OdooConfig, error)

LoadOdooConfig loads odoo server configuration from file

func (*OdooConfig) CreateOdooClient

func (conf *OdooConfig) CreateOdooClient() (*Client, error)

CreateOdooClient creates Odoo client based from a configuration

type RPCError

type RPCError struct {
	Code    OInt         `json:"code"`
	Message OString      `json:"message"`
	Data    RPCErrorData `json:"data"`
}

RPCError struct holds data related to RPC error

type RPCErrorData

type RPCErrorData struct {
	Name          OString       `json:"name"`
	Debug         OString       `json:"debug"`
	Message       OString       `json:"message"`
	Arguments     []interface{} `json:"arguments"`
	ExceptionType OString       `json:"exception_type"`
}

RPCErrorData struct holds error data from a request error

type RPCResponse

type RPCResponse struct {
	ID      OString     `json:"id"`
	JSONRPC OString     `json:"jsonrpc"`
	Result  interface{} `json:"result"`
	Error   *RPCError   `json:"error"`
}

RPCResponse struct holds data returned from server on a successfull request

Jump to

Keyboard shortcuts

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