160 lines
No EOL
4.9 KiB
TypeScript
160 lines
No EOL
4.9 KiB
TypeScript
import axios, { AxiosResponse } from "axios";
|
|
|
|
const axiosWithoutCredentials = axios.create({
|
|
baseURL: `http://localhost:8001`,
|
|
})
|
|
|
|
export class NetworkService {
|
|
static createAxiosInstance(ipAddress: string) {
|
|
return axios.create({
|
|
baseURL: `http://${ipAddress}:8001`,
|
|
});
|
|
}
|
|
|
|
static async checkIp(ipAddress: string, data: any): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.post('/ip/check_ip', data);
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
|
|
static async saveIp(ipAddress: string, data: any): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.post('/ip/apply_ip', data);
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
|
|
static async getHostname(): Promise<AxiosResponse>{
|
|
try{
|
|
const response = await axiosWithoutCredentials.get('/network/get_hostname')
|
|
return response
|
|
} catch (error: any){
|
|
return error.response
|
|
}
|
|
}
|
|
|
|
static async createUser(ipAddress: string, data: any): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.post('/auth/register', data);
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
|
|
static async login(ipAddress: string, data: any): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.post('/auth/login', data);
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
|
|
static async logout(ipAddress: string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.get('/auth/logout');
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
|
|
static async checkServerConf(ipAddress: string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.get('/auth/check_json');
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
|
|
static async whoami(ipAddress: string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.get('/auth/me');
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
|
|
static async scanUser(ipAddress: string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.get('/subnet/devices');
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
|
|
static async scanWifi(ipAddress: string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.get('/subnet/wifi');
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
static async addNote(ipAddress: string, address:string, note: string,type:string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.post(`/subnet/${type}/${address}/note`, {
|
|
note: note
|
|
});
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
static async editNote(ipAddress: string, address:string, note: string,type:string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.put(`/subnet/${type}/${address}/note`, {
|
|
note: note
|
|
});
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
static async deleteNote(ipAddress: string, address:string, type:string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.delete(`/subnet/${type}/${address}/note`);
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
static async statusNetwork(ipAddress: string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.get(`/subnet/wifi/status`);
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
static async connectNetwork(ipAddress: string, password:string, address: string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.post(`/subnet/wifi/connect`, {
|
|
ssid: address,
|
|
password: password,
|
|
});
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
static async disconnectNetwork(ipAddress: string, ssid: string): Promise<AxiosResponse> {
|
|
try {
|
|
const axiosInstance = this.createAxiosInstance(ipAddress);
|
|
return await axiosInstance.post(`/subnet/wifi/disconnect`, {
|
|
ssid: ssid,
|
|
});
|
|
} catch (error: any) {
|
|
return error.response;
|
|
}
|
|
}
|
|
} |