use of com.squareup.okhttp.Call in project casa by GluuFederation.
the class StytchService method sendSMS.
public StytchPhoneCredential sendSMS(String phoneNumber, String userId) {
OkHttpClient httpClient = new OkHttpClient();
String data = "{ \"phone_number\": \"+" + phoneNumber + "\"}";
RequestBody body = RequestBody.create(MediaType.parse("application/json"), data);
String asB64 = new String(new Base64().encode((properties.get("PROJECT_ID") + ":" + properties.get("SECRET")).getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
String authHeader = "Basic " + new String(asB64);
Request request = new Request.Builder().url(getScriptPropertyValue("ENROLL_ENDPOINT")).post(body).addHeader(HttpHeaders.AUTHORIZATION, authHeader).addHeader("Content-Type", "application/json").build();
Call call = httpClient.newCall(request);
logger.debug(request.toString() + "-" + request.headers() + "-" + request.body().toString());
try {
Response response = call.execute();
if (response.code() == 200 || response.code() == 201) {
String responseBody = response.body().string();
Map<String, String> map = mapper.readValue(responseBody, Map.class);
StytchPhoneCredential stytchPhoneCred = new StytchPhoneCredential();
stytchPhoneCred.setStytchUserId(map.get("user_id"));
stytchPhoneCred.setStytchPhoneId(map.get("phone_id"));
return stytchPhoneCred;
} else {
logger.debug("Response for enrolling user was : " + response.code() + "---" + response.message());
return null;
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
return null;
}
}
Aggregations