forked from skilld-labs/go-odoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodoo.go
357 lines (314 loc) · 9.55 KB
/
odoo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//Package odoo contains client code of library
//go:generate ./generator/generator -u $ODOO_ADMIN -p $ODOO_PASSWORD -d $ODOO_DATABASE --url $ODOO_URL -o $ODOO_REPO_PATH --models $ODOO_MODELS
package odoo
import (
"errors"
"github.com/kolo/xmlrpc"
)
var (
errClientConfigurationInvalid = errors.New("client configuration is invalid")
errClientNotAuthenticate = errors.New("client is not authenticate")
)
// ClientConfig is the configuration to create a new *Client by givin connection infomations.
type ClientConfig struct {
Database string
Admin string
Password string
URL string
}
func (c *ClientConfig) valid() bool {
return c.Database != "" &&
c.Admin != "" &&
c.Password != "" &&
c.URL != ""
}
// Client provides high and low level functions to interact with odoo
type Client struct {
common *xmlrpc.Client
object *xmlrpc.Client
cfg *ClientConfig
uid int64
auth bool
}
// NewClient creates a new *Client.
func NewClient(cfg *ClientConfig) (*Client, error) {
if !cfg.valid() {
return nil, errClientConfigurationInvalid
}
c := &Client{
cfg: cfg,
common: &xmlrpc.Client{},
object: &xmlrpc.Client{},
auth: false,
}
if err := c.authenticate(); err != nil {
return nil, err
}
return c, nil
}
// Close closes all opened client connections.
func (c *Client) Close() {
if c.common != nil {
c.common.Close()
}
if c.object != nil {
c.object.Close()
}
}
// Version get informations about your odoo instance version.
func (c *Client) Version() (Version, error) {
v := Version{}
reply, err := c.commonCall("version", nil)
if err != nil {
return Version{}, err
}
convertFromDynamicToStatic(reply, &v)
return v, nil
}
type criterion []interface{}
/*
Criteria is a set of criterion, each criterion is a triple (field_name, operator, value).
It allow you to search models.
see documentation: https://www.odoo.com/documentation/13.0/reference/orm.html#reference-orm-domains
*/
type Criteria []*criterion
// NewCriteria creates a new *Criteria.
func NewCriteria() *Criteria {
return &Criteria{}
}
func newCriterion(field, operator string, value interface{}) *criterion {
c := criterion(newTuple(field, operator, value))
return &c
}
// Add a new criterion to a *Criteria.
func (c *Criteria) Add(field, operator string, value interface{}) *Criteria {
*c = append(*c, newCriterion(field, operator, value))
return c
}
// Options allow you to filter search results.
type Options map[string]interface{}
// NewOptions creates a new *Options
func NewOptions() *Options {
opt := Options(make(map[string]interface{}))
return &opt
}
// Offset adds the offset options.
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#pagination
func (o *Options) Offset(offset int) *Options {
return o.Add("offset", offset)
}
// Limit adds the limit options.
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#pagination
func (o *Options) Limit(limit int) *Options {
return o.Add("limit", limit)
}
// FetchFields allow you to precise the model fields you want odoo to return.
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#search-and-read
func (o *Options) FetchFields(fields ...string) *Options {
ff := []string{}
for _, f := range fields {
ff = append(ff, f)
}
return o.Add("fields", ff)
}
// AllFields is useful for FieldsGet function. It represents the fields to document
// you want odoo to return.
// https://www.odoo.com/documentation/13.0/reference/orm.html#fields-views
func (o *Options) AllFields(fields ...string) *Options {
ff := []string{}
for _, f := range fields {
ff = append(ff, f)
}
return o.Add("allfields", ff)
}
// Attributes is useful for FieldsGet function. It represents the attributes to document
// you want odoo to return.
// https://www.odoo.com/documentation/13.0/reference/orm.html#fields-views
func (o *Options) Attributes(attributes ...string) *Options {
aa := []string{}
for _, a := range attributes {
aa = append(aa, a)
}
return o.Add("attributes", aa)
}
// Add on option by providing option name and value.
func (o *Options) Add(opt string, v interface{}) *Options {
(*o)[opt] = v
return o
}
func getValuesFromInterface(v interface{}) map[string]interface{} {
switch sv := v.(type) {
case map[string]interface{}:
return sv
default:
return convertFromStaticToDynamic(sv)
}
}
// Create a new model.
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#create-records
func (c *Client) Create(model string, values interface{}) (int64, error) {
v := getValuesFromInterface(values)
resp, err := c.ExecuteKw("create", model, []interface{}{v}, nil)
if err != nil {
return -1, err
}
return resp.(int64), nil
}
// Update existing model row(s).
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#update-records
func (c *Client) Update(model string, ids []int64, values interface{}) error {
v := getValuesFromInterface(values)
_, err := c.ExecuteKw("write", model, []interface{}{ids, v}, nil)
if err != nil {
return err
}
return nil
}
// Delete existing model row(s).
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#delete-records
func (c *Client) Delete(model string, ids []int64) error {
_, err := c.ExecuteKw("unlink", model, []interface{}{ids}, nil)
if err != nil {
return err
}
return nil
}
// SearchRead search model records matching with *Criteria and read it.
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#search-and-read
func (c *Client) SearchRead(model string, criteria *Criteria, options *Options, elem interface{}) error {
resp, err := c.ExecuteKw("search_read", model, argsFromCriteria(criteria), options)
if err != nil {
return err
}
if err := convertFromDynamicToStatic(resp, elem); err != nil {
return err
}
return nil
}
// Read model records matching with ids.
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#read-records
func (c *Client) Read(model string, ids []int64, options *Options, elem interface{}) error {
resp, err := c.ExecuteKw("read", model, []interface{}{ids}, options)
if err != nil {
return err
}
if err := convertFromDynamicToStatic(resp, elem); err != nil {
return err
}
return nil
}
// Count model records matching with *Criteria.
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#count-records
func (c *Client) Count(model string, criteria *Criteria, options *Options) (int64, error) {
resp, err := c.ExecuteKw("search_count", model, argsFromCriteria(criteria), options)
if err != nil {
return -1, err
}
return resp.(int64), nil
}
// Search model record ids matching with *Criteria.
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#list-records
func (c *Client) Search(model string, criteria *Criteria, options *Options) ([]int64, error) {
resp, err := c.ExecuteKw("search", model, argsFromCriteria(criteria), options)
if err != nil {
return []int64{}, err
}
return sliceInterfaceToInt64Slice(resp.([]interface{})), nil
}
// FieldsGet inspect model fields.
// https://www.odoo.com/documentation/13.0/webservices/odoo.html#listing-record-fields
func (c *Client) FieldsGet(model string, options *Options) (map[string]interface{}, error) {
resp, err := c.ExecuteKw("fields_get", model, nil, options)
if err != nil {
return nil, nil
}
return resp.(map[string]interface{}), nil
}
// ExecuteKw is a RPC function. The lowest library function. It is use for all
// function related to "xmlrpc/2/object" endpoint.
func (c *Client) ExecuteKw(method, model string, args []interface{}, options *Options) (interface{}, error) {
if err := c.checkForAuthentication(); err != nil {
return nil, err
}
resp, err := c.objectCall("execute_kw", []interface{}{c.cfg.Database, c.uid, c.cfg.Password, model, method, args, options})
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) authenticate() error {
if !c.isAuthenticate() {
resp, err := c.commonCall("authenticate", []interface{}{c.cfg.Database, c.cfg.Admin, c.cfg.Password, ""})
if err != nil {
return err
}
c.uid = resp.(int64)
c.auth = true
}
return nil
}
func (c *Client) commonCall(serviceMethod string, args interface{}) (interface{}, error) {
if err := c.loadCommonClient(); err != nil {
return nil, err
}
resp, err := c.call(c.common, serviceMethod, args)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) objectCall(serviceMethod string, args interface{}) (interface{}, error) {
if err := c.loadObjectClient(); err != nil {
return nil, err
}
resp, err := c.call(c.object, serviceMethod, args)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) call(x *xmlrpc.Client, serviceMethod string, args interface{}) (interface{}, error) {
var reply interface{}
if err := x.Call(serviceMethod, args, &reply); err != nil {
return nil, err
}
return reply, nil
}
func (c *Client) loadCommonClient() error {
return c.loadXmlrpcClient(c.common, "/xmlrpc/2/common")
}
func (c *Client) loadObjectClient() error {
return c.loadXmlrpcClient(c.object, "/xmlrpc/2/object")
}
func (c *Client) loadXmlrpcClient(x *xmlrpc.Client, path string) error {
if x.Client == nil {
newClient, err := xmlrpc.NewClient(c.cfg.URL+path, nil)
if err != nil {
return err
}
*x = *newClient
}
return nil
}
func (c *Client) checkForAuthentication() error {
if !c.isAuthenticate() {
return errClientNotAuthenticate
}
return nil
}
func (c *Client) isAuthenticate() bool {
return c.auth
}
func newTuple(values ...interface{}) []interface{} {
t := make([]interface{}, len(values))
for i, v := range values {
t[i] = v
}
return t
}
func argsFromCriteria(c *Criteria) []interface{} {
if c != nil {
return []interface{}{*c}
}
return []interface{}{}
}