Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Function for getting prices would be convenient. #97

Open
AlfHou opened this issue Dec 8, 2019 · 14 comments
Open

Function for getting prices would be convenient. #97

AlfHou opened this issue Dec 8, 2019 · 14 comments
Labels
enhancement New feature or request

Comments

@AlfHou
Copy link

AlfHou commented Dec 8, 2019

I tried to use your SDK for a side project I am working on, but due to there not being a function for getting prices, and your API documentation being fairly poor. I resorted to using Vy's undocumented API

@draperunner
Copy link
Contributor

draperunner commented Dec 9, 2019

Hi! You're right. This SDK is currently only for journey planning. If you want prices and offers, you can use the Offers API. There are some guides there, but depending on what you want to do, they might not all be relevant. Maybe I can help if you give me some more details. You can also always contact us by using the feedback form on developer.entur.org or by sending an email to kollektivdata@entur.org.

Thanks for reaching out! I agree, functions for getting prices would be convenient. :)

@AlfHou
Copy link
Author

AlfHou commented Dec 9, 2019

I see! Yeah, I tried to use the offers API but couldn't quite figure it out
Maybe I'll try it again in the future, but I kind of found another way that works atm. 😄

@draperunner
Copy link
Contributor

I'm interested in the details of what you're trying to do, if you'd like to share more details. Maybe we can improve the docs and SDK further. :)

Are you trying to show prices for journeys (trip patterns) or for certain zones? For a certain operator (Ruter, Vy or others)? For one adult, or special combinations?

@AlfHou
Copy link
Author

AlfHou commented Dec 9, 2019

I’m creating a lowfare calendar for train prices (like the one Norwegian has for their planes). So I need a way to get the prices for a given journey on a given day.

I think the prices for short train journeys (i.e. Lillestrøm - Oslo) stay more or less the same, but it is relevant for longer journeys (Oslo - Bergen).

Check out my repo for a example of what I am doing 😄

@AlfHou
Copy link
Author

AlfHou commented Jan 8, 2020

Hi @draperunner. Vy blocked the ip of the server my project was running on so i figured I'd try moving over to Entur.

I am trying to get an offer for this trip using this endpoint: https://api.entur.io/sales/v1/offers/search/trip/trip-pattern/{id}. My problem is I don't know how to get the id the endpoint requires. In the link I posted there is an ID that says "NSB:Line:41", however that doesn't seem to be working.

Any help would be appreciated :)

@draperunner
Copy link
Contributor

To get the ID, you also need to do make the search through Offers using the /search/graphql endpoint. All you need to do is to point the hosts.journeyplanner config to this new URL:

 const sdk = new EnturService({
     clientName: '<YOUR_CLIENT_NAME>',
+    hosts: {
+        journeyplanner: 'https://api.staging.entur.io/sales/v1/offers/search'
+    }
 })

The trip patterns will then have an id you can use to fetch offers from the endpoint you mentioned.

I suggest you use the staging URL for testing and respect rate limits, so we don't need to block you as well ;). Check out the throttler utility if you're bulk calling a lot of calls.

@AlfHou
Copy link
Author

AlfHou commented Jan 8, 2020

Burning the midnight oil and responding to (I assume) work related questions late at night? Props!

Thank you very much for the fast response. I’ll probably use C# and .NET Core for querying the API and then cache the results on my server, but thank you very much. I think I’ll figure it out by looking at the source code here.

I’ll make sure to follow the rate limits and try to implement some kind of throttler on my backend. Looking at your constraints in rateLimits.js, it seems like I am allowed to do quite a lot of calls.

@AlfHou
Copy link
Author

AlfHou commented Jan 9, 2020

So. I manage to query https://api.staging.entur.io/sales/v1/offers/search/graphql and get back this id: 5e16c6300718be000132710a.

However when i try to query https://api.staging.entur.io/sales/v1/offers/search/trip/trip-pattern/5e16c6300718be000132710a I receive the response: { "message": "Internal Server Error", "description": "An unexpected error occurred: Failed fetching offers for trip pattern id 5e16c6300718be000132710a" }.

Any suggestions?

@draperunner
Copy link
Contributor

A trip pattern is only cached for about 15 minutes, so if the ID 5e16c6300718be000132710a is older than this, you won't get any offers. You need to do the trip search again, and immediately ask for offers after receiving the results. I guess that's the issue. :)

@AlfHou
Copy link
Author

AlfHou commented Jan 9, 2020

I don't think that's it.
I am executing the queries right after each other.

For example, you could try this

curl --location --request POST 'https://api.staging.entur.io/sales/v1/offers/search/graphql' --header 'Content-Type: application/json' --data-raw '{"query":"{\r\n  trip(\r\n    from: {place: \"NSR:StopPlace:548\"\r\n    }\r\n\r\n    to: {\r\n      place:\"NSR:StopPlace:337\"\r\n    }\r\n    dateTime: \"2020-01-09T00:00:00+0100\",\r\n    modes: [rail]\r\n    \r\n    \r\n    \r\n  )\r\n\r\n#### Requested fields\r\n  {\r\n    tripPatterns {\r\n      startTime\r\n      duration\r\n\r\n          legs {\r\n            mode\r\n            distance\r\n            line {\r\n              id\r\n              publicCode\r\n              authority{\r\n                name\r\n              }\r\n            }\r\n          }\r\n    }\r\n  }\r\n}","variables":{}}' | grep -Po '"id": *"\K[^"]*' | awk 'NR==1{print$1}' | xargs -I id curl --location --request GET 'https://api.staging.entur.io/sales/v1/offers/search/trip/trip-pattern/id'

to see an example of what I mean

@draperunner
Copy link
Contributor

You're right, something's wrong with the query from the SDK. I'm not sure exactly what. But it works with our v1 beta version. You could try npm install @entur/sdk@next and use that version. There are some breaking changes though, and more might come.

@AlfHou
Copy link
Author

AlfHou commented Jan 9, 2020

I guess this is the wrong place to ask. I am not really looking to use the SDK. I just want to call the API. Is there something wrong with the offers API? And is there somewhere else I should rather ask for help?

@draperunner
Copy link
Contributor

It's something in the GraphQL query that makes offers unable to fetch product information. It works with the query used in @entur/sdk v1.0-next, but not the one in v0 or in your curl. Try removing fields and see if you suddenly get an answer.

@AlfHou
Copy link
Author

AlfHou commented Jan 9, 2020

I tried removing some fields from the post to the /sales/v1/offers/search/graphql graphql endpoint and got my query down to this

 trip(
   from: {place: "NSR:StopPlace:548"
   }

   to: {
     place:"NSR:StopPlace:337"
   }
   dateTime: "2020-01-10T00:00:00+0100",
   numTripPatterns: 20,
   modes: [rail]
   
   
   
 )

#### Requested fields
 {
   tripPatterns {
     startTime
     duration
   }
 }

I managed to get the sales/v1/offers/search/trip/trip-pattern{id} to respond. However the offers array it returns is empty. I'll explore some more.

@draperunner draperunner added the enhancement New feature or request label Feb 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants