1 package org.oxerr.viagogo.client.rescu.impl; 2 3 import java.util.function.Supplier; 4 5 import org.oxerr.rescu.ext.singleton.RestProxyFactorySingletonImpl; 6 import org.oxerr.viagogo.client.ViagogoClient; 7 import org.oxerr.viagogo.client.catalog.EventService; 8 import org.oxerr.viagogo.client.inventory.SellerEventService; 9 import org.oxerr.viagogo.client.inventory.SellerListingService; 10 import org.oxerr.viagogo.client.rescu.impl.catalog.EventServiceImpl; 11 import org.oxerr.viagogo.client.rescu.impl.inventory.SellerEventServiceImpl; 12 import org.oxerr.viagogo.client.rescu.impl.inventory.SellerListingServiceImpl; 13 import org.oxerr.viagogo.client.rescu.impl.sale.SaleServiceImpl; 14 import org.oxerr.viagogo.client.rescu.impl.webhook.WebhookServiceImpl; 15 import org.oxerr.viagogo.client.rescu.resource.catalog.EventResource; 16 import org.oxerr.viagogo.client.rescu.resource.inventory.SellerEventResource; 17 import org.oxerr.viagogo.client.rescu.resource.inventory.SellerListingResource; 18 import org.oxerr.viagogo.client.rescu.resource.sale.SaleResource; 19 import org.oxerr.viagogo.client.rescu.resource.webhook.WebhookResource; 20 import org.oxerr.viagogo.client.sale.SaleService; 21 import org.oxerr.viagogo.client.webhook.WebhookService; 22 23 import com.fasterxml.jackson.annotation.JsonInclude.Include; 24 import com.fasterxml.jackson.databind.DeserializationFeature; 25 import com.fasterxml.jackson.databind.ObjectMapper; 26 import com.fasterxml.jackson.databind.PropertyNamingStrategies; 27 import com.fasterxml.jackson.databind.SerializationFeature; 28 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 29 30 import io.openapitools.jackson.dataformat.hal.HALMapper; 31 import jakarta.ws.rs.HeaderParam; 32 import jakarta.ws.rs.PathParam; 33 import si.mazi.rescu.ClientConfig; 34 import si.mazi.rescu.IRestProxyFactory; 35 import si.mazi.rescu.Interceptor; 36 import si.mazi.rescu.RestProxyFactoryImpl; 37 import si.mazi.rescu.serialization.jackson.DefaultJacksonObjectMapperFactory; 38 import si.mazi.rescu.serialization.jackson.JacksonObjectMapperFactory; 39 40 public class ResCUViagogoClient implements ViagogoClient { 41 42 private static final String DEFAULT_BASE_URL = "https://api.viagogo.net"; 43 44 private final String baseUrl; 45 46 private final ClientConfig clientConfig; 47 48 private final IRestProxyFactory restProxyFactory; 49 50 private final EventService eventService; 51 52 private final SellerListingService sellerListingService; 53 54 private final SellerEventService sellerEventService; 55 56 private final SaleService saleService; 57 58 private final WebhookService webhookService; 59 60 public ResCUViagogoClient(String token, Interceptor... interceptors) { 61 this(DEFAULT_BASE_URL, token, interceptors); 62 } 63 64 public ResCUViagogoClient(String baseUrl, String token, Interceptor... interceptors) { 65 this(baseUrl, () -> token, interceptors); 66 } 67 68 public ResCUViagogoClient(Supplier<String> token, Interceptor... interceptors) { 69 this(DEFAULT_BASE_URL, token, interceptors); 70 } 71 72 public ResCUViagogoClient(String baseUrl, Supplier<String> tokenSupplier, Interceptor... interceptors) { 73 this.baseUrl = baseUrl; 74 75 JacksonObjectMapperFactory jacksonObjectMapperFactory = new DefaultJacksonObjectMapperFactory() { 76 77 @Override 78 public void configureObjectMapper(ObjectMapper objectMapper) { 79 super.configureObjectMapper(objectMapper); 80 objectMapper.registerModule(new JavaTimeModule()); 81 objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); 82 objectMapper.setSerializationInclusion(Include.NON_ABSENT); 83 objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 84 objectMapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); 85 objectMapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); 86 } 87 88 @Override 89 protected ObjectMapper createInstance() { 90 return new HALMapper(); 91 } 92 }; 93 94 this.clientConfig = new ClientConfig(); 95 clientConfig.addDefaultParam(PathParam.class, "version", "v2"); 96 clientConfig.addDefaultParam(HeaderParam.class, "Authorization", new Object() { 97 98 @Override 99 public String toString() { 100 return "Bearer " + tokenSupplier.get(); 101 } 102 103 }); 104 clientConfig.setJacksonObjectMapperFactory(jacksonObjectMapperFactory); 105 106 this.restProxyFactory = new RestProxyFactorySingletonImpl(new RestProxyFactoryImpl()); 107 108 this.eventService = new EventServiceImpl(createProxy(EventResource.class, interceptors)); 109 this.sellerListingService = new SellerListingServiceImpl(createProxy(SellerListingResource.class, interceptors)); 110 this.sellerEventService = new SellerEventServiceImpl(createProxy(SellerEventResource.class, interceptors)); 111 this.saleService = new SaleServiceImpl(createProxy(SaleResource.class, interceptors)); 112 this.webhookService = new WebhookServiceImpl(createProxy(WebhookResource.class, interceptors)); 113 } 114 115 @Override 116 public EventService getEventService() { 117 return this.eventService; 118 } 119 120 @Override 121 public SellerListingService getSellerListingService() { 122 return this.sellerListingService; 123 } 124 125 @Override 126 public SellerEventService getSellerEventService() { 127 return this.sellerEventService; 128 } 129 130 @Override 131 public SaleService getSaleService() { 132 return this.saleService; 133 } 134 135 @Override 136 public WebhookService getWebhookService() { 137 return this.webhookService; 138 } 139 140 protected <I> I createProxy(Class<I> restInterface, Interceptor... interceptors) { 141 return this.restProxyFactory.createProxy(restInterface, baseUrl, this.clientConfig, interceptors); 142 } 143 144 }