Skip to content

Commit c1825e6

Browse files
authored
Add proper user agent (#166)
1 parent 6b70ca4 commit c1825e6

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

server/src/banner.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,29 @@ pub mod version {
5050
use crossterm::style::Stylize;
5151
use semver::Version;
5252

53-
use crate::utils::github;
53+
use crate::utils::update;
5454

5555
pub fn print() {
5656
// print current version
57-
let current_version = current_version();
57+
let current = current();
5858
// not eprintln because if it is old release then time passed with be displayed beside it
5959
eprint!(
6060
"
6161
{} {} ",
6262
"Current Version:".to_string().blue().bold(),
63-
current_version
63+
current
6464
);
6565

6666
// check for latest release, if it cannot be fetched then print error as warn and return
67-
let latest_release = match github::get_latest() {
67+
let latest_release = match update::get_latest() {
6868
Ok(latest_release) => latest_release,
6969
Err(e) => {
7070
log::warn!("{}", e);
7171
return;
7272
}
7373
};
7474

75-
if latest_release.version > current_version {
75+
if latest_release.version > current {
7676
let time_since_latest_release = chrono::Utc::now() - latest_release.date;
7777
let time_since_latest_release = humanize_time(time_since_latest_release);
7878

@@ -91,9 +91,9 @@ pub mod version {
9191
}
9292
}
9393

94-
fn current_version() -> Version {
95-
let current_version = env!("VERGEN_BUILD_SEMVER");
96-
semver::Version::parse(current_version).expect("VERGEN_BUILD_SEMVER is always valid semver")
94+
pub fn current() -> Version {
95+
let current = env!("VERGEN_BUILD_SEMVER");
96+
semver::Version::parse(current).expect("VERGEN_BUILD_SEMVER is always valid semver")
9797
}
9898

9999
fn humanize_time(time_passed: Duration) -> String {

server/src/utils.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ pub fn hostname_unchecked() -> String {
127127
hostname::get().unwrap().into_string().unwrap()
128128
}
129129

130-
pub mod github {
130+
pub mod update {
131+
use crate::banner::version::current;
132+
use std::path::Path;
133+
131134
use anyhow::anyhow;
132135
use chrono::{DateTime, Utc};
133136

@@ -136,11 +139,32 @@ pub mod github {
136139
pub date: DateTime<Utc>,
137140
}
138141

142+
fn is_docker() -> String {
143+
if Path::new("/.dockerenv").exists() {
144+
return "Docker".to_string();
145+
}
146+
"Native".to_string()
147+
}
148+
149+
// User Agent for Github API call
150+
// Format: Parseable/<version> (OS; Platform)
151+
fn user_agent() -> String {
152+
let info = os_info::get();
153+
format!(
154+
"Parseable/{} ({}; {})",
155+
current(),
156+
info.os_type(),
157+
is_docker()
158+
)
159+
}
160+
139161
pub fn get_latest() -> Result<LatestRelease, anyhow::Error> {
140-
let json: serde_json::Value =
141-
ureq::get("https://api.github.com/repos/parseablehq/parseable/releases/latest")
142-
.call()?
143-
.into_json()?;
162+
let agent = ureq::builder().user_agent(user_agent().as_str()).build();
163+
164+
let json: serde_json::Value = agent
165+
.get("https://api.github.com/repos/parseablehq/parseable/releases/latest")
166+
.call()?
167+
.into_json()?;
144168

145169
let version = json["tag_name"]
146170
.as_str()

0 commit comments

Comments
 (0)