-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented rudimentary support for WebRequests (#635)
- Loading branch information
Showing
8 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
Submodule FreeShow
added at
1d1865
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<script lang="ts"> | ||
import { createEventDispatcher } from "svelte" | ||
import T from "../helpers/T.svelte" | ||
import CombinedInput from "../inputs/CombinedInput.svelte" | ||
import Dropdown from "../inputs/Dropdown.svelte" | ||
import TextInput from "../inputs/TextInput.svelte" | ||
import type { API_rest_command } from "./api" | ||
export let rest: API_rest_command | ||
/** | ||
* onChange Handler for URL component | ||
* This is a TextInput, so `e.target.value` | ||
* @param e onChange Event | ||
*/ | ||
function updateUrl(e) { | ||
// update url | ||
rest.url = e.target.value | ||
change() | ||
} | ||
/** | ||
* onChange Handler for Method component | ||
* This is a Dropdown, so `e.detail.name` | ||
* @param e onChange Event | ||
*/ | ||
function updateMethod(e) { | ||
// update method | ||
rest.method = e.detail.name | ||
change() | ||
} | ||
/** | ||
* onChange Handler for Payload component | ||
* This is a TextInput, so `e.target.value` | ||
* @param e onChange Event | ||
*/ | ||
function updatePayload(e) { | ||
// update payload | ||
rest.payload = e.target.value | ||
change() | ||
} | ||
/** | ||
* onChange Handler for ContentType component | ||
* This is a TextInput, so `e.target.value` | ||
* @param e onChange Event | ||
*/ | ||
function updateContentType(e) { | ||
// update ContentType | ||
rest.contentType = e.target.value | ||
change() | ||
} | ||
let dispatch = createEventDispatcher() | ||
function change() { | ||
dispatch("change", rest) | ||
} | ||
let dropdownInputs = [{ name: "GET" }, { name: "POST" }, { name: "PUT" }, { name: "DELETE" }] | ||
</script> | ||
|
||
<!-- URL --> | ||
<CombinedInput> | ||
<p><T id="inputs.url" /></p> | ||
<TextInput value={rest.url || ""} placeholder={"127.0.0.1"} on:change={(e) => updateUrl(e)} /> | ||
</CombinedInput> | ||
|
||
<!-- Method --> | ||
<CombinedInput> | ||
<p><T id="inputs.method" /></p> | ||
<Dropdown value={rest.method || "GET"} options={dropdownInputs} on:click={(e) => updateMethod(e)} /> | ||
</CombinedInput> | ||
|
||
<!-- ContentType --> | ||
<CombinedInput> | ||
<p><T id="inputs.contentType" /></p> | ||
<TextInput value={rest.contentType || ""} placeholder={"application/json"} on:change={(e) => updateContentType(e)} /> | ||
</CombinedInput> | ||
|
||
<!-- Body --> | ||
<CombinedInput> | ||
<p><T id="inputs.payload" /></p> | ||
<TextInput value={rest.payload || ""} placeholder={"{}"} on:change={(e) => updatePayload(e)} /> | ||
</CombinedInput> | ||
|
||
<!-- | ||
TODO: Better Namings | ||
? Send WebRequest | ||
- URL | ||
- Method | ||
- Content Type <-- einstellbar machen | ||
- Payload | ||
--> |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//import JZZ from "jzz" | ||
//import { toApp } from ".." | ||
import type { API_rest_command } from "./api" | ||
|
||
export async function sendRestCommand(data: API_rest_command) { | ||
|
||
// For Debugging: | ||
//console.log("Send REST (isch) Command:"); | ||
//console.log("URL:", data.url); | ||
//console.log("Method:", data.method); | ||
//console.log("Content-Type:", data.contentType); | ||
//console.log("Payload:", data.payload); | ||
|
||
// Prepare options: | ||
const options: RequestInit = {}; | ||
|
||
// if a Method is set, use the given Method, else use GET as default | ||
if (data.method) { | ||
options.method = data.method; | ||
} | ||
else { | ||
// on default use GET-Request | ||
options.method = "GET"; | ||
console.log("Using Default GET"); | ||
} | ||
|
||
// if Content Type is set, add the corresponding field in the Request-Header | ||
if (data.contentType) { | ||
options.headers = { 'Content-Type': data.contentType } | ||
} | ||
|
||
// If a Payload is provoded, add it to the requests body | ||
if (data.payload && (data.method === 'POST' || data.method === 'PUT')) { | ||
//options.body = JSON.stringify(data.payload); | ||
options.body = data.payload; | ||
} | ||
|
||
// Check if URL starts with HTTP or HTTPS, if not insert HTTP on default | ||
if (!(data.url.startsWith("http://") || data.url.startsWith("https://"))) { | ||
data.url = "http://" + data.url; | ||
} | ||
|
||
try { | ||
const response = await fetch(data.url, options); | ||
|
||
if (!response.ok) { | ||
//throw new Error(`HTTP error! status: ${response.status}`); | ||
console.error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
//let result : String = await response.json(); | ||
let result: String = await response.text(); | ||
console.log(result); | ||
|
||
//return result; | ||
|
||
} catch (error) { | ||
console.error('Request failed:', error); | ||
//throw error; | ||
} | ||
} |