@@ -3,6 +3,8 @@ package fi.hsl.jore4.hastus.graphql
33import com.expediagroup.graphql.client.jackson.GraphQLClientJacksonSerializer
44import com.expediagroup.graphql.client.jackson.types.OptionalInput
55import com.expediagroup.graphql.client.ktor.GraphQLKtorClient
6+ import com.expediagroup.graphql.client.types.GraphQLClientRequest
7+ import com.expediagroup.graphql.client.types.GraphQLClientResponse
68import fi.hsl.jore4.hastus.config.HasuraConfiguration
79import fi.hsl.jore4.hastus.data.hastus.IHastusData
810import fi.hsl.jore4.hastus.data.hastus.StopDistance
@@ -63,6 +65,31 @@ class GraphQLService(config: HasuraConfiguration) {
6365
6466 private val writer = CsvWriter ()
6567
68+ private fun <T : Any > sendRequest (
69+ request : GraphQLClientRequest <T >,
70+ headers : Map <String , String >
71+ ): T ? {
72+ return runBlocking {
73+ LOGGER .debug {
74+ " GraphQL request:\n ${request.query} ,\n variables: ${request.variables} "
75+ }
76+
77+ val queryResponse: GraphQLClientResponse <T > = client.execute(request) {
78+ headers.map { header(it.key, it.value) }
79+ }
80+
81+ LOGGER .debug {
82+ " GraphQL ${request.operationName} response: $queryResponse "
83+ }
84+
85+ if (queryResponse.errors?.isNotEmpty() == true ) {
86+ throw IllegalStateException (queryResponse.errors?.toString())
87+ }
88+
89+ queryResponse.data
90+ }
91+ }
92+
6693 private fun convertRoutes (
6794 routes : List <route_route>,
6895 distances : Map <Pair <String , String >, Int >
@@ -115,16 +142,7 @@ class GraphQLService(config: HasuraConfiguration) {
115142 )
116143 )
117144
118- val distancesBetweenStops: DistanceBetweenStopPoints .Result ? = runBlocking {
119- val queryResponse = client.execute(distancesQuery) {
120- headers.map { header(it.key, it.value) }
121- }
122- LOGGER .debug { " distance between stops graphQL response: $queryResponse " }
123- if (queryResponse.errors?.isNotEmpty() == true ) {
124- throw IllegalStateException (queryResponse.errors?.toString())
125- }
126- queryResponse.data
127- }
145+ val distancesBetweenStops: DistanceBetweenStopPoints .Result ? = sendRequest(distancesQuery, headers)
128146
129147 val transformedDistances = distancesBetweenStops
130148 ?.service_pattern_get_distances_between_stop_points_by_routes
@@ -148,16 +166,7 @@ class GraphQLService(config: HasuraConfiguration) {
148166 )
149167 )
150168
151- val routes: RoutesWithHastusData .Result ? = runBlocking {
152- val queryResponse = client.execute(routesQuery) {
153- headers.map { header(it.key, it.value) }
154- }
155- LOGGER .debug { " routes for routes graphQL response: $queryResponse " }
156- if (queryResponse.errors?.isNotEmpty() == true ) {
157- throw IllegalStateException (queryResponse.errors?.toString())
158- }
159- queryResponse.data
160- }
169+ val routes: RoutesWithHastusData .Result ? = sendRequest(routesQuery, headers)
161170
162171 val routeIds: List <UUID > = routes?.route_route?.map { it.route_id }.orEmpty()
163172 val distancesBetweenStops: List <JoreDistanceBetweenTwoStopPoints > = getStopDistances(
@@ -187,16 +196,7 @@ class GraphQLService(config: HasuraConfiguration) {
187196 )
188197 )
189198
190- val journeyPatterns: JourneyPatternsForRoutes .Result ? = runBlocking {
191- val queryResponse = client.execute(journeyPatternsQuery) {
192- headers.map { header(it.key, it.value) }
193- }
194- LOGGER .debug { " journey patterns for routes graphQL response: $queryResponse " }
195- if (queryResponse.errors?.isNotEmpty() == true ) {
196- throw IllegalStateException (queryResponse.errors?.toString())
197- }
198- queryResponse.data
199- }
199+ val journeyPatterns: JourneyPatternsForRoutes .Result ? = sendRequest(journeyPatternsQuery, headers)
200200
201201 return journeyPatterns
202202 ?.route_route
@@ -220,16 +220,7 @@ class GraphQLService(config: HasuraConfiguration) {
220220 fun getVehicleTypes (
221221 headers : Map <String , String >
222222 ): Map <Int , UUID > {
223- val vehicleTypes: ListVehicleTypes .Result ? = runBlocking {
224- val queryResponse = client.execute(ListVehicleTypes ()) {
225- headers.map { header(it.key, it.value) }
226- }
227- LOGGER .debug { " vehicle type graphQL response: $queryResponse " }
228- if (queryResponse.errors?.isNotEmpty() == true ) {
229- throw IllegalStateException (queryResponse.errors?.toString())
230- }
231- queryResponse.data
232- }
223+ val vehicleTypes: ListVehicleTypes .Result ? = sendRequest(ListVehicleTypes (), headers)
233224
234225 return vehicleTypes
235226 ?.timetables
@@ -243,16 +234,7 @@ class GraphQLService(config: HasuraConfiguration) {
243234 fun getDayTypes (
244235 headers : Map <String , String >
245236 ): Map <String , UUID > {
246- val dayTypes: ListDayTypes .Result ? = runBlocking {
247- val queryResponse = client.execute(ListDayTypes ()) {
248- headers.map { header(it.key, it.value) }
249- }
250- LOGGER .debug { " vehicle type graphQL response: $queryResponse " }
251- if (queryResponse.errors?.isNotEmpty() == true ) {
252- throw IllegalStateException (queryResponse.errors?.toString())
253- }
254- queryResponse.data
255- }
237+ val dayTypes: ListDayTypes .Result ? = sendRequest(ListDayTypes (), headers)
256238
257239 return dayTypes
258240 ?.timetables
@@ -277,16 +259,7 @@ class GraphQLService(config: HasuraConfiguration) {
277259 )
278260 )
279261
280- val vehicleScheduleFrames: InsertVehicleScheduleFrame .Result ? = runBlocking {
281- val queryResponse = client.execute(insertVehicleScheduleFrames) {
282- headers.map { header(it.key, it.value) }
283- }
284- LOGGER .debug { " vehicle type graphQL response: $queryResponse " }
285- if (queryResponse.errors?.isNotEmpty() == true ) {
286- throw IllegalStateException (queryResponse.errors?.toString())
287- }
288- queryResponse.data
289- }
262+ val vehicleScheduleFrames: InsertVehicleScheduleFrame .Result ? = sendRequest(insertVehicleScheduleFrames, headers)
290263
291264 return vehicleScheduleFrames
292265 ?.timetables
@@ -318,16 +291,7 @@ class GraphQLService(config: HasuraConfiguration) {
318291 )
319292 )
320293
321- val journeyPatternRefs: InsertJourneyPatternRefs .Result ? = runBlocking {
322- val queryResponse = client.execute(insertJourneyPatternRefs) {
323- headers.map { header(it.key, it.value) }
324- }
325- LOGGER .debug { " vehicle type graphQL response: $queryResponse " }
326- if (queryResponse.errors?.isNotEmpty() == true ) {
327- throw IllegalStateException (queryResponse.errors?.toString())
328- }
329- queryResponse.data
330- }
294+ val journeyPatternRefs: InsertJourneyPatternRefs .Result ? = sendRequest(insertJourneyPatternRefs, headers)
331295
332296 return journeyPatternRefs
333297 ?.timetables
0 commit comments