1 package org.oxerr.seatgeek.client.rescu;
2
3 import jakarta.ws.rs.HeaderParam;
4
5 import org.apache.commons.lang3.ArrayUtils;
6 import org.oxerr.rescu.ext.ratelimiter.RateLimiterInterceptor;
7 import org.oxerr.rescu.ext.ratelimiter.RateLimiter;
8 import org.oxerr.rescu.ext.singleton.RestProxyFactorySingletonImpl;
9 import org.oxerr.seatgeek.client.ListingService;
10 import org.oxerr.seatgeek.client.SeatGeekClient;
11
12 import com.fasterxml.jackson.annotation.JsonInclude.Include;
13 import com.fasterxml.jackson.databind.DeserializationFeature;
14 import com.fasterxml.jackson.databind.ObjectMapper;
15 import com.fasterxml.jackson.databind.PropertyNamingStrategies;
16 import com.fasterxml.jackson.databind.SerializationFeature;
17 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
18
19 import si.mazi.rescu.ClientConfig;
20 import si.mazi.rescu.IRestProxyFactory;
21 import si.mazi.rescu.Interceptor;
22 import si.mazi.rescu.RestProxyFactoryImpl;
23 import si.mazi.rescu.serialization.jackson.DefaultJacksonObjectMapperFactory;
24 import si.mazi.rescu.serialization.jackson.JacksonObjectMapperFactory;
25
26 public class ResCUSeatGeekClient implements SeatGeekClient {
27
28 private final String baseUrl;
29
30 private final ClientConfig clientConfig;
31
32 private final IRestProxyFactory restProxyFactory;
33
34 private final ListingService listingService;
35
36 public ResCUSeatGeekClient(String token, RateLimiter rateLimiter, Interceptor... interceptors) {
37 this("https://sellerdirect-api.seatgeek.com", token, rateLimiter, interceptors);
38 }
39
40 public ResCUSeatGeekClient(String baseUrl, String token, RateLimiter rateLimiter, Interceptor... interceptors) {
41 this.baseUrl = baseUrl;
42
43 JacksonObjectMapperFactory jacksonObjectMapperFactory = new DefaultJacksonObjectMapperFactory() {
44
45 @Override
46 public void configureObjectMapper(ObjectMapper objectMapper) {
47 super.configureObjectMapper(objectMapper);
48 objectMapper.registerModule(new JavaTimeModule());
49 objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
50 objectMapper.setSerializationInclusion(Include.NON_ABSENT);
51 objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
52 objectMapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
53 objectMapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);
54 }
55
56 @Override
57 protected ObjectMapper createInstance() {
58 return new ObjectMapper();
59 }
60 };
61
62 this.clientConfig = new ClientConfig();
63 clientConfig.addDefaultParam(HeaderParam.class, "Authorization", "token " + token);
64 clientConfig.setJacksonObjectMapperFactory(jacksonObjectMapperFactory);
65
66 this.restProxyFactory = new RestProxyFactorySingletonImpl(new RestProxyFactoryImpl());
67
68 RateLimiterInterceptor rateLimiterInterceptor = new RateLimiterInterceptor(rateLimiter);
69 Interceptor[] allInterceptors = ArrayUtils.addFirst(interceptors, rateLimiterInterceptor);
70
71 this.listingService = new ListingServiceImpl(createProxy(ListingResource.class, allInterceptors));
72 }
73
74 @Override
75 public ListingService getListingService() {
76 return this.listingService;
77 }
78
79 protected <I> I createProxy(Class<I> restInterface, Interceptor... interceptors) {
80 return this.restProxyFactory.createProxy(restInterface, baseUrl, this.clientConfig, interceptors);
81 }
82
83 }