mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 14:34:54 +00:00
Added tuya space to community
This commit is contained in:
38
libs/common/src/integrations/tuya/tuya.service.ts
Normal file
38
libs/common/src/integrations/tuya/tuya.service.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
|
||||
@Injectable()
|
||||
export class TuyaService {
|
||||
private tuya: TuyaContext;
|
||||
|
||||
constructor(private readonly configService: ConfigService) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||
const tuyaEuUrl = this.configService.get<string>('tuya-config.TUYA_EU_URL');
|
||||
this.tuya = new TuyaContext({
|
||||
baseUrl: tuyaEuUrl,
|
||||
accessKey,
|
||||
secretKey,
|
||||
});
|
||||
}
|
||||
async createSpace({ name }: { name: string }) {
|
||||
const path = '/v2.0/cloud/space/creation';
|
||||
const body = { name };
|
||||
|
||||
const response = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
body,
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
return response.result as string;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
'Error creating space in Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user