-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70af62e
commit eecce52
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* @flow */ | ||
|
||
import { getPartnerAttributionID, getSessionID } from "@paypal/sdk-client/src"; | ||
import { inlineMemoize, request } from "@krakenjs/belter/src"; | ||
import { ZalgoPromise } from "@krakenjs/zalgo-promise/src"; | ||
|
||
import { HEADERS } from "../constants/api"; | ||
|
||
type RestAPIParams = {| | ||
method?: string, | ||
url: string, | ||
data: Object, | ||
accessToken: ?string, | ||
|}; | ||
|
||
// TODO: for the fingerprint work we should swap this out for fetch support | ||
// we did not get the time for doing this during the initial project | ||
function callRestAPI({ | ||
accessToken, | ||
method, | ||
url, | ||
data, | ||
}: RestAPIParams): ZalgoPromise<Object> { | ||
const partnerAttributionID = getPartnerAttributionID() || ""; | ||
|
||
if (!accessToken) { | ||
throw new Error(`No access token passed to API request ${url}`); | ||
} | ||
|
||
const requestHeaders = { | ||
[HEADERS.AUTHORIZATION]: `Bearer ${accessToken}`, | ||
[HEADERS.CONTENT_TYPE]: `application/json`, | ||
[HEADERS.PARTNER_ATTRIBUTION_ID]: partnerAttributionID, | ||
[HEADERS.CLIENT_METADATA_ID]: getSessionID(), | ||
}; | ||
|
||
return request({ | ||
method, | ||
url, | ||
headers: requestHeaders, | ||
json: data, | ||
}).then(({ status, body, headers: responseHeaders }) => { | ||
if (status >= 300) { | ||
const error = new Error( | ||
`${url} returned status ${status}\n\n${JSON.stringify(body)}` | ||
); | ||
|
||
// $FlowFixMe | ||
error.response = { status, headers: responseHeaders, body }; | ||
|
||
throw error; | ||
} | ||
|
||
return body; | ||
}); | ||
} | ||
|
||
export function callMemoizedRestAPI({ | ||
accessToken, | ||
method, | ||
url, | ||
data, | ||
}: RestAPIParams): ZalgoPromise<Object> { | ||
return inlineMemoize( | ||
callMemoizedRestAPI, | ||
() => callRestAPI({ accessToken, method, url, data }), | ||
[accessToken, method, url, JSON.stringify(data)] | ||
); | ||
} |