Add support for multiple HTTP methods in streamed_fetch and fetchNative
This commit is contained in:
@@ -333,6 +333,7 @@ async fn streamed_fetch(
|
|||||||
headers: String,
|
headers: String,
|
||||||
body: String,
|
body: String,
|
||||||
app: AppHandle,
|
app: AppHandle,
|
||||||
|
method: String,
|
||||||
) -> String {
|
) -> String {
|
||||||
//parse headers
|
//parse headers
|
||||||
let headers_json: Value = match serde_json::from_str(&headers) {
|
let headers_json: Value = match serde_json::from_str(&headers) {
|
||||||
@@ -357,14 +358,51 @@ async fn streamed_fetch(
|
|||||||
return format!(r#"{{"success":false,"body":"Invalid header JSON"}}"#);
|
return format!(r#"{{"success":false,"body":"Invalid header JSON"}}"#);
|
||||||
}
|
}
|
||||||
|
|
||||||
let body_decoded = general_purpose::STANDARD.decode(body.as_bytes()).unwrap();
|
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let response = client
|
let builder: reqwest::RequestBuilder;
|
||||||
|
if method == "POST" {
|
||||||
|
|
||||||
|
let body_decoded = general_purpose::STANDARD.decode(body.as_bytes()).unwrap();
|
||||||
|
|
||||||
|
builder = client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
.headers(headers)
|
.headers(headers)
|
||||||
.timeout(Duration::from_secs(240))
|
.timeout(Duration::from_secs(240))
|
||||||
.body(body_decoded)
|
.body(body_decoded)
|
||||||
|
}
|
||||||
|
else if method == "GET" {
|
||||||
|
builder = client
|
||||||
|
.get(&url)
|
||||||
|
.headers(headers)
|
||||||
|
.timeout(Duration::from_secs(240));
|
||||||
|
}
|
||||||
|
else if method == "PUT" {
|
||||||
|
|
||||||
|
let body_decoded = general_purpose::STANDARD.decode(body.as_bytes()).unwrap();
|
||||||
|
|
||||||
|
builder = client
|
||||||
|
.put(&url)
|
||||||
|
.headers(headers)
|
||||||
|
.timeout(Duration::from_secs(240))
|
||||||
|
.body(body_decoded)
|
||||||
|
}
|
||||||
|
else if method == "DELETE" {
|
||||||
|
|
||||||
|
let body_decoded = general_purpose::STANDARD.decode(body.as_bytes()).unwrap();
|
||||||
|
|
||||||
|
builder = client
|
||||||
|
.delete(&url)
|
||||||
|
.headers(headers)
|
||||||
|
.timeout(Duration::from_secs(240))
|
||||||
|
.body(body_decoded)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return format!(r#"{{"success":false, body:"Invalid method"}}"#);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
let response = builder
|
||||||
.send()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
|||||||
@@ -1811,7 +1811,7 @@ const pipeFetchLog = (fetchLogIndex: number, readableStream: ReadableStream<Uint
|
|||||||
export async function fetchNative(url:string, arg:{
|
export async function fetchNative(url:string, arg:{
|
||||||
body:string|Uint8Array|ArrayBuffer,
|
body:string|Uint8Array|ArrayBuffer,
|
||||||
headers?:{[key:string]:string},
|
headers?:{[key:string]:string},
|
||||||
method?:"POST",
|
method?:"POST"|"GET"|"PUT"|"DELETE",
|
||||||
signal?:AbortSignal,
|
signal?:AbortSignal,
|
||||||
useRisuTk?:boolean,
|
useRisuTk?:boolean,
|
||||||
chatId?:string
|
chatId?:string
|
||||||
@@ -1836,9 +1836,14 @@ export async function fetchNative(url:string, arg:{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
arg.method = arg.method ?? 'POST'
|
||||||
|
|
||||||
let headers = arg.headers ?? {}
|
let headers = arg.headers ?? {}
|
||||||
let realBody:Uint8Array
|
let realBody:Uint8Array
|
||||||
|
|
||||||
|
if(arg.method === 'GET' || arg.method === 'DELETE'){
|
||||||
|
realBody = new Uint8Array(0)
|
||||||
|
}
|
||||||
if(typeof arg.body === 'string'){
|
if(typeof arg.body === 'string'){
|
||||||
realBody = new TextEncoder().encode(arg.body)
|
realBody = new TextEncoder().encode(arg.body)
|
||||||
}
|
}
|
||||||
@@ -1861,7 +1866,7 @@ export async function fetchNative(url:string, arg:{
|
|||||||
success: true,
|
success: true,
|
||||||
url: url,
|
url: url,
|
||||||
resType: 'stream',
|
resType: 'stream',
|
||||||
chatId: arg.chatId
|
chatId: arg.chatId,
|
||||||
})
|
})
|
||||||
if(isTauri){
|
if(isTauri){
|
||||||
fetchIndex++
|
fetchIndex++
|
||||||
@@ -1885,6 +1890,7 @@ export async function fetchNative(url:string, arg:{
|
|||||||
url: url,
|
url: url,
|
||||||
headers: JSON.stringify(headers),
|
headers: JSON.stringify(headers),
|
||||||
body: Buffer.from(realBody).toString('base64'),
|
body: Buffer.from(realBody).toString('base64'),
|
||||||
|
method: arg.method
|
||||||
}).then((res) => {
|
}).then((res) => {
|
||||||
try {
|
try {
|
||||||
const parsedRes = JSON.parse(res as string)
|
const parsedRes = JSON.parse(res as string)
|
||||||
@@ -1973,7 +1979,7 @@ export async function fetchNative(url:string, arg:{
|
|||||||
"risu-url": encodeURIComponent(url),
|
"risu-url": encodeURIComponent(url),
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
},
|
},
|
||||||
method: "POST",
|
method: arg.method,
|
||||||
signal: arg.signal
|
signal: arg.signal
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -1990,7 +1996,7 @@ export async function fetchNative(url:string, arg:{
|
|||||||
body: realBody,
|
body: realBody,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
method: arg.method,
|
method: arg.method,
|
||||||
signal: arg.signal
|
signal: arg.signal,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user