UNPKG

@bridgerakol/samsung-smart-api

Version:

Node Module For Connecting Samsung Smartthings API

82 lines (77 loc) 3.14 kB
import axios from 'axios'; import { roomsPayload, roomList, roomsResponse } from './payloadType' export default class Rooms { BaseUrl: string; ReqHeader: Object constructor(BearerToken: string, BaseUrl: string) { this.BaseUrl = BaseUrl; this.ReqHeader = { "Authorization": BearerToken, "Content-Type": "application/json" } } getList = async (locationId: string) => { let url = `${this.BaseUrl}/locations/${locationId}/rooms` return axios.get(url, { headers: this.ReqHeader }) .then((res: any) => { let Rdata: roomList = res.data return { data: Rdata, status: true } }) .catch((err: any) => { return { data: err.response, status: false } }) } getBulkList = async (locations: string[]) => { // let url = `${this.BaseUrl}/locations/${locationId}/rooms`; let promises: any = []; locations.forEach(loc => { promises.push(this.getList(loc)) }) let result = await Promise.all(promises) return result; } getDetail = async (locationId: string, roomId: string) => { let url = `${this.BaseUrl}/locations/${locationId}/rooms/${roomId}` return axios.get(url, { headers: this.ReqHeader }) .then((res: any) => { let Rdata: roomsResponse = res.data return { data: Rdata, status: true } }) .catch((err: any) => { return { data: err.response, status: false } }) } create = async (locationId: string, payload: roomsPayload) => { let url = `${this.BaseUrl}/locations/${locationId}/rooms` return axios.post(url, payload, { headers: this.ReqHeader }) .then((res: any) => { let Rdata: roomsResponse = res.data return { data: Rdata, status: true } }) .catch((err: any) => { return { data: err.response, status: false } }) } remove = async (locationId: string, roomId: string) => { let url = `${this.BaseUrl}/locations/${locationId}/rooms/${roomId}` return axios.delete(url, { headers: this.ReqHeader }) .then((res: any) => { let Rdata: {} = res.data return { data: Rdata, status: true } }) .catch((err: any) => { return { data: err.response, status: false } }) } update = async (locationId: string, roomId: string, payload: roomsPayload) => { let url = `${this.BaseUrl}/locations/${locationId}/rooms/${roomId}` return axios.put(url, payload, { headers: this.ReqHeader }) .then((res: any) => { let Rdata: roomsResponse = res.data return { data: Rdata, status: true } }) .catch((err: any) => { return { data: err.response, status: false } }) } }