Skip to content

Instantly share code, notes, and snippets.

@rednafi
Last active December 22, 2022 20:25
Show Gist options
  • Save rednafi/3e853ce3841b0567fd66d676c6c5858f to your computer and use it in GitHub Desktop.
Save rednafi/3e853ce3841b0567fd66d676c6c5858f to your computer and use it in GitHub Desktop.
Get provider attributes from GET orders/ endpoint
// Must update the USERNAME, PASSWORD, and ORDER_CODE variables.
const USERNAME = "<your-username>"; // Has to be an email.
const PASSWORD = "<your-password>";
const ORDER_CODE = "KI00000133"; // Collect this from zapier panel.
const ROOT_DOMAIN = "https://kiyatec.dendisoftware.com";
// Cache the token.
let config = { token: "" };
async function getToken() {
const url = `${ROOT_DOMAIN}/api/api-token-auth/`;
const headers = {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
};
// If the token is already cached, return it.
if (config.token !== "") {
config.log("Using cached token.");
return config.token;
}
// Make post request to the url and get the token.
const response = await fetch(url, {
method: "POST",
headers: headers,
body: `username=${USERNAME}&password=${PASSWORD}`,
});
// Throw error if the response is not ok.
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const payload = await response.json();
// Cache the token.
config.token = payload.token;
return config.token;
}
async function getProviderFromOrder() {
const url = `${ROOT_DOMAIN}/api/v1/orders?order_code=${ORDER_CODE}`;
const token = await getToken();
const headers = {
"Content-Type": "application/json",
Authorization: `Token ${token}`,
};
// Make get request to the url.
const response = await fetch(url, {
method: "GET",
headers: headers,
});
// Throw error if the response is not ok.
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const payload = await response.json();
return payload.results[0].provider;
}
// //Test. Comment out this section and run it with `node src.js`.
// let promise = getProviderFromOrder();
// promise.then((payload) => {
// console.log(payload);
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment