|
| 1 | +package io.permit.sdk.api; |
| 2 | + |
| 3 | +import com.google.gson.FieldNamingPolicy; |
| 4 | +import com.google.gson.Gson; |
| 5 | +import com.google.gson.GsonBuilder; |
| 6 | +import io.permit.sdk.PermitConfig; |
| 7 | +import io.permit.sdk.api.models.*; |
| 8 | +import okhttp3.*; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +interface IElementsApi { |
| 17 | + UserLoginResponse loginAs(String userId, String tenantId) throws IOException, PermitApiException; |
| 18 | +} |
| 19 | + |
| 20 | +public class ElementsClient implements IElementsApi { |
| 21 | + final static Logger logger = LoggerFactory.getLogger(ApiClient.class); |
| 22 | + private final OkHttpClient client = new OkHttpClient(); |
| 23 | + private final PermitConfig config; |
| 24 | + private final Headers headers; |
| 25 | + private final String apiUrl; |
| 26 | + |
| 27 | + public ElementsClient(PermitConfig config) { |
| 28 | + this.config = config; |
| 29 | + this.headers = new Headers.Builder() |
| 30 | + .add("Content-Type", "application/json") |
| 31 | + .add("Authorization", String.format("Bearer %s", this.config.getToken())) |
| 32 | + .build(); |
| 33 | + this.apiUrl = this.config.getApiUrl(); |
| 34 | + } |
| 35 | + |
| 36 | + private void throwIfErrorResponseCode(String requestRepr, Response response, String responseContent, List<Integer> expectedErrorCodes) throws PermitApiException { |
| 37 | + String log = String.format("Received response: %s : status code %d : %s", requestRepr, response.code(), responseContent); |
| 38 | + if (!response.isSuccessful() && this.config.isDebugMode()) { |
| 39 | + this.logger.error(log); |
| 40 | + } else { |
| 41 | + this.logger.debug(log); |
| 42 | + } |
| 43 | + if (!response.isSuccessful() && !expectedErrorCodes.contains(response.code())) { |
| 44 | + throw new PermitApiException( |
| 45 | + String.format( |
| 46 | + "unexpected status code: %d for request: %s", |
| 47 | + response.code(), |
| 48 | + requestRepr |
| 49 | + ) |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void throwIfErrorResponseCode(String requestRepr, Response response, String responseContent) throws PermitApiException { |
| 55 | + throwIfErrorResponseCode(requestRepr, response, responseContent, List.of()); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public UserLoginResponse loginAs(String userId, String tenantId) throws IOException, PermitApiException { |
| 60 | + UserLoginRequest element = new UserLoginRequest(); |
| 61 | + element.tenantId = tenantId; |
| 62 | + element.userId = userId; |
| 63 | + |
| 64 | + // request body |
| 65 | + Gson gson = new GsonBuilder() |
| 66 | + .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) |
| 67 | + .create(); |
| 68 | + String requestBody = gson.toJson(element); |
| 69 | + RequestBody body = RequestBody.create(requestBody, MediaType.parse("application/json")); |
| 70 | + |
| 71 | + // create the request |
| 72 | + String url = String.format("%s/v2/auth/elements_login_as", this.config.getApiUrl()); |
| 73 | + Request request = new Request.Builder() |
| 74 | + .url(url) |
| 75 | + .headers(this.headers) |
| 76 | + .post(body) |
| 77 | + .build(); |
| 78 | + |
| 79 | + String requestRepr = String.format("permit.elements.login_as(%s)", requestBody); |
| 80 | + this.logger.debug(String.format("Sending request: %s", requestRepr)); |
| 81 | + |
| 82 | + // send the request |
| 83 | + try (Response response = client.newCall(request).execute()) { |
| 84 | + ResponseBody responseBody = response.body(); |
| 85 | + if (responseBody == null) { |
| 86 | + throw new IOException("got empty response"); |
| 87 | + } |
| 88 | + String responseString = responseBody.string(); |
| 89 | + throwIfErrorResponseCode(requestRepr, response, responseString); |
| 90 | + UserLoginResponse userLoginResponse = gson.fromJson(responseString, UserLoginResponse.class); |
| 91 | + userLoginResponse.content = new HashMap<>(); |
| 92 | + userLoginResponse.content.put("url", userLoginResponse.redirectUrl); |
| 93 | + return userLoginResponse; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public String getApiUrl() { |
| 98 | + return apiUrl; |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments