-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactored and improved usability of dsn/auth types
- Loading branch information
Showing
7 changed files
with
155 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ mod macros; | |
mod auth; | ||
mod dsn; | ||
mod project_id; | ||
mod utils; | ||
pub mod protocol; | ||
|
||
pub use auth::*; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use chrono::{DateTime, Utc, TimeZone}; | ||
|
||
/// Converts a datetime object into a float timestamp. | ||
pub fn datetime_to_timestamp(dt: &DateTime<Utc>) -> f64 { | ||
if dt.timestamp_subsec_nanos() == 0 { | ||
dt.timestamp() as f64 | ||
} else { | ||
(dt.timestamp() as f64) + | ||
((dt.timestamp_subsec_micros() as f64) / 1_000_000f64) | ||
} | ||
} | ||
|
||
pub fn timestamp_to_datetime(ts: f64) -> DateTime<Utc> { | ||
let secs = ts as i64; | ||
let micros = (ts.fract() * 1_000_000f64) as u32; | ||
Utc.timestamp_opt(secs, micros * 1000).unwrap() | ||
} | ||
|
||
|
||
pub mod ts_seconds_float { | ||
use std::fmt; | ||
use serde::{ser, de}; | ||
use chrono::{DateTime, Utc, TimeZone}; | ||
|
||
use super::timestamp_to_datetime; | ||
|
||
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error> | ||
where D: de::Deserializer<'de> | ||
{ | ||
Ok(d.deserialize_any(SecondsTimestampVisitor) | ||
.map(|dt| dt.with_timezone(&Utc))?) | ||
} | ||
|
||
pub fn serialize<S>(dt: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error> | ||
where S: ser::Serializer | ||
{ | ||
if dt.timestamp_subsec_nanos() == 0 { | ||
serializer.serialize_i64(dt.timestamp()) | ||
} else { | ||
serializer.serialize_f64( | ||
(dt.timestamp() as f64) + | ||
((dt.timestamp_subsec_micros() as f64) / 1_000_000f64) | ||
) | ||
} | ||
} | ||
|
||
struct SecondsTimestampVisitor; | ||
|
||
impl<'de> de::Visitor<'de> for SecondsTimestampVisitor { | ||
type Value = DateTime<Utc>; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result | ||
{ | ||
write!(formatter, "a unix timestamp") | ||
} | ||
|
||
fn visit_f64<E>(self, value: f64) -> Result<DateTime<Utc>, E> | ||
where E: de::Error | ||
{ | ||
Ok(timestamp_to_datetime(value)) | ||
} | ||
|
||
fn visit_i64<E>(self, value: i64) -> Result<DateTime<Utc>, E> | ||
where E: de::Error | ||
{ | ||
Ok(Utc.timestamp_opt(value, 0).unwrap()) | ||
} | ||
|
||
fn visit_u64<E>(self, value: u64) -> Result<DateTime<Utc>, E> | ||
where E: de::Error | ||
{ | ||
Ok(Utc.timestamp_opt(value as i64, 0).unwrap()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
extern crate chrono; | ||
extern crate sentry_types; | ||
use chrono::{TimeZone, Utc}; | ||
use sentry_types::{Auth, Dsn, protocol}; | ||
|
||
|
||
#[test] | ||
fn test_auth_parsing() { | ||
let auth: Auth = "Sentry sentry_timestamp=1328055286.5, \ | ||
sentry_client=raven-python/42, \ | ||
sentry_version=6, \ | ||
sentry_key=public, \ | ||
sentry_secret=secret" | ||
.parse() | ||
.unwrap(); | ||
assert_eq!(auth.timestamp(), Some(Utc.ymd(2012, 2, 1).and_hms_milli(0, 14, 46, 500))); | ||
assert_eq!(auth.client_agent(), Some("raven-python/42")); | ||
assert_eq!(auth.version(), 6); | ||
assert_eq!(auth.public_key(), "public"); | ||
assert_eq!(auth.secret_key(), Some("secret")); | ||
|
||
assert_eq!( | ||
auth.to_string(), | ||
"Sentry sentry_key=public, \ | ||
sentry_version=6, \ | ||
sentry_timestamp=1328055286.5, \ | ||
sentry_client=raven-python/42, \ | ||
sentry_secret=secret" | ||
); | ||
} | ||
|
||
#[test] | ||
fn auth_to_dsn() { | ||
let url = "https://username:password@domain:8888/23"; | ||
let dsn = url.parse::<Dsn>().unwrap(); | ||
let auth = dsn.to_auth(Some("sentry-rust/1.0")); | ||
assert_eq!(auth.client_agent(), Some("sentry-rust/1.0")); | ||
assert_eq!(auth.version(), protocol::LATEST); | ||
assert_eq!(auth.public_key(), "username"); | ||
assert_eq!(auth.secret_key(), Some("password")); | ||
} |