| 70 | * Base for commands that need to use Rest API via the SmartThings Core SDK. |
| 71 | */ |
| 72 | export const apiCommand = async <T extends APICommandFlags>( |
| 73 | flags: T, |
| 74 | addAdditionalHeaders?: (stCommand: SmartThingsCommand<T>, headers: HttpClientHeaders) => void, |
| 75 | ): Promise<APICommand<T>> => { |
| 76 | const stCommand = await smartThingsCommand(flags) |
| 77 | |
| 78 | // The `|| undefined` at then end of this line is to normalize falsy values to `undefined`. |
| 79 | const token = (flags.token ?? stCommand.cliConfig.stringConfigValue('token')) || undefined |
| 80 | |
| 81 | // Calculate the environment and clientIdProvider. In old configs, the clientIdProvider had to |
| 82 | // be specified manually. Now we support specifying the environment, which determines the |
| 83 | // clientIdProvider. If both are specified, environment wins. If only one is specified, calculate |
| 84 | // the other based on it. If neither is specified, use the globalClientIdProvider. |
| 85 | const calculateEnvironment = (): [string, SmartThingsURLProvider] => { |
| 86 | const environment = (flags.environment ?? stCommand.cliConfig.stringConfigValue('environment')) || undefined |
| 87 | // first look for environment; then fall back to the old config style |
| 88 | if (environment) { |
| 89 | if (environment in urlProvidersByEnvironment) { |
| 90 | return [environment, urlProvidersByEnvironment[environment]] |
| 91 | } |
| 92 | return fatalError(`unknown environment: ${environment}`) |
| 93 | } |
| 94 | |
| 95 | const configClientIdProvider = stCommand.profile.clientIdProvider |
| 96 | if (configClientIdProvider) { |
| 97 | if (typeof configClientIdProvider !== 'object') { |
| 98 | stCommand.logger.error('ignoring invalid configClientIdProvider') |
| 99 | } else { |
| 100 | const clientIdProvider = configClientIdProvider as ClientIdProvider |
| 101 | // Look up the environment based on the clientIdProvider.baseURL. |
| 102 | const environment = Object.entries(urlProvidersByEnvironment) |
| 103 | .find(([, provider]) => provider.baseURL === clientIdProvider.baseURL)?.[0] |
| 104 | ?? 'unknown' |
| 105 | return [environment, clientIdProvider] |
| 106 | } |
| 107 | } |
| 108 | return ['global', globalClientIdProvider] |
| 109 | } |
| 110 | |
| 111 | const [environment, urlProvider] = calculateEnvironment() |
| 112 | const logger = coreSDKLoggerFromLog4JSLogger(log4js.getLogger('rest-client')) |
| 113 | |
| 114 | const buildHeaders = async (): Promise<HttpClientHeaders> => { |
| 115 | // eslint-disable-next-line @typescript-eslint/naming-convention |
| 116 | const headers: HttpClientHeaders = { 'User-Agent': userAgent } |
| 117 | |
| 118 | if (flags.language) { |
| 119 | if (flags.language !== 'NONE') { |
| 120 | headers['Accept-Language'] = flags.language |
| 121 | } |
| 122 | } else { |
| 123 | headers['Accept-Language'] = osLocale() |
| 124 | } |
| 125 | |
| 126 | return headers |
| 127 | } |
| 128 | const headers = await buildHeaders() |
| 129 | if (addAdditionalHeaders) { |