@@ -10,31 +10,29 @@ import com.colisweb.distances.providers.google.{
1010}
1111import com .colisweb .distances .{DistanceApi , TravelMode , Types , _ }
1212import com .google .maps .OkHttpRequestHandler
13- import eu .timepit .refined .api .Refined
14- import eu .timepit .refined .auto ._
1513import org .slf4j .LoggerFactory
1614
1715import scala .concurrent .ExecutionContext
18- import scala .concurrent .duration .{Duration , FiniteDuration }
1916import scala .util .Try
2017
2118final class JRubyScalaDistance (googleApiConfig : GoogleApiConfiguration , redisConfig : RedisConfiguration ) {
2219
2320 implicit val contextShift : ContextShift [IO ] = IO .contextShift(ExecutionContext .global)
2421
22+ val logger = LoggerFactory .getLogger(classOf [OkHttpRequestHandler ])
23+ val loggingF = (message : String ) => logger.debug(message.replaceAll(" key=([^&]*)&" , " key=REDACTED&" ))
24+
25+ val googleGeoApiContext = GoogleGeoApiContext (
26+ googleApiConfig.apiKey,
27+ googleApiConfig.connectTimeout,
28+ googleApiConfig.readTimeout,
29+ googleApiConfig.queryRateLimit,
30+ loggingF
31+ )
32+
2533 val distanceApi : DistanceApi [IO , GoogleDistanceProviderError ] = {
26- val logger = LoggerFactory .getLogger(classOf [OkHttpRequestHandler ])
27- val loggingF = (message : String ) => logger.debug(message.replaceAll(" key=([^&]*)&" , " key=REDACTED&" ))
2834
29- val distanceProvider = GoogleDistanceProvider [IO ](
30- GoogleGeoApiContext (
31- googleApiConfig.apiKey,
32- googleApiConfig.connectTimeout,
33- googleApiConfig.readTimeout,
34- googleApiConfig.queryRateLimit,
35- loggingF
36- )
37- )
35+ val distanceProvider = GoogleDistanceProvider [IO ](googleGeoApiContext)
3836
3937 val cache = RedisCache [IO ](
4038 caches.RedisConfiguration (redisConfig.host, redisConfig.port),
@@ -52,17 +50,18 @@ final class JRubyScalaDistance(googleApiConfig: GoogleApiConfiguration, redisCon
5250 origin : LatLong ,
5351 destination : LatLong ,
5452 travelMode : TravelMode
55- ): Try [Types .Distance ] =
53+ ): Try [Types .Distance ] =
5654 distanceApi
5755 .distance(origin, destination, List (travelMode))
5856 .unsafeRunSync()
5957 .getOrElse(travelMode, Left (new RuntimeException (" Unknown travelMode exception happened" )))
6058 .toTry
6159
62-
6360 def getDrivingDistance (
6461 origin : LatLong ,
6562 destination : LatLong
6663 ): Try [Types .Distance ] = getDistance(origin, destination, TravelMode .Driving )
6764
65+ def shutdown (): Unit = googleGeoApiContext.geoApiContext.shutdown()
66+
6867}
0 commit comments