-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.ts
97 lines (85 loc) · 2.3 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { PodioError } from "./lib/error.ts";
import { ClientRequest, HTTP_POST } from "./transport/request.ts";
import { PodioResponse } from "./transport/response.ts";
import { EventHandler } from "./lib/event.ts";
import { ClientIO } from "./client.d.ts";
import {
OAuthObject,
GrantType,
AuthDataRequest,
} from "./lib/oauth.d.ts";
const API_URL = "https://api.podio.com:443";
const DEFAULT_HEADERS = {
"Content-Type": "application/json",
"Accept": "application/json",
"User-Agent": `podio-ts`,
};
export class Client extends ClientRequest implements ClientIO {
protected clientId: string;
protected clientSecret: string;
constructor(clientId: string, clientSecret: string, options: any = {}) {
super(API_URL, DEFAULT_HEADERS);
this.clientId = clientId;
this.clientSecret = clientSecret;
this.configure(options);
}
/**
* Configure the client
*/
configure(options: any): Client {
const { store } = options;
// setup event handler
this.event = new EventHandler();
// setup store for session management
if (store) {
this.store = store;
}
return this;
}
/**
* Configure client with specific API credentials
*
* @param clientId
* @param clientSecret
*/
configureWith(clientId: string, clientSecret: string, options: any): Client {
this.clientId = clientId;
this.clientSecret = clientSecret;
return this.configure(
Object.assign({ store: this.store, headers: DEFAULT_HEADERS }, options),
);
}
/**
* Perform API authentication
*
* @param grantType
* @param authData
*/
async authenticate(
grantType: GrantType,
authData: AuthDataRequest,
): Promise<PodioError | OAuthObject | PodioResponse> {
if (this.store) {
const oauth = await this.store.get(grantType, authData);
if (!oauth.isInvalid && oauth.access_token) {
// check if not yet expired
return new Promise((resolve) => resolve(oauth));
}
}
return this.request(
HTTP_POST,
"/oauth/token",
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: {
grant_type: grantType,
client_id: this.clientId,
client_secret: this.clientSecret,
...authData,
},
},
);
}
}