callRPC method

Future callRPC(
  1. dynamic path,
  2. dynamic funcName,
  3. dynamic params
)

Low Level RPC call. It has to be used on all Odoo Controllers with type='json'

Implementation

Future<dynamic> callRPC(path, funcName, params) async {
  var headers = {'Content-type': 'application/json'};
  var cookie = '';
  if (_sessionId != null) {
    cookie = 'session_id=${_sessionId!.id}';
  }
  if (frontendLang.isNotEmpty) {
    if (cookie.isEmpty) {
      cookie = 'frontend_lang=$frontendLang';
    } else {
      cookie += '; frontend_lang=$frontendLang';
    }
  }
  if (cookie.isNotEmpty) {
    headers['Cookie'] = cookie;
  }

  final uri = Uri.parse(baseURL + path);
  var body = json.encode({
    'jsonrpc': '2.0',
    'method': funcName,
    'params': params,
    'id': sha1.convert(utf8.encode(DateTime.now().toString())).toString()
  });

  try {
    if (_inRequestStreamActive) _inRequestStreamController.add(true);
    final response = await httpClient.post(uri, body: body, headers: headers);

    _updateSessionIdFromCookies(response);
    var result = json.decode(response.body);
    if (result['error'] != null) {
      if (result['error']['code'] == 100) {
        // session expired
        _setSessionId('');
        final err = result['error'].toString();
        throw OdooSessionExpiredException(err);
      } else {
        // Other error
        final err = result['error'].toString();
        throw OdooException(err);
      }
    }

    if (_inRequestStreamActive) _inRequestStreamController.add(false);
    return result['result'];
  } catch (e) {
    if (_inRequestStreamActive) _inRequestStreamController.add(false);
    rethrow;
  }
}