Skip to content

Commit

Permalink
fetch with native, because tauri can not use proxy in js
Browse files Browse the repository at this point in the history
  • Loading branch information
Poordeveloper committed May 3, 2023
1 parent 890bf61 commit 6c91278
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<h1 align="center">A ChatGPT App for all platforms</h1>

English / [简体中文](./docs/README-zh-CN.md) / [繁體中文](./docs/README-zh-TW.md) / [Deutsch](./docs/README-de-DE.md) / [Français](./docs/README-fr-FR.md) / [Italiano](./docs/README-it-IT.md) / [한국어](./docs/README-kr-KR.md)
<!-- English / [简体中文](./docs/README-zh-CN.md) / [繁體中文](./docs/README-zh-TW.md) / [Deutsch](./docs/README-de-DE.md) / [Français](./docs/README-fr-FR.md) / [Italiano](./docs/README-it-IT.md) / [한국어](./docs/README-kr-KR.md) -->

Built with <b>Rust</b> + <b>Tauri</b> + <b>Vue</b> + <b>Axum</b>

Expand Down Expand Up @@ -54,7 +54,7 @@ Timeout of OpenAI api request

# Run Android / iOS

Comming soom
Coming soom

# Thanks
- The initial frontend code was copied from [Chanzhaoyu/chatgpt-web](https://github.com/Chanzhaoyu/chatgpt-web)
Expand Down
7 changes: 1 addition & 6 deletions shared/src/gpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,13 @@ pub async fn fetch_usage(rng: DateRange) -> Result<String> {
"${url}/dashboard/billing/usage?start_date=${}&end_date=${}",
rng.start_date, rng.end_date
);
let client = if let Some(c) = build_proxy_client() {
c
} else {
reqwest::Client::new()
};
let client = build_proxy_client().unwrap_or(reqwest::Client::new());
let res = client
.get(url_usage)
.header("Authorization", format!("Bearer ${key}"))
.header("Content-Type", "application/json")
.send()
.await?;
res.status();
Ok(res.text().await?)
}

Expand Down
10 changes: 10 additions & 0 deletions shared/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ fn get_auth(url: &str) -> Option<(String, String)> {
None
}

pub async fn fetch(url: &str) -> crate::Result<String> {
Ok(build_proxy_client()
.unwrap_or(reqwest::Client::new())
.get(url)
.send()
.await?
.text()
.await?)
}

mod test {
#[test]
fn test_proxy() {
Expand Down
7 changes: 7 additions & 0 deletions src-tauri/src/app/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ pub fn set(key: String, value: String) {
pub fn get(key: String) -> String {
std::env::var(key).unwrap_or_default()
}

#[command]
pub async fn fetch(url: String) -> std::result::Result<String, String> {
Ok(shared::network::fetch(&url)
.await
.map_err(|x| x.to_string())?)
}
2 changes: 1 addition & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use app::cmd;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![cmd::call, cmd::set, cmd::get])
.invoke_handler(tauri::generate_handler![cmd::call, cmd::set, cmd::get, cmd::fetch])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
11 changes: 9 additions & 2 deletions src/components/common/PromptStore/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { usePromptStore } from '@/store'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { t } from '@/locales'
import { getLanguage } from '@/hooks/useLanguage'
import { isTauri, fetch as tauri_fetch } from '@/tauri'
interface DataProps {
renderKey: string
Expand Down Expand Up @@ -221,8 +222,14 @@ const exportPromptTemplate = () => {
const downloadPromptTemplate = async () => {
try {
importLoading.value = true
const response = await fetch(downloadURL.value)
const data = await response.text()
let data
if (isTauri) {
data = await tauri_fetch(downloadURL.value)
}
else {
const response = await fetch(downloadURL.value)
data = await response.text()
}
try {
const jsonData = JSON.parse(data)
if ('key' in jsonData[0] && 'value' in jsonData[0])
Expand Down
4 changes: 4 additions & 0 deletions src/tauri/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ export async function loadEnvFromLocalStorage() {
await invoke('set', { key, value: env[key] })
}

export async function fetch(url: string): Promise<string> {
return invoke('fetch', { url })
}

export default call

0 comments on commit 6c91278

Please sign in to comment.