-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Included Instance run and change proxy
* Missing API call to create instance from Profile * Missing button to delete proxy and service in one go
- Loading branch information
Showing
6 changed files
with
148 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,133 @@ | ||
import { InteractionOptions, NetworkOptions } from "../../constants/globals"; | ||
import { Instance, InstanceProxyService } from "../../recoil/atoms/instances"; | ||
import { Service } from "../../recoil/atoms/services"; | ||
|
||
export const fetchProxy = async (instance: Instance) => { | ||
await new Promise((resolve) => setTimeout(resolve, 800)); | ||
let services; | ||
|
||
let mockProxy = { | ||
id: "", | ||
port: 2, | ||
status: "stopped", | ||
service: { | ||
id: "", | ||
name: "CoAP", | ||
network: NetworkOptions[0], | ||
interaction: InteractionOptions[0], | ||
host: "localhost", | ||
port: 2022, | ||
}, | ||
}; | ||
|
||
services = [mockProxy, mockProxy, mockProxy]; | ||
|
||
return services; | ||
export const fetchProxy = async (host: string) => { | ||
let response = []; | ||
try { | ||
response = await fetch("http://" + host + "/api/proxies/") | ||
.then((response) => response.json()) | ||
// Map the content of the response | ||
.then((data) => | ||
data.map((element: any) => { | ||
if ("service" in element) { | ||
let service = element["service"]; | ||
|
||
// Parse the network and the interaction | ||
if ("network" in service) | ||
service["network"] = NetworkOptions.find( | ||
(x) => x.value === service["network"] | ||
); | ||
if ("interaction" in service) | ||
service["interaction"] = InteractionOptions.find( | ||
(x) => x.value === service["interaction"] | ||
); | ||
|
||
// Re-assign the service | ||
element["service"] = service; | ||
} | ||
|
||
return element; | ||
}) | ||
); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
|
||
return response; | ||
}; | ||
|
||
export const patchService = async (id: number, service: Service) => { | ||
await new Promise((resolve) => setTimeout(resolve, 800)); | ||
let servs; | ||
export const patchService = async (host: string, service: Service) => { | ||
try { | ||
const response = await fetch( | ||
"http://" + host + "/api/services/" + service.id + "/", | ||
{ | ||
method: "POST", | ||
body: JSON.stringify({ | ||
name: service.name, | ||
port: service.port, | ||
host: service.host, | ||
}), | ||
headers: { | ||
"Content-type": "application/json; charset=UTF-8", | ||
}, | ||
} | ||
) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
// Parse the network and the interaction | ||
if ("network" in data) | ||
data["network"] = NetworkOptions.find( | ||
(x) => x.value === data["network"] | ||
); | ||
|
||
if ("interaction" in data) | ||
data["interaction"] = InteractionOptions.find( | ||
(x) => x.value === data["interaction"] | ||
); | ||
|
||
return { | ||
...service, | ||
...data, | ||
}; | ||
}); | ||
|
||
return response; | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
|
||
return service; | ||
}; | ||
|
||
export const patchProxy = async (id: number, proxy: InstanceProxyService) => { | ||
await new Promise((resolve) => setTimeout(resolve, 800)); | ||
let servs; | ||
|
||
return proxy; | ||
export const changeProxyPort = async ( | ||
host: string, | ||
proxyId: string, | ||
port: number | ||
) => { | ||
try { | ||
await fetch("http://" + host + "/api/proxies/" + proxyId + "/port", { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
port: port, | ||
}), | ||
headers: { | ||
"Content-type": "application/json; charset=UTF-8", | ||
}, | ||
}) | ||
.then((response) => response.json()) | ||
.catch((err) => { | ||
console.log(err.message); | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
export const changeProxyStatus = async ( | ||
instanceId: number, | ||
proxyId: string, | ||
status: string | ||
host: string, | ||
id: string, | ||
status: string, | ||
changeStatus: (status: string) => any | ||
) => { | ||
await new Promise((resolve) => setTimeout(resolve, 800)); | ||
return status; | ||
try { | ||
await fetch("http://" + host + "/api/proxies/" + id + "/status", { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
status: status, | ||
}), | ||
headers: { | ||
"Content-type": "application/json; charset=UTF-8", | ||
}, | ||
}) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
if (["running", "stopped"].includes(data.status)) | ||
changeStatus(data.status); | ||
}) | ||
.catch((err) => { | ||
console.log(err.message); | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters