11package io .permit .sdk .api ;
22
3+ import com .google .gson .FieldNamingPolicy ;
34import com .google .gson .Gson ;
5+ import com .google .gson .GsonBuilder ;
46import io .permit .sdk .PermitConfig ;
57import io .permit .sdk .api .models .*;
68import okhttp3 .*;
79import org .slf4j .Logger ;
810import org .slf4j .LoggerFactory ;
911
1012import java .io .IOException ;
13+ import java .util .HashMap ;
1114import java .util .List ;
1215
1316interface IElementsApi {
14- UserLoginRequest loginAs (String userId , String tenantId ) throws IOException , PermitApiException ;
17+ UserLoginResponse loginAs (String userId , String tenantId ) throws IOException , PermitApiException ;
1518}
1619
1720public class ElementsClient implements IElementsApi {
18- final static int HTTP_404_NOT_FOUND = 404 ;
19-
2021 final static Logger logger = LoggerFactory .getLogger (ApiClient .class );
2122 private final OkHttpClient client = new OkHttpClient ();
2223 private final PermitConfig config ;
2324 private final Headers headers ;
24- private final String baseUrl ;
25+ private final String apiUrl ;
2526
2627 public ElementsClient (PermitConfig config ) {
2728 this .config = config ;
2829 this .headers = new Headers .Builder ()
2930 .add ("Content-Type" , "application/json" )
3031 .add ("Authorization" , String .format ("Bearer %s" , this .config .getToken ()))
3132 .build ();
32- this .baseUrl = this .config .getPdpAddress ();
33+ this .apiUrl = this .config .getApiUrl ();
3334 }
3435
3536 private void throwIfErrorResponseCode (String requestRepr , Response response , String responseContent , List <Integer > expectedErrorCodes ) throws PermitApiException {
@@ -55,18 +56,20 @@ private void throwIfErrorResponseCode(String requestRepr, Response response, Str
5556 }
5657
5758 @ Override
58- public UserLoginRequest loginAs (String userId , String tenantId ) throws IOException , PermitApiException {
59+ public UserLoginResponse loginAs (String userId , String tenantId ) throws IOException , PermitApiException {
5960 UserLoginRequest element = new UserLoginRequest ();
6061 element .tenantId = tenantId ;
6162 element .userId = userId ;
6263
6364 // request body
64- Gson gson = new Gson ();
65+ Gson gson = new GsonBuilder ()
66+ .setFieldNamingPolicy (FieldNamingPolicy .LOWER_CASE_WITH_UNDERSCORES )
67+ .create ();
6568 String requestBody = gson .toJson (element );
6669 RequestBody body = RequestBody .create (requestBody , MediaType .parse ("application/json" ));
6770
6871 // create the request
69- String url = String .format ("%s/v2/auth/elements_login_as" , this .baseUrl );
72+ String url = String .format ("%s/v2/auth/elements_login_as" , this .config . getApiUrl () );
7073 Request request = new Request .Builder ()
7174 .url (url )
7275 .headers (this .headers )
@@ -84,8 +87,15 @@ public UserLoginRequest loginAs(String userId, String tenantId) throws IOExcepti
8487 }
8588 String responseString = responseBody .string ();
8689 throwIfErrorResponseCode (requestRepr , response , responseString );
87- return gson .fromJson (responseString , UserLoginRequest .class );
90+ UserLoginResponse userLoginResponse = gson .fromJson (responseString , UserLoginResponse .class );
91+ userLoginResponse .content = new HashMap <>();
92+ userLoginResponse .content .put ("url" , userLoginResponse .redirectUrl );
93+ return userLoginResponse ;
8894 }
8995 }
96+
97+ public String getApiUrl () {
98+ return apiUrl ;
99+ }
90100}
91101
0 commit comments