forked from SmartThingsCommunity/smartthings-core-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathst-client.ts
More file actions
130 lines (118 loc) · 5.65 KB
/
st-client.ts
File metadata and controls
130 lines (118 loc) · 5.65 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import axios, { AxiosRequestConfig } from 'axios'
import { Authenticator } from './authenticator'
import { RESTClient, RESTClientConfig } from './rest-client'
import { AppsEndpoint } from './endpoint/apps'
import { CapabilitiesEndpoint } from './endpoint/capabilities'
import { DevicePreferencesEndpoint } from './endpoint/devicepreferences'
import { DeviceProfilesEndpoint } from './endpoint/deviceprofiles'
import { ChannelsEndpoint } from './endpoint/channels'
import { DevicesEndpoint } from './endpoint/devices'
import { DriversEndpoint } from './endpoint/drivers'
import { HistoryEndpoint } from './endpoint/history'
import { HubdevicesEndpoint } from './endpoint/hubdevices'
import { InstalledAppsEndpoint } from './endpoint/installedapps'
import { InvitesSchemaAppEndpoint } from './endpoint/invites-schemaApp'
import { ModesEndpoint } from './endpoint/modes'
import { LocationsEndpoint } from './endpoint/locations'
import { NotificationsEndpoint } from './endpoint/notifications'
import { OrganizationsEndpoint } from './endpoint/organizations'
import { PresentationEndpoint } from './endpoint/presentation'
import { RoomsEndpoint } from './endpoint/rooms'
import { RulesEndpoint } from './endpoint/rules'
import { ScenesEndpoint } from './endpoint/scenes'
import { SubscriptionsEndpoint } from './endpoint/subscriptions'
import { SchedulesEndpoint } from './endpoint/schedules'
import { SchemaEndpoint } from './endpoint/schema'
import { ServicesEndpoint } from './endpoint/services'
import { VirtualDevicesEndpoint } from './endpoint/virtualdevices'
import { SmartThingsURLProvider, globalSmartThingsURLProvider, HttpClientHeaders } from './endpoint-client'
export class SmartThingsClient extends RESTClient {
public readonly apps: AppsEndpoint
public readonly capabilities: CapabilitiesEndpoint
public readonly channels: ChannelsEndpoint
public readonly devicePreferences: DevicePreferencesEndpoint
public readonly deviceProfiles: DeviceProfilesEndpoint
public readonly devices: DevicesEndpoint
public readonly drivers: DriversEndpoint
public readonly history: HistoryEndpoint
public readonly hubdevices: HubdevicesEndpoint
public readonly installedApps: InstalledAppsEndpoint
public readonly invitesSchema: InvitesSchemaAppEndpoint
public readonly locations: LocationsEndpoint
public readonly modes: ModesEndpoint
public readonly notifications: NotificationsEndpoint
public readonly organizations: OrganizationsEndpoint
public readonly presentation: PresentationEndpoint
public readonly rooms: RoomsEndpoint
public readonly rules: RulesEndpoint
public readonly scenes: ScenesEndpoint
public readonly subscriptions: SubscriptionsEndpoint
public readonly schedules: SchedulesEndpoint
public readonly schema: SchemaEndpoint
public readonly services: ServicesEndpoint
public readonly virtualDevices: VirtualDevicesEndpoint
constructor(authenticator: Authenticator, config?: RESTClientConfig) {
super(authenticator, config)
this.apps = new AppsEndpoint(this.config)
this.capabilities = new CapabilitiesEndpoint(this.config)
this.channels = new ChannelsEndpoint(this.config)
this.devicePreferences = new DevicePreferencesEndpoint(this.config)
this.deviceProfiles = new DeviceProfilesEndpoint(this.config)
this.devices = new DevicesEndpoint(this.config)
this.drivers = new DriversEndpoint(this.config)
this.history = new HistoryEndpoint(this.config)
this.hubdevices = new HubdevicesEndpoint(this.config)
this.installedApps = new InstalledAppsEndpoint(this.config)
this.invitesSchema = new InvitesSchemaAppEndpoint(this.config)
this.locations = new LocationsEndpoint(this.config)
this.modes = new ModesEndpoint(this.config)
this.notifications = new NotificationsEndpoint(this.config)
this.organizations = new OrganizationsEndpoint(this.config)
this.presentation = new PresentationEndpoint(this.config)
this.rooms = new RoomsEndpoint(this.config)
this.rules = new RulesEndpoint(this.config)
this.scenes = new ScenesEndpoint(this.config)
this.subscriptions = new SubscriptionsEndpoint(this.config)
this.schedules = new SchedulesEndpoint(this.config)
this.schema = new SchemaEndpoint(this.config)
this.services = new ServicesEndpoint(this.config)
this.virtualDevices = new VirtualDevicesEndpoint(this.config)
}
public setLocation(id: string): SmartThingsClient {
this.config.locationId = id
return this
}
/**
* @param headers http headers to be merged with existing client headers
*/
public clone(headers?: HttpClientHeaders): SmartThingsClient {
const config: RESTClientConfig = { ...this.config, headers: { ...this.config.headers, ...headers } }
return new SmartThingsClient(this.config.authenticator, config)
}
}
export class SmartThingsOAuthClient {
private authURL: string
constructor(private clientId: string, private clientSecret: string,
private redirectUri: string, urlProvider?: SmartThingsURLProvider) {
this.authURL = urlProvider?.authURL || globalSmartThingsURLProvider.authURL
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async redeemCode(authCode: string): Promise<any> {
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${Buffer.from(this.clientId + ':' + this.clientSecret).toString('base64')}`,
'Accept': 'application/json',
}
const axiosConfig: AxiosRequestConfig = {
url: this.authURL,
method: 'POST',
headers,
data: `grant_type=authorization_code&code=${authCode}&client_id=${this.clientId}&redirect_uri=${this.redirectUri}`,
}
const response = await axios.request(axiosConfig)
if (response.status > 199 && response.status < 300) {
return response.data
}
throw Error(`error ${response.status} with message ${response.data}`)
}
}