issueCertificate method

Future<Map<String, dynamic>> issueCertificate(
  1. String csr,
  2. String otp
)

Issues a compliance certificate using the provided CSR and OTP.

Implementation

Future<Map<String, dynamic>> issueCertificate(String csr, String otp) async {
  final headers = {
    "Accept-Version": API.settings["API_VERSION"]!,
    "OTP": otp,
    'Content-Type': 'application/json',
    ...authHeaders,
  };

  try {
    final response = await http.post(
      Uri.parse('$baseUrl/compliance'),
      headers: headers,
      body: jsonEncode({"csr": base64Encode(utf8.encode(csr))}),
    );

    if (response.statusCode != 200) {
      print("Error: ${response.statusCode}, Body: ${response.body}");
      throw Exception("Error issuing a compliance certificate.");
    }

    final data = jsonDecode(response.body);
    final issuedCertificate = '''
-----BEGIN CERTIFICATE-----
${utf8.decode(base64Decode(data["binarySecurityToken"]))}
-----END CERTIFICATE-----
''';
    return {
      "issued_certificate": issuedCertificate,
      "api_secret": data["secret"],
      "request_id": data["requestID"],
    };
  } catch (e) {
    print("An error occurred: $e");
    rethrow;
  }
}