package com.plannaplan.services; import java.io.IOException; import java.util.Map; import java.util.concurrent.ExecutionException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.type.TypeReference; import com.github.scribejava.core.builder.ServiceBuilder; import com.github.scribejava.core.model.OAuth1AccessToken; import com.github.scribejava.core.model.OAuthRequest; import com.github.scribejava.core.model.Response; import com.github.scribejava.core.model.Verb; import com.github.scribejava.core.oauth.OAuth10aService; import com.plannaplan.api.UsosOauth1Service; import com.plannaplan.models.UserApiResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** * service to call usos api endpoints */ @Service public class UsosApiService { private static final String NAME_FIELD = "first_name"; private static final String SURNAME_FIELD = "last_name"; @Value("${plannaplan.apiurl}") private String apiUrl; @Value("${plannaplan.apikey}") private String apikey; @Value("${plannaplan.apisecret}") private String apisecret; public UsosApiService() { } /** * /services/users/user * * @param usosId user id in usos * @return UserApiResponse modle contatining desired values */ public UserApiResponse getUserData(String usosId) { final UserApiResponse apiResponse = new UserApiResponse(); try { final OAuth10aService service = new ServiceBuilder(apikey).apiSecret(apisecret) .build(UsosOauth1Service.instance()); final OAuthRequest request = new OAuthRequest(Verb.GET, apiUrl + "/services/users/user?user_id=" + usosId); service.signRequest(new OAuth1AccessToken("", ""), request); System.out.println("PRZED TRY"); try (Response response = service.execute(request)) { System.out.println("TRY"); final String json = response.getBody(); final ObjectMapper mapper = new ObjectMapper(); Map map = mapper.readValue(json, new TypeReference>() { }); System.out.println("FIELD"); apiResponse.setName(map.get(NAME_FIELD)); System.out.println(map.get(NAME_FIELD)); apiResponse.setSurname(map.get(SURNAME_FIELD)); System.out.println(map.get(SURNAME_FIELD)); } } catch (IOException | InterruptedException | ExecutionException e) { e.printStackTrace(); } return apiResponse; } }