-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathODXApiClient.cs
More file actions
174 lines (161 loc) · 6.43 KB
/
ODXApiClient.cs
File metadata and controls
174 lines (161 loc) · 6.43 KB
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
using ODXProxy.Client.Models;
namespace ODXProxy.Client;
/// <summary>
/// Provides high-level methods to interact with an Odoo instance via the ODX Proxy Gateway.
/// </summary>
public static class OdxApiClient
{
private static OdxProxyClient Client => OdxProxyClient.Instance;
/// <summary>
/// Initializes the underlying ODX Proxy Client. Must be called once before any other methods.
/// </summary>
public static void Init(ODXProxyClientInfo options) => OdxProxyClient.Init(options);
private static ODXClientKeywordRequest GetCleanKeyword(ODXClientKeywordRequest keyword) =>
keyword with { Order = null, Limit = null, Offset = null, Fields = null };
/// <summary>
/// Performs a search, returning only record IDs.
/// </summary>
public static Task<ODXServerResponse<int[]>> SearchAsync(string model, object[] domain, ODXClientKeywordRequest keyword, string? id = null, CancellationToken ct = default)
{
var request = new ODXClientRequest
{
Id = id ?? Ulid.NewUlid().ToString(),
Action = "search",
ModelId = model,
Keyword = GetCleanKeyword(keyword),
Params = domain,
OdooInstance = Client.OdooInstance
};
return Client.PostRequestAsync<int[]>(request, ct);
}
/// <summary>
/// Performs a search and reads the data of the found records.
/// </summary>
public static Task<ODXServerResponse<T[]>> SearchReadAsync<T>(string model, object[] domain, ODXClientKeywordRequest keyword, string? id = null, CancellationToken ct = default)
{
var request = new ODXClientRequest
{
Id = id ?? Ulid.NewUlid().ToString(),
Action = "search_read",
ModelId = model,
Keyword = keyword,
Params = domain,
OdooInstance = Client.OdooInstance
};
return Client.PostRequestAsync<T[]>(request, ct);
}
/// <summary>
/// Counts the number of records matching the search domain.
/// </summary>
public static Task<ODXServerResponse<int>> SearchCountAsync(string model, object[] domain, ODXClientKeywordRequest keyword, string? id = null, CancellationToken ct = default)
{
var request = new ODXClientRequest
{
Id = id ?? Ulid.NewUlid().ToString(),
Action = "search_count",
ModelId = model,
Keyword = GetCleanKeyword(keyword),
Params = domain,
OdooInstance = Client.OdooInstance
};
return Client.PostRequestAsync<int>(request, ct);
}
/// <summary>
/// Reads the data for a specific set of record IDs.
/// </summary>
public static Task<ODXServerResponse<T[]>> ReadAsync<T>(string model, int[] ids, ODXClientKeywordRequest keyword, string? id = null, CancellationToken ct = default)
{
var request = new ODXClientRequest
{
Id = id ?? Ulid.NewUlid().ToString(),
Action = "read",
ModelId = model,
Keyword = GetCleanKeyword(keyword),
Params = new object[] { ids },
OdooInstance = Client.OdooInstance
};
return Client.PostRequestAsync<T[]>(request, ct);
}
/// <summary>
/// Retrieves metadata for a model's fields.
/// </summary>
public static Task<ODXServerResponse<T>> FieldsGetAsync<T>(string model, ODXClientKeywordRequest keyword, string? id = null, CancellationToken ct = default)
{
var request = new ODXClientRequest
{
Id = id ?? Ulid.NewUlid().ToString(),
Action = "fields_get",
ModelId = model,
Keyword = GetCleanKeyword(keyword),
Params = Array.Empty<object>(),
OdooInstance = Client.OdooInstance
};
return Client.PostRequestAsync<T>(request, ct);
}
/// <summary>
/// Creates a new record.
/// </summary>
/// <returns>The ID of the newly created record.</returns>
public static Task<ODXServerResponse<int>> CreateAsync(string model, object values, ODXClientKeywordRequest keyword, string? id = null, CancellationToken ct = default)
{
var request = new ODXClientRequest
{
Id = id ?? Ulid.NewUlid().ToString(),
Action = "create",
ModelId = model,
Keyword = GetCleanKeyword(keyword),
Params = new object[] { values },
OdooInstance = Client.OdooInstance
};
return Client.PostRequestAsync<int>(request, ct);
}
/// <summary>
/// Updates existing records.
/// </summary>
public static Task<ODXServerResponse<bool>> WriteAsync(string model, int[] ids, object values, ODXClientKeywordRequest keyword, string? id = null, CancellationToken ct = default)
{
var request = new ODXClientRequest
{
Id = id ?? Ulid.NewUlid().ToString(),
Action = "write",
ModelId = model,
Keyword = GetCleanKeyword(keyword),
Params = new object[] { ids, values },
OdooInstance = Client.OdooInstance
};
return Client.PostRequestAsync<bool>(request, ct);
}
/// <summary>
/// Deletes (unlinks) records by their IDs.
/// </summary>
public static Task<ODXServerResponse<bool>> RemoveAsync(string model, int[] ids, ODXClientKeywordRequest keyword, string? id = null, CancellationToken ct = default)
{
var request = new ODXClientRequest
{
Id = id ?? Ulid.NewUlid().ToString(),
Action = "unlink",
ModelId = model,
Keyword = GetCleanKeyword(keyword),
Params = new object[] { ids },
OdooInstance = Client.OdooInstance
};
return Client.PostRequestAsync<bool>(request, ct);
}
/// <summary>
/// Calls an arbitrary method on a model.
/// </summary>
public static Task<ODXServerResponse<T>> CallMethodAsync<T>(string model, string methodName, object[] parameters, ODXClientKeywordRequest keyword, string? id = null, CancellationToken ct = default)
{
var request = new ODXClientRequest
{
Id = id ?? Ulid.NewUlid().ToString(),
Action = "call_method",
ModelId = model,
FunctionName = methodName,
Keyword = GetCleanKeyword(keyword),
Params = parameters,
OdooInstance = Client.OdooInstance
};
return Client.PostRequestAsync<T>(request, ct);
}
}