fetchProvider static method

Future<HttpProvider> fetchProvider(
  1. String providerId,
  2. String appId,
  3. String timestamp,
  4. String signature,
)

Implementation

static Future<HttpProvider> fetchProvider(String providerId, String appId,
    String timestamp, String signature) async {
  final dio = Dio();

  dio.httpClientAdapter = NativeAdapter(
      createCupertinoConfiguration: () =>
          URLSessionConfiguration.ephemeralSessionConfiguration());

  dio.options.headers['accept'] = '*/*';
  dio.options.headers['accept-language'] = 'en-GB,en-US;q=0.9,en;q=0.8';
  dio.options.headers['Content-Type'] = 'application/json';

  final data = jsonEncode({'signature': signature, 'timestamp': timestamp});
  final response = await dio.post<String>(
    'https://api.reclaimprotocol.org/api/applications/$appId/provider/$providerId',
    data: data,
  );
  if (response.statusCode == 200) {
    // Successful request
    final Map<String, dynamic> jsonResult = jsonDecode(response.toString());

    if (!jsonResult.containsKey("providers") ||
        !jsonResult["providers"].containsKey("httpProvider")) {
      throw Exception('Response is malformed $jsonResult');
    }

    if (jsonResult["providers"]["httpProvider"].isEmpty) {
      throw Exception('No providers returned');
    }

    HttpProvider provider =
        HttpProvider.fromJson(jsonResult["providers"]["httpProvider"][0]);

    return provider;
  } else {
    // Handle errors
    throw Exception(
        'Failed to fetch providers. Status code: ${response.statusCode}');
  }
}