11# Support Python 2 and 3 API calls without importing
22# a 3rd party library
3+
4+ import json
5+
36try :
47 from urllib .request import Request as HTTPRequest
58 from urllib .parse import urlencode
@@ -54,16 +57,6 @@ class Request(object):
5457 '''
5558
5659 def __init__ (self , options ):
57- self .http_request = None
58- self .__initialize_options (options )
59- self .__initialize_headers ()
60- self .__build_http_request ()
61-
62- # PRIVATE
63-
64- # Initializes the Request object with all the passed in params. These
65- # do not change once set
66- def __initialize_options (self , options ):
6760 self .host = options ['host' ]
6861 self .port = options ['port' ]
6962 self .ssl = options ['ssl' ]
@@ -77,13 +70,16 @@ def __initialize_options(self, options):
7770 self .app_id = options ['app_id' ]
7871 self .app_version = options ['app_version' ]
7972
80- # Initializes the basic headers
81- def __initialize_headers (self ):
8273 self .headers = {
8374 'User-Agent' : self .__build_user_agent (),
8475 'Accept' : 'application/json, application/vnd.amadeus+json'
8576 }
8677
78+ self .url = self .__build_url ()
79+ self .http_request = self .__build_http_request ()
80+
81+ # PRIVATE
82+
8783 # Determines the User Agent
8884 def __build_user_agent (self ):
8985 user_agent = 'amadeus-python/{0}' .format (self .client_version )
@@ -92,30 +88,31 @@ def __build_user_agent(self):
9288 user_agent += ' {0}/{1}' .format (self .app_id , self .app_version )
9389 return user_agent
9490
95- # Builds up a HTTP Request objectm if not akready set
91+ # Builds a HTTP Request object based on the path, params, and verb
9692 def __build_http_request (self ):
97- request = self .__request_for_verb ()
98- self .__add_post_data_header ()
99- self .__add_bearer_token_header ()
100- self .__apply_headers (request )
101- self .http_request = request
93+ # Requests token in case has not been set
94+ if (self .bearer_token is None ):
95+ self .headers ['Content-Type' ] = 'application/x-www-form-urlencoded'
96+ return HTTPRequest (self .url ,
97+ data = urlencode (self .params ).encode (),
98+ headers = self .headers )
10299
103- # Builds a HTTP Request object based on the path, params, and verb
104- def __request_for_verb (self ):
105- params = self ._encoded_params ().encode ()
106- path = self .__full_url ()
100+ # Adds the authentication header since the bearer token has been set
101+ self .headers ['Authorization' ] = self .bearer_token
107102
108103 if (self .verb == 'GET' ):
109- return HTTPRequest (path )
104+ return HTTPRequest (self . url , headers = self . headers )
110105 else :
111- return HTTPRequest (path , params )
106+ return HTTPRequest (self .url ,
107+ data = json .dumps (self .params ).encode (),
108+ headers = self .headers )
112109
113110 # Encodes the params before sending them
114111 def _encoded_params (self ):
115112 return self ._urlencode (self .params )
116113
117114 # Builds up the full URL based on the scheme, host, path, and params
118- def __full_url (self ):
115+ def __build_url (self ):
119116 full_url = '{0}://{1}' .format (self .scheme , self .host )
120117 if not self .__port_matches_scheme ():
121118 full_url = '{0}:{1}' .format (full_url , self .port )
@@ -128,20 +125,6 @@ def __port_matches_scheme(self):
128125 return ((self .ssl and self .port == 443 ) or
129126 (not self .ssl and self .port == 80 ))
130127
131- # Adds an extra header if the verb is POST
132- def __add_post_data_header (self ):
133- if (self .verb == 'POST' ):
134- self .headers ['Content-Type' ] = 'application/x-www-form-urlencoded'
135-
136- # Adds the authentication header if the bearer token has been set
137- def __add_bearer_token_header (self ):
138- if (self .bearer_token is not None ):
139- self .headers ['Authorization' ] = self .bearer_token
140-
141- # Applies all the headers to the HTTP Request object
142- def __apply_headers (self , http_request ):
143- http_request .headers = self .headers
144-
145128 # Helper method to prepare the parameter encoding
146129 def _urlencode (self , d ):
147130 return urlencode (self ._flatten_keys (d , '' , {}))
0 commit comments