Skip to content

Commit

Permalink
Add apk version updater
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Jun 16, 2024
1 parent d0c4b7f commit 8891c3e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ edition = { workspace = true }
version = { workspace = true }

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
reqwest = { workspace = true }
Expand Down
19 changes: 13 additions & 6 deletions apps/daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,26 @@ pub async fn main() {
println!("update oneinch swap tokenlist error: {}", err)
}
}
// update version

match version_client.update_ios_version().await {
Ok(version) => {
println!("ios version: {:?}", version)
println!("update ios version: {:?}", version)
}
Err(err) => {
println!("update ios version error: {}", err)
}
}

match version_client.update_apk_version().await {
Ok(version) => {
println!("update apk version: {:?}", version)
}
Err(err) => {
println!("ios version error: {}", err)
println!("update apk error: {}", err)
}
}

// update device
let result = device_updater.update().await;
match result {
match device_updater.update().await {
Ok(result) => {
println!("device updater result: {:?}", result)
}
Expand Down
73 changes: 59 additions & 14 deletions apps/daemon/src/version_updater.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
use primitives::Platform;
use serde::{Deserialize, Serialize};
use std::error::Error;
use storage::{database::DatabaseClient, models::Version};

pub struct Client {
database: DatabaseClient,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ITunesLookupResponse {
pub results: Vec<ITunesLoopUpResult>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ITunesLoopUpResult {
pub version: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubRepository {
pub name: String,
pub draft: bool,
pub prerelease: bool,
pub assets: Vec<GitHubRepositoryAsset>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubRepositoryAsset {
pub name: String,
}

impl Client {
pub fn new(database_url: &str) -> Self {
let database = DatabaseClient::new(database_url);
Expand All @@ -25,22 +49,43 @@ impl Client {
Ok(version)
}

pub async fn update_apk_version(&mut self) -> Result<Version, Box<dyn Error>> {
let version = self.get_github_apk_version().await?;
let version = Version {
id: 0,
platform: Platform::Android.as_str().to_string(),
production: version.clone(),
beta: version.clone(),
alpha: version.clone(),
};
let _ = self.database.set_version(version.clone())?;
Ok(version)
}

pub async fn get_app_store_version(&self) -> Result<String, Box<dyn Error>> {
let url = "https://itunes.apple.com/lookup?bundleId=com.gemwallet.ios";
let resp = reqwest::get(url).await?;
let json = resp.json::<serde_json::Value>().await?;
let version = json["results"][0]["version"].as_str().unwrap_or_default();
Ok(version.to_string())
let response = reqwest::get(url)
.await?
.json::<ITunesLookupResponse>()
.await?;
let result = response.results.first().expect("expect result");
Ok(result.version.to_string())
}

// pub async fn get_google_play_version(&self) -> Result<String, Box<dyn Error>> {
// let url = "https://play.google.com/store/apps/details?id=com.gemwallet.android";
// let resp = reqwest::get(url).await?;
// let body = resp.text().await?;
// let version = body.split("Current Version").collect::<Vec<&str>>()[1]
// .split("<div class=\"BgcNfc\">")[1]
// .split("</div>")[0]
// .trim();
// Ok(version.to_string())
// }
pub async fn get_github_apk_version(&self) -> Result<String, Box<dyn Error>> {
let url = "https://api.github.com/repos/gemwalletcom/gem-android/releases";
let client = reqwest::Client::builder().user_agent("").build()?;
let response = client
.get(url)
.send()
.await?
.json::<Vec<GitHubRepository>>()
.await?;
let results = response
.into_iter()
.filter(|x| !x.draft && !x.prerelease)
.collect::<Vec<_>>();
let result = results.first().expect("expect github repository");
Ok(result.name.clone())
}
}

0 comments on commit 8891c3e

Please sign in to comment.