com.garmin.client.GarminClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of garmin-oauth Show documentation
Show all versions of garmin-oauth Show documentation
Package garmin license, new user license to app
The newest version!
package com.garmin.client;
import com.garmin.config.GarminConfig;
import com.garmin.model.GarminTokenInfo;
import com.garmin.service.HttpRequestAdapter;
import com.garmin.util.Consts;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
/**
* Request Garmin service interface unified export
* @Classname GarminClient
* @Description TODO
* @Date 2019/10/18 10:29
* @Created by weihua.zou
*/
@Component
@Slf4j
@Data
public class GarminClient {
private final GarminConfig garminConfig;
private ThreadLocal userAccessToken = new ThreadLocal<>();
private ThreadLocal userAccessSecret = new ThreadLocal();
private final RestTemplate restTemplate;
private HttpEntity httpEntity;
public GarminClient(GarminConfig garminConfig, RestTemplate restTemplate) {
this.garminConfig = garminConfig;
this.restTemplate = restTemplate;
restTemplate.setInterceptors(Collections.singletonList((request, body, execution) -> {
DefaultOAuthConsumer consumer = new DefaultOAuthConsumer(garminConfig.getConsumerkey(), garminConfig.getConsumerSecret());
if (getUserAccessToken().get() != null && getUserAccessSecret().get() != null) {
consumer.setTokenWithSecret(getUserAccessToken().get(), getUserAccessSecret().get());
}
try {
consumer.sign(new HttpRequestAdapter(request, body));
} catch (OAuthMessageSignerException | OAuthExpectationFailedException
| OAuthCommunicationException e) {
throw new RuntimeException("unable to sign request with consumer_key=" + garminConfig.getConsumerkey(), e);
}
return execution.execute(request, body);
}));
}
public String getRequestTokenSecret(){
log.info("getRequestTokenSecret is start,garminConfig:{}",garminConfig!=null?garminConfig.toString():null);
if(garminConfig==null ||StringUtils.isBlank(garminConfig.getRequestTokenUrl())){
log.error("requestTokenUrl is null");
return null;
}
try {
ResponseEntity result = restTemplate.exchange(garminConfig.getRequestTokenUrl(), HttpMethod.GET,httpEntity,String.class);
String tokenAndSecret =result.getBody();
log.info("tokenSecret is {}",tokenAndSecret);
return tokenAndSecret;
}catch (Exception e){
log.error(e.getMessage());
}
return null;
}
public String generateAccessTokenSecret(String url, GarminTokenInfo info)
{
String accessTokenSecret = null;
try{
log.debug("generateAccessTokenSecret getRestTemplate is start");
RestTemplate restTemplate = this.getRestTemplate(info.getToken(), info.getSecret());
log.debug("generateAccessTokenSecret getRestTemplate is end");
ResponseEntity result = restTemplate.exchange(url, HttpMethod.POST,this.httpEntity,String.class);
log.debug("restTemplate exchange is end ,url:{}",url);
accessTokenSecret = result.getBody();
log.info("value in accessTokenSecret is: {}",accessTokenSecret);
}
catch(Exception e){
log.error(e.getMessage());
}
return accessTokenSecret + Consts.VARIABLE_DELIMTER + info.getUserId() + Consts.VARIABLE_DELIMTER + info.getUserName();
}
// @PostConstruct
public void init() {
//
// restTemplate = new RestTemplateBuilder().additionalInterceptors(new ClientHttpRequestInterceptor() {
//
// @Override
// public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
// throws IOException {
// DefaultOAuthConsumer consumer = new DefaultOAuthConsumer(garminConfig.getConsumerkey(), garminConfig.getConsumerSecret());
//
// if (getUserAccessToken().get() != null && getUserAccessSecret().get() != null) {
// consumer.setTokenWithSecret(getUserAccessToken().get(), getUserAccessSecret().get());
// }
//
// try {
// consumer.sign(new HttpRequestAdapter(request, body));
// } catch (OAuthMessageSignerException | OAuthExpectationFailedException
// | OAuthCommunicationException e) {
// throw new RuntimeException("unable to sign request with consumer_key=" + garminConfig.getConsumerkey(), e);
// }
// return execution.execute(request, body);
// }
// }).build();
//
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_JSON);
// httpEntity = new HttpEntity(headers);
}
public RestTemplate getRestTemplate(String userAccessToken, String userAccessSecret) {
this.userAccessToken.set(userAccessToken);
this.userAccessSecret.set(userAccessSecret);
return restTemplate;
}
}