Created
October 9, 2022 23:26
-
-
Save anvilation/dce648c3ea16110ff06c0c26797075d2 to your computer and use it in GitHub Desktop.
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
import { request } from 'urllib'; | |
import * as https from 'https'; | |
import * as http from 'http'; | |
const httpsAgent = new https.Agent({ keepAlive: true }); | |
const agent = new http.Agent({ keepAlive: true }); | |
export class OTRestAPI { | |
user: string; | |
pass: string; | |
csurl: string; | |
otdsurl: string; | |
ticket: string | null = null; | |
constructor(user: string, pass: string, csurl: string, otdsurl: string) { | |
this.user = user; | |
this.pass = pass; | |
this.csurl = csurl; | |
this.otdsurl = otdsurl; | |
} | |
async login() { | |
try { | |
const url = `${this.otdsurl}/otdsws/v1/authentication/credentials`; | |
const body = { | |
user_name: this.user, | |
password: this.pass, | |
ticket_type: 'OTDSTICKET' | |
}; | |
let options: urllib.RequestOptions = { | |
method: 'POST', | |
maxRedirects: 10, | |
data: body, | |
contentType: 'json', | |
dataType: 'json', | |
httpsAgent: httpsAgent, | |
agent: agent | |
} | |
const req = await request(url, options); | |
this.ticket = req.data.ticket; | |
return Promise.resolve(); | |
} catch (error) { | |
console.error(`Error: OTRestAPI.login`); | |
console.error(error); | |
return Promise.reject(error); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment