mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-23 01:49:13 +01:00
gRPC Support (#20)
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
use log::{debug, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use sqlx::types::JsonValue;
|
||||
use tauri::{AppHandle, Manager, State};
|
||||
use tokio::sync::Mutex;
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
use crate::{is_dev, models};
|
||||
|
||||
@@ -16,6 +14,9 @@ pub enum AnalyticsResource {
|
||||
Dialog,
|
||||
Environment,
|
||||
Folder,
|
||||
GrpcConnection,
|
||||
GrpcMessage,
|
||||
GrpcRequest,
|
||||
HttpRequest,
|
||||
HttpResponse,
|
||||
KeyValue,
|
||||
@@ -31,6 +32,9 @@ impl AnalyticsResource {
|
||||
"CookieJar" => Some(AnalyticsResource::CookieJar),
|
||||
"Environment" => Some(AnalyticsResource::Environment),
|
||||
"Folder" => Some(AnalyticsResource::Folder),
|
||||
"GrpcConnection" => Some(AnalyticsResource::GrpcConnection),
|
||||
"GrpcMessage" => Some(AnalyticsResource::GrpcMessage),
|
||||
"GrpcRequest" => Some(AnalyticsResource::GrpcRequest),
|
||||
"HttpRequest" => Some(AnalyticsResource::HttpRequest),
|
||||
"HttpResponse" => Some(AnalyticsResource::HttpResponse),
|
||||
"KeyValue" => Some(AnalyticsResource::KeyValue),
|
||||
@@ -90,6 +94,9 @@ fn resource_name(resource: AnalyticsResource) -> &'static str {
|
||||
AnalyticsResource::Dialog => "dialog",
|
||||
AnalyticsResource::Environment => "environment",
|
||||
AnalyticsResource::Folder => "folder",
|
||||
AnalyticsResource::GrpcRequest => "grpc_request",
|
||||
AnalyticsResource::GrpcConnection => "grpc_connection",
|
||||
AnalyticsResource::GrpcMessage => "grpc_message",
|
||||
AnalyticsResource::HttpRequest => "http_request",
|
||||
AnalyticsResource::HttpResponse => "http_response",
|
||||
AnalyticsResource::KeyValue => "key_value",
|
||||
@@ -129,14 +136,13 @@ pub struct LaunchEventInfo {
|
||||
pub async fn track_launch_event(app_handle: &AppHandle) -> LaunchEventInfo {
|
||||
let namespace = "analytics";
|
||||
let last_tracked_version_key = "last_tracked_version";
|
||||
let db_instance: State<'_, Mutex<Pool<Sqlite>>> = app_handle.state();
|
||||
let pool = &*db_instance.lock().await;
|
||||
|
||||
let mut info = LaunchEventInfo::default();
|
||||
|
||||
info.num_launches = models::get_key_value_int(namespace, "num_launches", 0, pool).await + 1;
|
||||
info.num_launches =
|
||||
models::get_key_value_int(app_handle, namespace, "num_launches", 0).await + 1;
|
||||
info.previous_version =
|
||||
models::get_key_value_string(namespace, last_tracked_version_key, "", pool).await;
|
||||
models::get_key_value_string(app_handle, namespace, last_tracked_version_key, "").await;
|
||||
info.current_version = app_handle.package_info().version.to_string();
|
||||
|
||||
if info.previous_version.is_empty() {
|
||||
@@ -167,19 +173,18 @@ pub async fn track_launch_event(app_handle: &AppHandle) -> LaunchEventInfo {
|
||||
AnalyticsAction::Launch,
|
||||
Some(json!({ "num_launches": info.num_launches })),
|
||||
)
|
||||
.await;
|
||||
|
||||
.await;
|
||||
|
||||
// Update key values
|
||||
|
||||
models::set_key_value_string(
|
||||
app_handle,
|
||||
namespace,
|
||||
last_tracked_version_key,
|
||||
info.current_version.as_str(),
|
||||
pool,
|
||||
)
|
||||
.await;
|
||||
models::set_key_value_int(namespace, "num_launches", info.num_launches, pool).await;
|
||||
models::set_key_value_int(app_handle, namespace, "num_launches", info.num_launches).await;
|
||||
|
||||
info
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::fs::{create_dir_all, File};
|
||||
use std::fs;
|
||||
use std::fs::{create_dir_all, File};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
@@ -7,28 +7,26 @@ use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use base64::Engine;
|
||||
use http::{HeaderMap, HeaderName, HeaderValue, Method};
|
||||
use http::header::{ACCEPT, USER_AGENT};
|
||||
use http::{HeaderMap, HeaderName, HeaderValue, Method};
|
||||
use log::{error, info, warn};
|
||||
use reqwest::{multipart, Url};
|
||||
use reqwest::redirect::Policy;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use reqwest::{multipart, Url};
|
||||
use sqlx::types::{Json, JsonValue};
|
||||
use tauri::{AppHandle, Wry};
|
||||
use tauri::AppHandle;
|
||||
|
||||
use crate::{emit_side_effect, models, render, response_err};
|
||||
use crate::{models, render, response_err};
|
||||
|
||||
pub async fn send_http_request(
|
||||
app_handle: &AppHandle,
|
||||
request: models::HttpRequest,
|
||||
response: &models::HttpResponse,
|
||||
environment: Option<models::Environment>,
|
||||
cookie_jar: Option<models::CookieJar>,
|
||||
app_handle: &AppHandle<Wry>,
|
||||
pool: &Pool<Sqlite>,
|
||||
download_path: Option<PathBuf>,
|
||||
) -> Result<models::HttpResponse, String> {
|
||||
let environment_ref = environment.as_ref();
|
||||
let workspace = models::get_workspace(&request.workspace_id, pool)
|
||||
let workspace = models::get_workspace(app_handle, &request.workspace_id)
|
||||
.await
|
||||
.expect("Failed to get Workspace");
|
||||
|
||||
@@ -88,7 +86,7 @@ pub async fn send_http_request(
|
||||
let url = match Url::from_str(url_string.as_str()) {
|
||||
Ok(u) => u,
|
||||
Err(e) => {
|
||||
return response_err(response, e.to_string(), app_handle, pool).await;
|
||||
return response_err(response, e.to_string(), app_handle).await;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -293,7 +291,7 @@ pub async fn send_http_request(
|
||||
let sendable_req = match request_builder.build() {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
return response_err(response, e.to_string(), app_handle, pool).await;
|
||||
return response_err(response, e.to_string(), app_handle).await;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -362,12 +360,9 @@ pub async fn send_http_request(
|
||||
);
|
||||
}
|
||||
|
||||
response = models::update_response_if_id(&response, pool)
|
||||
response = models::update_response_if_id(app_handle, &response)
|
||||
.await
|
||||
.expect("Failed to update response");
|
||||
if !request.id.is_empty() {
|
||||
emit_side_effect(app_handle, "updated_model", &response);
|
||||
}
|
||||
|
||||
// Copy response to download path, if specified
|
||||
match (download_path, response.body_path.clone()) {
|
||||
@@ -397,18 +392,13 @@ pub async fn send_http_request(
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
cookie_jar.cookies = json_cookies;
|
||||
match models::upsert_cookie_jar(pool, &cookie_jar).await {
|
||||
Ok(updated_jar) => {
|
||||
emit_side_effect(app_handle, "updated_model", &updated_jar);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to update cookie jar: {}", e);
|
||||
}
|
||||
if let Err(e) = models::upsert_cookie_jar(&app_handle, &cookie_jar).await {
|
||||
error!("Failed to update cookie jar: {}", e);
|
||||
};
|
||||
}
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
Err(e) => response_err(response, e.to_string(), app_handle, pool).await,
|
||||
Err(e) => response_err(response, e.to_string(), app_handle).await,
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,8 @@
|
||||
use std::time::SystemTime;
|
||||
|
||||
use log::info;
|
||||
use tauri::{AppHandle, updater, Window, Wry};
|
||||
use tauri::api::dialog;
|
||||
use tauri::{updater, AppHandle, Window};
|
||||
|
||||
use crate::is_dev;
|
||||
|
||||
@@ -27,14 +27,17 @@ impl YaakUpdater {
|
||||
}
|
||||
pub async fn force_check(
|
||||
&mut self,
|
||||
app_handle: &AppHandle<Wry>,
|
||||
app_handle: &AppHandle,
|
||||
mode: UpdateMode,
|
||||
) -> Result<bool, updater::Error> {
|
||||
self.last_update_check = SystemTime::now();
|
||||
|
||||
let update_mode = get_update_mode_str(mode);
|
||||
let enabled = !is_dev();
|
||||
info!("Checking for updates mode={} enabled={}", update_mode, enabled);
|
||||
info!(
|
||||
"Checking for updates mode={} enabled={}",
|
||||
update_mode, enabled
|
||||
);
|
||||
|
||||
if !enabled {
|
||||
return Ok(false);
|
||||
@@ -89,10 +92,11 @@ impl YaakUpdater {
|
||||
}
|
||||
pub async fn check(
|
||||
&mut self,
|
||||
app_handle: &AppHandle<Wry>,
|
||||
app_handle: &AppHandle,
|
||||
mode: UpdateMode,
|
||||
) -> Result<bool, updater::Error> {
|
||||
let ignore_check = self.last_update_check.elapsed().unwrap().as_secs() < MAX_UPDATE_CHECK_SECONDS;
|
||||
let ignore_check =
|
||||
self.last_update_check.elapsed().unwrap().as_secs() < MAX_UPDATE_CHECK_SECONDS;
|
||||
if ignore_check {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user