Added api service
This commit is contained in:
@ -1,54 +1,74 @@
|
||||
package com.plannaplan.services;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.social.oauth1.OAuth1Parameters;
|
||||
import org.springframework.social.support.ClientHttpRequestFactorySelector;
|
||||
// import org.springframework.social.oauth1.OAuth1Template;
|
||||
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;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.social.oauth1.*;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
// import org.springframework.social.oauth1.ProtectedResourceClientFactory;
|
||||
|
||||
@Service
|
||||
/**
|
||||
* service to call usos api endpoints
|
||||
*/
|
||||
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() {
|
||||
|
||||
}
|
||||
|
||||
public void xd() {
|
||||
RestTemplate xddd = new RestTemplate();
|
||||
/**
|
||||
* /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());
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("oauth_version", "1.0");
|
||||
headers.set("oauth_signature_method", "HMAC-SHA1");
|
||||
headers.set("oauth_consumer_key", "e6jbeN57HC99MfsfmZwN");
|
||||
headers.set("oauth_signature", "fhcLdwerJyzZBGA9WnAYzbySJRW9Wv5wj3h8uVgp");
|
||||
headers.set("oauth_nonce", "" + (int) (Math.random() * 100000000));
|
||||
headers.set("oauth_timestamp", "" + (System.currentTimeMillis() / 1000));
|
||||
final OAuthRequest request = new OAuthRequest(Verb.GET, apiUrl + "/services/users/user?user_id=" + usosId);
|
||||
service.signRequest(new OAuth1AccessToken("", ""), request);
|
||||
|
||||
// BasicAuthenticationInterceptor
|
||||
try (Response response = service.execute(request)) {
|
||||
final String json = response.getBody();
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
Map<String, String> map = mapper.readValue(json, new TypeReference<Map<String, String>>() {
|
||||
});
|
||||
apiResponse.setName(map.get(NAME_FIELD));
|
||||
apiResponse.setSurname(map.get(SURNAME_FIELD));
|
||||
}
|
||||
} catch (IOException | InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// OAuth1Credentials credentials;
|
||||
// RestTemplate client = new
|
||||
// RestTemplate(ClientHttpRequestFactorySelector.getRequestFactory());
|
||||
// OAuth1RequestInterceptor interceptor = new
|
||||
// OAuth1RequestInterceptor(credentials);
|
||||
// List<ClientHttpRequestInterceptor> interceptors = new
|
||||
// LinkedList<ClientHttpRequestInterceptor>();
|
||||
// interceptors.add(interceptor);
|
||||
// client.setInterceptors(interceptors);
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||
|
||||
ResponseEntity<String> result = xddd.exchange("https://usosapidemo.amu.edu.pl/services/users/user",
|
||||
HttpMethod.GET, entity, String.class);
|
||||
System.out.println(result);
|
||||
return apiResponse;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user