Skip to content

Commit

Permalink
Merge pull request #219 from darseen/master
Browse files Browse the repository at this point in the history
Auto JSON.stringify for POST Requests
  • Loading branch information
tsolakoua authored Oct 9, 2024
2 parents cf17e0f + 7b2d050 commit 2afc8dd
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 49 deletions.
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ npm install amadeus --save
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).

```js
var Amadeus = require('amadeus');
const Amadeus = require('amadeus');

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

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

```js
var amadeus = new Amadeus();
const amadeus = new Amadeus();
```

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

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:

```js
var amadeus = new Amadeus({
const amadeus = new Amadeus({
hostname: 'production'
});
```
Expand Down Expand Up @@ -104,7 +104,7 @@ amadeus.client.get('/v2/reference-data/urls/checkin-links', { airlineCode: 'BA'

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

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

```js
var amadeus = new Amadeus({
const amadeus = new Amadeus({
clientId: 'REPLACE_BY_YOUR_API_KEY',
clientSecret: 'REPLACE_BY_YOUR_API_SECRET',
logger: new MyConsole()
Expand All @@ -162,7 +162,7 @@ var amadeus = new Amadeus({
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`.

```js
var amadeus = new Amadeus({
const amadeus = new Amadeus({
clientId: 'REPLACE_BY_YOUR_API_KEY',
clientSecret: 'REPLACE_BY_YOUR_API_SECRET',
logLevel: 'debug'
Expand Down Expand Up @@ -204,7 +204,7 @@ amadeus.shopping.flightOffersSearch.get({

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

// Flight Offers Price
amadeus.shopping.flightOffersSearch.get({
Expand All @@ -214,12 +214,12 @@ amadeus.shopping.flightOffersSearch.get({
adults: '1'
}).then(function(response){
return amadeus.shopping.flightOffers.pricing.post(
JSON.stringify({
{
'data': {
'type': 'flight-offers-pricing',
'flightOffers': [response.data[0]]
}
})
}
)
}).then(function(response){
console.log(response.data);
Expand All @@ -229,19 +229,19 @@ amadeus.shopping.flightOffersSearch.get({

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

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

// Retrieve flight order with ID 'XXX'. This ID comes from the
// Flight Create Orders API, which is a temporary ID in test environment.
Expand All @@ -261,9 +261,9 @@ amadeus.shopping.flightOffersSearch.get({
adults: '1'
}).then(function(response){
return amadeus.shopping.seatmaps.post(
JSON.stringify({
{
'data': [response.data[0]]
})
}
);
}).then(function(response){
console.log(response.data);
Expand All @@ -276,10 +276,10 @@ amadeus.shopping.seatmaps.get({
});

// Flight Availabilities Search
amadeus.shopping.availability.flightAvailabilities.post(JSON.stringify((body));
amadeus.shopping.availability.flightAvailabilities.post(body);

// Branded Fares Upsell
amadeus.shopping.flightOffers.upselling.post(JSON.stringify(body));
amadeus.shopping.flightOffers.upselling.post(body);

// Flight Choice Prediction
amadeus.shopping.flightOffersSearch.get({
Expand All @@ -288,9 +288,7 @@ amadeus.shopping.flightOffersSearch.get({
departureDate: '2022-11-01',
adults: '2'
}).then(function(response){
return amadeus.shopping.flightOffers.prediction.post(
JSON.stringify(response)
);
return amadeus.shopping.flightOffers.prediction.post(response);
}).then(function(response){
console.log(response.data);
}).catch(function(responseError){
Expand Down Expand Up @@ -384,26 +382,28 @@ amadeus.shopping.hotelOfferSearch('XXX').get()

// Hotel Booking API v2
amadeus.booking.hotelOrders.post(
JSON.stringfy({
{
'data': {
'type': 'hotel-order',
'guests': [],
'travelAgent': {},
'roomAssociations': [],
'payment': {}
}})
}
}
)


// Hotel Booking API v1
amadeus.booking.hotelBookings.post(
JSON.stringify({
{
'data': {
'offerId': 'XXXX',
'guests': [],
'payments': [],
'rooms': []
}})
}
}
)

// On-Demand Flight Status
Expand Down Expand Up @@ -510,13 +510,13 @@ amadeus.analytics.itineraryPriceMetrics.get({

//Cars & Transfers APIs
// Transfer Search API: Search Transfer
amadeus.shopping.transferOffers.post(JSON.stringify(body));
amadeus.shopping.transferOffers.post(body);

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

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

```
Expand Down
4 changes: 2 additions & 2 deletions spec/amadeus/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('Client', () => {
// make an authenticated POST call
client.post(path, params);
// ensure Client.call() was called with the right parameters
expect(call).toHaveBeenCalledWith('POST', path, params, 'token');
expect(call).toHaveBeenCalledWith('POST', path, JSON.stringify(params), 'token');
});

it('should work without params', () => {
Expand All @@ -119,7 +119,7 @@ describe('Client', () => {
return { then: resolve => resolve('token') };
}};
client.post(path);
expect(call).toHaveBeenCalledWith('POST', path, {}, 'token');
expect(call).toHaveBeenCalledWith('POST', path, JSON.stringify({}), 'token');
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/amadeus/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class Client {
* @return {Promise.<Response,ResponseError>} a Promise
*/
post(path, params = {}) {
return this.request('POST', path, params);
const stringifiedParams = typeof params === 'string' ? params : JSON.stringify(params);
return this.request('POST', path, stringifiedParams);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/amadeus/namespaces/booking/hotel_bookings.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class HotelBookings {
*
* ```js
* amadeus.booking.hotelBookings.post(
* JSON.stringify({
* {
* 'data': {
* 'offerId': 'XXXX',
* 'guests': [],
* 'payments': [],
* 'rooms': []
* }})
* }}
* )
* ```
*/
Expand Down
8 changes: 4 additions & 4 deletions src/amadeus/namespaces/booking/hotel_orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ class HotelOrders {
*
* ```js
* amadeus.booking.hotelOrders.post(
* JSON.stringfy({
* {
* 'data': {
* 'type': 'hotel-order',
* 'guests': [],
* 'travelAgent': {},
* 'roomAssociations': [],
* 'payment': {}
* }})
*)
* }
* })
* ```
*/
post(params = {}) {

return this.client.post('/v2/booking/hotel-orders', params);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/amadeus/namespaces/ordering/transfer_orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TransferOrders {
* To book the transfer-offer(s) suggested by transferOffers and create a transfer-order
*
* ```js
* amadeus.ordering.transferOrders.post(body, '2094123123');;
* amadeus.ordering.transferOrders.post(body, '2094123123');
* ```
*/
post(body, offerId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* ```js
* let amadeus = new Amadeus();
* amadeus.ordering.transferOrder('XXX').transfers.cancellation.post(JSON.stringify({}), '12345');;
* amadeus.ordering.transferOrder('XXX').transfers.cancellation.post({}, '12345');;
* ```
*
* @param {Client} client
Expand All @@ -24,7 +24,7 @@ class Cancellation {
* To cancel a transfer order with ID 'XXX' and confirmation number '12345'
*
* ```js
* amadeus.ordering.transferOrder('XXX').transfers.cancellation.post(JSON.stringify({}), '12345');;
* amadeus.ordering.transferOrder('XXX').transfers.cancellation.post({}, '12345');;
* ```
*/
post(body, confirmNbr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class FlightChoicePrediction {
* departureDate: '2020-08-01',
* adults: '2'
* }).then(function(response){
* return amadeus.shopping.flightOffers.prediction.post(
* JSON.stringify(response)
* );
* return amadeus.shopping.flightOffers.prediction.post(response);
* }).then(function(response){
* console.log(response.data);
* }).catch(function(responseError){
Expand Down
4 changes: 2 additions & 2 deletions src/amadeus/namespaces/shopping/flight_offers_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FlightOffersSearch {
* To do a customized search with given options.
*
* ```js
* amadeus.shopping.flightOffersSearch.post (JSON.stringify({
* amadeus.shopping.flightOffersSearch.post({
"currencyCode": "USD",
"originDestinations": [
{
Expand Down Expand Up @@ -114,7 +114,7 @@ class FlightOffersSearch {
}
}
}
}))
});
* ```
*/
post(params = {}) {
Expand Down
6 changes: 3 additions & 3 deletions src/amadeus/namespaces/shopping/seatmaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class Seatmaps {
* departureDate: '2020-08-01'
* }).then(function(response){
* return amadeus.shopping.flightOffers.seatmaps.post(
* JSON.stringify({
* 'data': response.data
* })
* {
* data: response.data
* }
* );
* });
* ```
Expand Down

0 comments on commit 2afc8dd

Please sign in to comment.