View Javadoc
1   package org.oxerr.vividseats.client.rescu.impl.v1;
2   
3   import java.util.function.Supplier;
4   
5   import org.oxerr.rescu.ext.singleton.RestProxyFactorySingletonImpl;
6   import org.oxerr.vividseats.client.rescu.resource.v1.ListingResource;
7   import org.oxerr.vividseats.client.v1.ListingService;
8   import org.oxerr.vividseats.client.v1.VividSeatsClient;
9   
10  import com.fasterxml.jackson.annotation.JsonInclude.Include;
11  import com.fasterxml.jackson.databind.DeserializationFeature;
12  import com.fasterxml.jackson.databind.ObjectMapper;
13  import com.fasterxml.jackson.databind.SerializationFeature;
14  import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
15  
16  import io.github.poshjosh.ratelimiter.store.BandwidthsStore;
17  import jakarta.ws.rs.HeaderParam;
18  import si.mazi.rescu.ClientConfig;
19  import si.mazi.rescu.IRestProxyFactory;
20  import si.mazi.rescu.Interceptor;
21  import si.mazi.rescu.RestProxyFactoryImpl;
22  import si.mazi.rescu.serialization.jackson.DefaultJacksonObjectMapperFactory;
23  import si.mazi.rescu.serialization.jackson.JacksonObjectMapperFactory;
24  
25  public class ResCUVividSeatsClient implements VividSeatsClient {
26  
27  	private static final String DEFAULT_BASE_URL = "https://brokers.vividseats.com/webservices";
28  
29  	private final String baseUrl;
30  
31  	private final ClientConfig clientConfig;
32  
33  	private final IRestProxyFactory restProxyFactory;
34  
35  	private final ListingService listingService;
36  
37  	/**
38  	 * Constructs a client with the specified token.
39  	 *
40  	 * @param token the token to access the API.
41  	 * @param interceptors the interceptors to intercept the requests.
42  	 */
43  	public ResCUVividSeatsClient(String token, Interceptor... interceptors) {
44  		this(DEFAULT_BASE_URL, token, interceptors);
45  	}
46  
47  	/**
48  	 * Constructs a client with the specified base URL and token.
49  	 *
50  	 * @param baseUrl the base URL of the API.
51  	 * @param token the token to access the API.
52  	 * @param interceptors the interceptors to intercept the requests.
53  	 */
54  	public ResCUVividSeatsClient(String baseUrl, String token, Interceptor... interceptors) {
55  		this(baseUrl, () -> token, interceptors);
56  	}
57  
58  	/**
59  	 * Constructs a client with the specified token.
60  	 *
61  	 * @param token the token to access the API.
62  	 * @param interceptors the interceptors to intercept the requests.
63  	 */
64  	public ResCUVividSeatsClient(Supplier<String> token, Interceptor... interceptors) {
65  		this(DEFAULT_BASE_URL, token, interceptors);
66  	}
67  
68  	/**
69  	 * Constructs a client with the specified base URL and token.
70  	 *
71  	 * @param baseUrl the base URL of the API.
72  	 * @param tokenSupplier the token supplier to access the API.
73  	 * @param interceptors the interceptors to intercept the requests.
74  	 */
75  	public ResCUVividSeatsClient(String baseUrl, Supplier<String> tokenSupplier, Interceptor... interceptors) {
76  		this(baseUrl, tokenSupplier, BandwidthsStore.ofDefaults(), interceptors);
77  	}
78  
79  	/**
80  	 * Constructs a client with the specified base URL, token supplier, bandwidths store and interceptors.
81  	 *
82  	 * @param baseUrl the base URL of the API.
83  	 * @param tokenSupplier the token supplier to access the API.
84  	 * @param bandwidthsStore the bandwidths store to store the bandwidth.
85  	 * @param interceptors the interceptors to intercept the requests.
86  	 */
87  	public ResCUVividSeatsClient(String baseUrl, Supplier<String> tokenSupplier, BandwidthsStore<String> bandwidthsStore, Interceptor... interceptors) {
88  		this.baseUrl = baseUrl;
89  
90  		JacksonObjectMapperFactory jacksonObjectMapperFactory = new DefaultJacksonObjectMapperFactory() {
91  
92  			@Override
93  			public void configureObjectMapper(ObjectMapper objectMapper) {
94  				super.configureObjectMapper(objectMapper);
95  				objectMapper.registerModule(new JavaTimeModule());
96  				objectMapper.setSerializationInclusion(Include.NON_ABSENT);
97  				objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
98  				objectMapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
99  				objectMapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);
100 			}
101 
102 		};
103 
104 		this.clientConfig = new ClientConfig();
105 		clientConfig.addDefaultParam(HeaderParam.class, "Api-token", new Object() {
106 
107 			@Override
108 			public String toString() {
109 				return tokenSupplier.get();
110 			}
111 
112 		});
113 		clientConfig.setJacksonObjectMapperFactory(jacksonObjectMapperFactory);
114 
115 		this.restProxyFactory = new RestProxyFactorySingletonImpl(new RestProxyFactoryImpl());
116 
117 		this.listingService = new ListingServiceImpl(createProxy(ListingResource.class, interceptors));
118 	}
119 
120 	@Override
121 	public ListingService getListingService() {
122 		return this.listingService;
123 	}
124 
125 	protected <I> I createProxy(Class<I> restInterface, Interceptor... interceptors) {
126 		return this.restProxyFactory.createProxy(restInterface, baseUrl, this.clientConfig, interceptors);
127 	}
128 
129 }