Skip to content

Commit 2afc8dd

Browse files
authored
Merge pull request #219 from darseen/master
Auto JSON.stringify for POST Requests
2 parents cf17e0f + 7b2d050 commit 2afc8dd

File tree

10 files changed

+48
-49
lines changed

10 files changed

+48
-49
lines changed

README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ npm install amadeus --save
2121
To make your first API call, you will need to [register](https://developers.amadeus.com/register) for an Amadeus Developer Account and [set up your first application](https://developers.amadeus.com/my-apps).
2222

2323
```js
24-
var Amadeus = require('amadeus');
24+
const Amadeus = require('amadeus');
2525

26-
var amadeus = new Amadeus({
26+
const amadeus = new Amadeus({
2727
clientId: 'REPLACE_BY_YOUR_API_KEY',
2828
clientSecret: 'REPLACE_BY_YOUR_API_SECRET'
2929
});
@@ -50,7 +50,7 @@ The client can be initialized directly.
5050

5151
```js
5252
// Initialize using parameters
53-
var amadeus = new Amadeus({
53+
const amadeus = new Amadeus({
5454
clientId: 'REPLACE_BY_YOUR_API_KEY',
5555
clientSecret: 'REPLACE_BY_YOUR_API_SECRET'
5656
});
@@ -59,15 +59,15 @@ var amadeus = new Amadeus({
5959
Alternatively, it can be initialized without any parameters if the environment variables `AMADEUS_CLIENT_ID` and `AMADEUS_CLIENT_SECRET` are present.
6060

6161
```js
62-
var amadeus = new Amadeus();
62+
const amadeus = new Amadeus();
6363
```
6464

6565
Your credentials can be found on the [Amadeus dashboard](https://developers.amadeus.com/my-apps).
6666

6767
By default, the SDK environment is set to `test` environment. To switch to a `production` (pay-as-you-go) environment, please switch the hostname as follows:
6868

6969
```js
70-
var amadeus = new Amadeus({
70+
const amadeus = new Amadeus({
7171
hostname: 'production'
7272
});
7373
```
@@ -104,7 +104,7 @@ amadeus.client.get('/v2/reference-data/urls/checkin-links', { airlineCode: 'BA'
104104

105105
Or, with a `POST` using `.client.post` method:
106106
```js
107-
amadeus.client.post('/v1/shopping/flight-offers/pricing', JSON.stringify({ data }));
107+
amadeus.client.post('/v1/shopping/flight-offers/pricing', { data });
108108
```
109109

110110
## Promises
@@ -152,7 +152,7 @@ If a page is not available, the response will resolve to `null`.
152152
The SDK makes it easy to add your own logger that is compatible with the default `console`.
153153

154154
```js
155-
var amadeus = new Amadeus({
155+
const amadeus = new Amadeus({
156156
clientId: 'REPLACE_BY_YOUR_API_KEY',
157157
clientSecret: 'REPLACE_BY_YOUR_API_SECRET',
158158
logger: new MyConsole()
@@ -162,7 +162,7 @@ var amadeus = new Amadeus({
162162
Additionally, to enable more verbose logging, you can set the appropriate level on your own logger. The easiest way would be to enable debugging via a parameter during initialization, or using the `AMADEUS_LOG_LEVEL` environment variable. The available options are `silent` (default), `warn`, and `debug`.
163163

164164
```js
165-
var amadeus = new Amadeus({
165+
const amadeus = new Amadeus({
166166
clientId: 'REPLACE_BY_YOUR_API_KEY',
167167
clientSecret: 'REPLACE_BY_YOUR_API_SECRET',
168168
logLevel: 'debug'
@@ -204,7 +204,7 @@ amadeus.shopping.flightOffersSearch.get({
204204

205205
// Flight Offers Search POST
206206
// A full example can be found at https://github.com/amadeus4dev/amadeus-code-examples
207-
amadeus.shopping.flightOffersSearch.post(JSON.stringify(body))
207+
amadeus.shopping.flightOffersSearch.post(body)
208208

209209
// Flight Offers Price
210210
amadeus.shopping.flightOffersSearch.get({
@@ -214,12 +214,12 @@ amadeus.shopping.flightOffersSearch.get({
214214
adults: '1'
215215
}).then(function(response){
216216
return amadeus.shopping.flightOffers.pricing.post(
217-
JSON.stringify({
217+
{
218218
'data': {
219219
'type': 'flight-offers-pricing',
220220
'flightOffers': [response.data[0]]
221221
}
222-
})
222+
}
223223
)
224224
}).then(function(response){
225225
console.log(response.data);
@@ -229,19 +229,19 @@ amadeus.shopping.flightOffersSearch.get({
229229

230230
// Flight Offers Price with additional parameters
231231
// for example: check additional baggage options
232-
amadeus.shopping.flightOffers.pricing.post(JSON.stringify(body),{include: 'bags'})
232+
amadeus.shopping.flightOffers.pricing.post(body ,{include: 'bags'});
233233

234234
// Flight Create Orders
235235
// To book the flight-offer(s) returned by the Flight Offers Price
236236
// and create a flight-order with travelers' information.
237237
// A full example can be found at https://git.io/JtnYo
238238
amadeus.booking.flightOrders.post(
239-
JSON.stringify({
239+
{
240240
'type': 'flight-order',
241241
'flightOffers': [priced-offers],
242242
'travelers': []
243-
})
244-
)
243+
}
244+
);
245245

246246
// Retrieve flight order with ID 'XXX'. This ID comes from the
247247
// Flight Create Orders API, which is a temporary ID in test environment.
@@ -261,9 +261,9 @@ amadeus.shopping.flightOffersSearch.get({
261261
adults: '1'
262262
}).then(function(response){
263263
return amadeus.shopping.seatmaps.post(
264-
JSON.stringify({
264+
{
265265
'data': [response.data[0]]
266-
})
266+
}
267267
);
268268
}).then(function(response){
269269
console.log(response.data);
@@ -276,10 +276,10 @@ amadeus.shopping.seatmaps.get({
276276
});
277277

278278
// Flight Availabilities Search
279-
amadeus.shopping.availability.flightAvailabilities.post(JSON.stringify((body));
279+
amadeus.shopping.availability.flightAvailabilities.post(body);
280280

281281
// Branded Fares Upsell
282-
amadeus.shopping.flightOffers.upselling.post(JSON.stringify(body));
282+
amadeus.shopping.flightOffers.upselling.post(body);
283283

284284
// Flight Choice Prediction
285285
amadeus.shopping.flightOffersSearch.get({
@@ -288,9 +288,7 @@ amadeus.shopping.flightOffersSearch.get({
288288
departureDate: '2022-11-01',
289289
adults: '2'
290290
}).then(function(response){
291-
return amadeus.shopping.flightOffers.prediction.post(
292-
JSON.stringify(response)
293-
);
291+
return amadeus.shopping.flightOffers.prediction.post(response);
294292
}).then(function(response){
295293
console.log(response.data);
296294
}).catch(function(responseError){
@@ -384,26 +382,28 @@ amadeus.shopping.hotelOfferSearch('XXX').get()
384382

385383
// Hotel Booking API v2
386384
amadeus.booking.hotelOrders.post(
387-
JSON.stringfy({
385+
{
388386
'data': {
389387
'type': 'hotel-order',
390388
'guests': [],
391389
'travelAgent': {},
392390
'roomAssociations': [],
393391
'payment': {}
394-
}})
392+
}
393+
}
395394
)
396395

397396

398397
// Hotel Booking API v1
399398
amadeus.booking.hotelBookings.post(
400-
JSON.stringify({
399+
{
401400
'data': {
402401
'offerId': 'XXXX',
403402
'guests': [],
404403
'payments': [],
405404
'rooms': []
406-
}})
405+
}
406+
}
407407
)
408408

409409
// On-Demand Flight Status
@@ -510,13 +510,13 @@ amadeus.analytics.itineraryPriceMetrics.get({
510510

511511
//Cars & Transfers APIs
512512
// Transfer Search API: Search Transfer
513-
amadeus.shopping.transferOffers.post(JSON.stringify(body));
513+
amadeus.shopping.transferOffers.post(body);
514514

515515
// Transfer Book API: Book a transfer based on the offer id
516-
amadeus.ordering.transferOrders.post(JSON.stringify(body),offerId='2094123123');
516+
amadeus.ordering.transferOrders.post(body, offerId='2094123123');
517517

518518
// Transfer Management API: Cancel a transfer based on the order id & confirmation number
519-
amadeus.ordering.transferOrder('XXX').transfers.cancellation.post(JSON.stringify({}), confirmNbr='12345');
519+
amadeus.ordering.transferOrder('XXX').transfers.cancellation.post({}, confirmNbr='12345');
520520

521521
```
522522

spec/amadeus/client.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('Client', () => {
110110
// make an authenticated POST call
111111
client.post(path, params);
112112
// ensure Client.call() was called with the right parameters
113-
expect(call).toHaveBeenCalledWith('POST', path, params, 'token');
113+
expect(call).toHaveBeenCalledWith('POST', path, JSON.stringify(params), 'token');
114114
});
115115

116116
it('should work without params', () => {
@@ -119,7 +119,7 @@ describe('Client', () => {
119119
return { then: resolve => resolve('token') };
120120
}};
121121
client.post(path);
122-
expect(call).toHaveBeenCalledWith('POST', path, {}, 'token');
122+
expect(call).toHaveBeenCalledWith('POST', path, JSON.stringify({}), 'token');
123123
});
124124
});
125125

src/amadeus/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class Client {
7373
* @return {Promise.<Response,ResponseError>} a Promise
7474
*/
7575
post(path, params = {}) {
76-
return this.request('POST', path, params);
76+
const stringifiedParams = typeof params === 'string' ? params : JSON.stringify(params);
77+
return this.request('POST', path, stringifiedParams);
7778
}
7879

7980
/**

src/amadeus/namespaces/booking/hotel_bookings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ class HotelBookings {
2626
*
2727
* ```js
2828
* amadeus.booking.hotelBookings.post(
29-
* JSON.stringify({
29+
* {
3030
* 'data': {
3131
* 'offerId': 'XXXX',
3232
* 'guests': [],
3333
* 'payments': [],
3434
* 'rooms': []
35-
* }})
35+
* }}
3636
* )
3737
* ```
3838
*/

src/amadeus/namespaces/booking/hotel_orders.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ class HotelOrders {
2626
*
2727
* ```js
2828
* amadeus.booking.hotelOrders.post(
29-
* JSON.stringfy({
29+
* {
3030
* 'data': {
3131
* 'type': 'hotel-order',
3232
* 'guests': [],
3333
* 'travelAgent': {},
3434
* 'roomAssociations': [],
3535
* 'payment': {}
36-
* }})
37-
*)
36+
* }
37+
* })
3838
* ```
39+
3940
*/
4041
post(params = {}) {
41-
4242
return this.client.post('/v2/booking/hotel-orders', params);
4343
}
4444
}

src/amadeus/namespaces/ordering/transfer_orders.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TransferOrders {
2525
* To book the transfer-offer(s) suggested by transferOffers and create a transfer-order
2626
*
2727
* ```js
28-
* amadeus.ordering.transferOrders.post(body, '2094123123');;
28+
* amadeus.ordering.transferOrders.post(body, '2094123123');
2929
* ```
3030
*/
3131
post(body, offerId) {

src/amadeus/namespaces/ordering/transfer_orders/transfers/cancellation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* ```js
88
* let amadeus = new Amadeus();
9-
* amadeus.ordering.transferOrder('XXX').transfers.cancellation.post(JSON.stringify({}), '12345');;
9+
* amadeus.ordering.transferOrder('XXX').transfers.cancellation.post({}, '12345');;
1010
* ```
1111
*
1212
* @param {Client} client
@@ -24,7 +24,7 @@ class Cancellation {
2424
* To cancel a transfer order with ID 'XXX' and confirmation number '12345'
2525
*
2626
* ```js
27-
* amadeus.ordering.transferOrder('XXX').transfers.cancellation.post(JSON.stringify({}), '12345');;
27+
* amadeus.ordering.transferOrder('XXX').transfers.cancellation.post({}, '12345');;
2828
* ```
2929
*/
3030
post(body, confirmNbr) {

src/amadeus/namespaces/shopping/flight_offers/flight_choice_prediction.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ class FlightChoicePrediction {
3131
* departureDate: '2020-08-01',
3232
* adults: '2'
3333
* }).then(function(response){
34-
* return amadeus.shopping.flightOffers.prediction.post(
35-
* JSON.stringify(response)
36-
* );
34+
* return amadeus.shopping.flightOffers.prediction.post(response);
3735
* }).then(function(response){
3836
* console.log(response.data);
3937
* }).catch(function(responseError){

src/amadeus/namespaces/shopping/flight_offers_search.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class FlightOffersSearch {
5252
* To do a customized search with given options.
5353
*
5454
* ```js
55-
* amadeus.shopping.flightOffersSearch.post (JSON.stringify({
55+
* amadeus.shopping.flightOffersSearch.post({
5656
"currencyCode": "USD",
5757
"originDestinations": [
5858
{
@@ -114,7 +114,7 @@ class FlightOffersSearch {
114114
}
115115
}
116116
}
117-
}))
117+
});
118118
* ```
119119
*/
120120
post(params = {}) {

src/amadeus/namespaces/shopping/seatmaps.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ class Seatmaps {
5151
* departureDate: '2020-08-01'
5252
* }).then(function(response){
5353
* return amadeus.shopping.flightOffers.seatmaps.post(
54-
* JSON.stringify({
55-
* 'data': response.data
56-
* })
54+
* {
55+
* data: response.data
56+
* }
5757
* );
5858
* });
5959
* ```

0 commit comments

Comments
 (0)