oauthRequest static method

Future<Result> oauthRequest(
  1. String method,
  2. String uri,
  3. String body
)

Implementation

static Future<Result> oauthRequest(
    String method, String uri, String body) async {
  var url = Uri.parse(uri);
  var client = HttpClient();
  HttpClientRequest request = await client.postUrl(url);
  request.headers.set('x-authing-request-from', 'sdk-flutter');
  request.headers.set('x-authing-lang', Util.getLangHeader());

  if (method.toLowerCase() == "post".toLowerCase()) {
    String type = (body.startsWith('{') || body.startsWith("[")) &&
            (body.endsWith("]") || body.endsWith("}"))
        ? "application/json; charset=utf-8"
        : "application/x-www-form-urlencoded; charset=utf-8";

    request.headers.set("content-type", type);
  }

  request.add(utf8.encode(body));

  HttpClientResponse response = await request.close();
  var res = await response.transform(utf8.decoder).join();
  Result result = Result();
  if (response.statusCode == 200 || response.statusCode == 201) {
    CookieManager().addCookies(response);
    result.code = 200;
    result.message = "success";
    result.data = jsonDecode(res);
    return result;
  } else {
    result.code = response.statusCode;
    result.message = "authRequest failed. " + res;
    return result;
  }
}