Skip to content

Commit c114ca6

Browse files
authored
Add support to Trip parser (#55)
* add support for trip purpose * fix pep errors * refactor name * update tests * update docs * update result docs * add methods to encode to base64 * update encoding * update docs * update method docs * update structure of encoding * update readme * minor fix * add from_file and from_base64 paths to test * add from_file and from_base64 paths to test * fixes in docs * test post methods for prediction and trip parser * refactor the method that constructs post body * add tests for encoding * update tests description * add patch with name update
1 parent dc55f04 commit c114ca6

File tree

14 files changed

+228
-2
lines changed

14 files changed

+228
-2
lines changed

README.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,18 @@ List of supported endpoints
279279
# AI Generated Photos
280280
amadeus.media.files.generated_photos.get(category='MOUNTAIN')
281281
282+
# Trip Parser
283+
# Encode to Base64 your booking confirmation file (.html, .eml, .pdf supported)
284+
response = amadeus.travel.trip_parser_jobs.post(amadeus.travel.from_file(path_to_file))
285+
# Alternatively you can use a Base64 encoded content directly
286+
response = amadeus.travel.trip_parser_jobs.post(amadeus.travel.from_base64(base64))
287+
# Or you can call the API with the JSON directly
288+
response = amadeus.travel.trip_parser_jobs.post(body)
289+
# Get the parsing status of the process by jobId
290+
amadeus.travel.trip_parser_jobs.status(response.data['id']).get()
291+
# Get the result of the process by jobId
292+
amadeus.travel.trip_parser_jobs.result(response.data['id']).get()
293+
282294
Development & Contributing
283295
--------------------------
284296

amadeus/mixins/encoder.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import base64
2+
3+
4+
# Encodes a file to Base64 format
5+
def encode_file_to_base64(file):
6+
with open(file, 'rb') as opened_file:
7+
return base64.b64encode(opened_file.read()).decode()

amadeus/namespaces/_travel.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
from amadeus.client.decorator import Decorator
22
from amadeus.travel._analytics import Analytics
33
from amadeus.travel._predictions import Predictions
4+
from amadeus.travel._trip_parser_jobs import TripParser
5+
from amadeus.travel._encoder import from_file, from_base64
46

57

68
class Travel(Decorator, object):
79
def __init__(self, client):
810
Decorator.__init__(self, client)
911
self.analytics = Analytics(client)
1012
self.predictions = Predictions(client)
13+
self.trip_parser_jobs = TripParser(client)
14+
15+
def from_file(self, file):
16+
return from_file(file)
17+
18+
def from_base64(self, base64):
19+
return from_base64(base64)

amadeus/shopping/flight_offers/_prediction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def post(self, body):
1212
amadeus.shopping.flight_offers.get(origin='MAD',
1313
destination='NYC',
1414
departureDate='2019-08-01'
15-
).body
15+
).result
1616
)
1717
1818
:rtype: amadeus.Response

amadeus/travel/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ._analytics import Analytics
22
from ._predictions import TripPurpose, FlightDelay
3+
from ._trip_parser_jobs import TripParser
34

4-
__all__ = ['Analytics', 'TripPurpose', 'FlightDelay']
5+
__all__ = ['Analytics', 'TripPurpose', 'FlightDelay', 'TripParser']

amadeus/travel/_encoder.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from amadeus.mixins.encoder import encode_file_to_base64
2+
3+
4+
# Encodes file to Base64 and constructs POST body
5+
def from_file(file):
6+
encoded_file = encode_file_to_base64(file)
7+
return __build_trip_parser_body(encoded_file)
8+
9+
10+
# Constructs POST body from Base64
11+
def from_base64(base64):
12+
return __build_trip_parser_body(base64)
13+
14+
15+
# Takes a Base64 and returns a dict
16+
def __build_trip_parser_body(base64):
17+
return {
18+
'data': {
19+
'type': 'trip-parser-job',
20+
'content': base64,
21+
},
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from amadeus.client.decorator import Decorator
2+
from amadeus.travel.trip_parser_jobs._status import TripParserStatus
3+
from amadeus.travel.trip_parser_jobs._result import TripParserResult
4+
5+
6+
class TripParser(Decorator, object):
7+
def __init__(self, client):
8+
Decorator.__init__(self, client)
9+
10+
def status(self, job_id):
11+
return TripParserStatus(self.client, job_id)
12+
13+
def result(self, job_id):
14+
return TripParserResult(self.client, job_id)
15+
16+
def post(self, body):
17+
'''
18+
Sends the request for the parsing with the
19+
booking details and input parameters.
20+
21+
.. code-block:: python
22+
23+
amadeus.travel.trip_parser_jobs.post(
24+
'{ "data": {
25+
"type": "trip-parser-job",
26+
"content": : "base64string" }}'
27+
28+
You can call our helper functions with your booking details
29+
in a file or the content encoded in Base64
30+
31+
.. code-block:: python
32+
amadeus.travel.trip_parser_jobs.post(amadeus.travel.from_file(path_to_file))
33+
amadeus.travel.trip_parser_jobs.post(amadeus.travel.from_base64(base64))
34+
35+
:rtype: amadeus.Response
36+
:raises amadeus.ResponseError: if the request could not be completed
37+
'''
38+
return self.client.post('/v2/travel/trip-parser-jobs', body)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from ._result import TripParserResult
2+
from ._status import TripParserStatus
3+
4+
__all__ = ['TripParserResult', 'TripParserStatus']
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from amadeus.client.decorator import Decorator
2+
3+
4+
class TripParserResult(Decorator, object):
5+
def __init__(self, client, job_id):
6+
Decorator.__init__(self, client)
7+
self.job_id = job_id
8+
9+
def get(self, **params):
10+
'''
11+
Returns the complete result of parsing as a aggregated view of Trip
12+
13+
.. code-block:: python
14+
15+
amadeus.travel.trip_parser_jobs.result('XXX').get
16+
17+
:rtype: amadeus.Response
18+
:raises amadeus.ResponseError: if the request could not be completed
19+
'''
20+
return self.client.get(
21+
'/v2/travel/trip-parser-jobs/{0}/result'.format(self.job_id),
22+
**params)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from amadeus.client.decorator import Decorator
2+
3+
4+
class TripParserStatus(Decorator, object):
5+
def __init__(self, client, job_id):
6+
Decorator.__init__(self, client)
7+
self.job_id = job_id
8+
9+
def get(self, **params):
10+
'''
11+
Returns the parsing status and the link to the result
12+
in case of successful parsing.
13+
14+
.. code-block:: python
15+
16+
amadeus.travel.trip_parser_jobs.status('XXX').get
17+
18+
:rtype: amadeus.Response
19+
:raises amadeus.ResponseError: if the request could not be completed
20+
'''
21+
return self.client.get(
22+
'/v2/travel/trip-parser-jobs/{0}'.format(self.job_id),
23+
**params)

0 commit comments

Comments
 (0)