fix searchRead
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import '../odoo_rpc_helper.dart';
|
||||
|
||||
/// Klien JSON-RPC utk Odoo 17. Authenticate + execute_kw.
|
||||
class OdooClient {
|
||||
@@ -42,6 +44,68 @@ class OdooClient {
|
||||
return _call('object', 'execute_kw', params);
|
||||
}
|
||||
|
||||
Future<dynamic> executeKwdWithArgs(String model, String method,
|
||||
{List<dynamic> fields = const [], Map<String, dynamic>? kwargs}) async {
|
||||
_uid ??= await authenticate();
|
||||
final args = [
|
||||
|
||||
db,
|
||||
_uid,
|
||||
password,
|
||||
model,
|
||||
method,
|
||||
[],
|
||||
fields,
|
||||
|
||||
if (kwargs != null) kwargs,
|
||||
];
|
||||
return _call('object', 'execute_kw', args);
|
||||
}
|
||||
|
||||
Future<dynamic> executeSearchReadPayload(String model, List<String> fields) async {
|
||||
int uid = await authenticate();
|
||||
final payload = OdooRpcHelper.searchRead(
|
||||
db: db,
|
||||
uid: uid,
|
||||
password: password,
|
||||
model: model,
|
||||
domain: [[]],
|
||||
fields: fields,
|
||||
limit: 20,
|
||||
);
|
||||
|
||||
return _callSearchRead(payload);
|
||||
}
|
||||
|
||||
Future<dynamic> _callSearchRead(Map<String, dynamic> payload) async{
|
||||
// Use jsonEncode along with a JsonEncoder for indentation
|
||||
JsonEncoder encoder = const JsonEncoder.withIndent(' ');
|
||||
String prettyString = encoder.convert(payload);
|
||||
|
||||
debugPrint(prettyString);
|
||||
|
||||
try {
|
||||
final resp = await _http
|
||||
.post(_endpoint,
|
||||
headers: {'Content-Type': 'application/json'}, body: jsonEncode(payload))
|
||||
.timeout(const Duration(seconds: 25));
|
||||
final json = jsonDecode(resp.body) as Map<String, dynamic>;
|
||||
if (json.containsKey('error')) {
|
||||
final err = json['error'] as Map?;
|
||||
String? message = (err?['data'] as Map?)?['message'] as String? ??
|
||||
(err?['message'] as String?) ??
|
||||
'Unknown Odoo error';
|
||||
throw Exception('Odoo error: $message');
|
||||
}
|
||||
return json['result'];
|
||||
} on TimeoutException {
|
||||
throw Exception('Timeout — pastikan server Odoo:18069 tercapai');
|
||||
} on http.ClientException catch (e) {
|
||||
throw Exception('Koneksi gagal: ${e.message}');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Future<dynamic> _call(String service, String method, List<dynamic> args) async {
|
||||
final body = {
|
||||
'jsonrpc': '2.0',
|
||||
@@ -52,6 +116,13 @@ class OdooClient {
|
||||
'args': args,
|
||||
},
|
||||
};
|
||||
|
||||
// Use jsonEncode along with a JsonEncoder for indentation
|
||||
JsonEncoder encoder = const JsonEncoder.withIndent(' ');
|
||||
String prettyString = encoder.convert(body);
|
||||
|
||||
debugPrint(prettyString);
|
||||
|
||||
try {
|
||||
final resp = await _http
|
||||
.post(_endpoint,
|
||||
|
||||
Reference in New Issue
Block a user