Created
January 11, 2018 17:22
-
-
Save paulosuzart/eeb273af93aad993225c0880a149e491 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import * as rp from "request"; | |
import { factory } from "../utils/logger"; | |
import { RequestResponse } from "request"; | |
const logger = factory.getLogger("squadify"); | |
export type Query = { | |
query: string, | |
variables: any | |
}; | |
/** | |
* Squadify client for typescript. | |
*/ | |
export class SquadifyClient { | |
static API_URL: string = process.env.SQUADIFY_API_URL || "https://api.squadify.io/graphql"; | |
_apikey: string; | |
_projectUrl: string; | |
_token: string; | |
constructor(apikey: string, projectUrl: string) { | |
this._apikey = apikey; | |
this._projectUrl = projectUrl; | |
} | |
/** | |
* Generate a token from the API. Make sure the provided apikey has the right permission. | |
*/ | |
private async getToken(): Promise<string> { | |
const query = { | |
query: `mutation GenToken($input:GenerateTokenInputType!) { | |
genToken(input:$input) | |
}`, | |
variables: { | |
"input": { | |
"uid": "peggy-watson" | |
} | |
} | |
} as Query; | |
const response = await this.callApi(query); | |
return response.data.genToken; | |
} | |
async token() { | |
if (!this._token) { | |
this._token = await this.getToken(); | |
} | |
return this._token; | |
} | |
/** | |
* Calls stuff in the Squadify API. For invoking queries agains the project, use {@SquadifyClient#call}. | |
* @see SquadifyClient#call | |
* @param query {Query} | |
*/ | |
callApi(query: Query): Promise<any> { | |
const headers = { "apikey": this._apikey }; | |
return new Promise((resolve, reject) => { | |
rp.post(SquadifyClient.API_URL, { headers, json: query }, (err, res, body) => { | |
if (err) { | |
logger.error({ msg: "Http client error for Squadify", data: err }); | |
reject(err); | |
} | |
if (res.statusCode != 200) { | |
logger.error({ data: body, msg: `Got response code ${res.statusCode} for query.` }); | |
console.log(body); | |
reject(new Error("Unable to query Squadify. Response not 200")); | |
} | |
logger.debug({ msg: "Got Squadify response", data: body }); | |
resolve(body); | |
}); | |
}); | |
} | |
/** | |
* Calls a project query or mutation. | |
* @param query {Query} | |
*/ | |
async call(query) { | |
const headers = { "authorization": `Bearer ${await this.token()}` }; | |
const doCall = (): Promise<RequestResponse> => { | |
return new Promise((resolve, reject) => { | |
rp.post(this._projectUrl, { headers, json: query }, (err, res, body) => { | |
resolve(res); | |
}); | |
}); | |
}; | |
let response = await doCall(); | |
if (response.statusCode == 403) { | |
this._token = undefined; | |
response = await doCall(); | |
} | |
return response.body; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment