makeRequest method

Future<Object> makeRequest(
  1. String functionName,
  2. Map<String, Object> args
)

Implementation

Future<Object> makeRequest(
    String functionName, Map<String, Object> args) async {
  try {
    var func = fnTable[functionName];
    var encodedArgs = Map();
    args.forEach((argName, argValue) {
      encodedArgs[argName] = encode(typeTable, "$functionName.args.$argName",
          func.args[argName], argValue);
    });

    var locale = context == null ? null : Localizations.localeOf(context);

    PackageInfo packageInfo;
    try {
      packageInfo = await PackageInfo.fromPlatform();
    } catch (e) {}

    final platform = {
      "os": Platform.operatingSystem,
      "osVersion": Platform.operatingSystemVersion,
      "dartVersion": Platform.version,
      "appId": packageInfo?.packageName,
      "screenWidth": context == null ? 0 : MediaQuery.of(context).size.width,
      "screenHeight": context == null ? 0 : MediaQuery.of(context).size.height
    };

    final deviceInfo = DeviceInfoPlugin();

    if (Platform.isAndroid) {
      final androidInfo = await deviceInfo.androidInfo;
      platform["model"] = androidInfo.model; //Ex: SM-1234
      platform["brand"] = androidInfo.brand; //Ex: Samsung
      platform["version"] = androidInfo.version.release; //10
      platform["sdkVersion"] = androidInfo.version.sdkInt; //29
    } else if (Platform.isIOS) {
      final iosInfo = await deviceInfo.iosInfo;
      platform["model"] = iosInfo.name; //Ex: iPhone 11 Pro Max
      platform["brand"] = "Apple";
      platform["version"] = iosInfo.systemVersion; //13.1
    }

    var body = {
      "version": 3,
      "requestId": _randomBytesHex(16),
      "name": functionName,
      "args": encodedArgs,
      "extra": this.extra,
      "deviceInfo": {
        "id": await _deviceId(),
        "language": locale == null
            ? null
            : "${locale.languageCode}-${locale.countryCode}",
        "platform": platform,
        "timezone": DateTime.now().timeZoneName,
        "type": Platform.isAndroid
            ? "android"
            : Platform.isIOS
                ? "ios"
                : "flutter",
        "version": packageInfo?.version
      }
    };

    var response = await http.post(baseUrl,
        headers: this.headers, body: jsonEncode(body));
    var responseBody = jsonDecode(utf8.decode(response.bodyBytes));

    if (responseBody["error"] != null) {
      throw _throwError(responseBody["error"]["type"],
          responseBody["error"]["message"], responseBody["error"]["data"]);
    } else {
      return decode(
          typeTable, "$functionName.ret", func.ret, responseBody["result"]);
    }
  } catch (e) {
    if (e is SdkgenError || e is SdkgenErrorWithData)
      throw e;
    else
      throw _throwError("Fatal", e.toString(), null);
  }
}