Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
xvello committed Sep 6, 2023
1 parent 41ae7ed commit c301b84
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

#[derive(Debug, Deserialize, Serialize)]
pub struct CaptureRequest {
Expand Down
41 changes: 18 additions & 23 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::sync::Arc;
use anyhow::{anyhow, Result};
use bytes::Bytes;

use axum::{http::StatusCode, Json};
use axum::body::HttpBody;
use axum::{http::StatusCode, Json};
// TODO: stream this instead
use axum::extract::{Query, State};
use axum::http::HeaderMap;
Expand All @@ -15,10 +15,10 @@ use base64::Engine;
use serde_json::Value;

use crate::{
api::{CaptureResponse,CaptureResponseCode},
event::{RawEvent, EventFormData, EventQuery, ProcessedEvent},
api::{CaptureResponse, CaptureResponseCode},
event::{EventFormData, EventQuery, ProcessedEvent, RawEvent},
router, sink, token,
utils::uuid_v7
utils::uuid_v7,
};

pub async fn event(
Expand Down Expand Up @@ -81,14 +81,10 @@ pub async fn event(
pub fn process_single_event(event: &RawEvent, query: &EventQuery) -> Result<ProcessedEvent> {
let distinct_id = match &event.distinct_id {
Some(id) => id,
None => {
match event.properties.get("distinct_id").map(|v| v.as_str()) {
Some(Some(id)) => id,
_ => {
return Err(anyhow!("missing distinct_id"))
},
}
}
None => match event.properties.get("distinct_id").map(|v| v.as_str()) {
Some(Some(id)) => id,
_ => return Err(anyhow!("missing distinct_id")),
},
};

Ok(ProcessedEvent {
Expand All @@ -103,9 +99,7 @@ pub fn process_single_event(event: &RawEvent, query: &EventQuery) -> Result<Proc
})
}

pub fn extract_and_verify_token(
events: &[RawEvent]
) -> Result<String, String> {
pub fn extract_and_verify_token(events: &[RawEvent]) -> Result<String, String> {
let mut request_token: Option<String> = None;

// Collect the token from the batch, detect multiples to reject request
Expand All @@ -117,7 +111,7 @@ pub fn extract_and_verify_token(
_ => {
return Err("event with no token".into());
}
}
},
};
if let Some(token) = &request_token {
if !token.eq(&event_token) {
Expand All @@ -133,11 +127,10 @@ pub fn extract_and_verify_token(
request_token.ok_or("no token found in request".into())
}


pub async fn process_events(
sink: Arc<dyn sink::EventSink + Send + Sync>,
events: &[RawEvent],
query: &EventQuery
query: &EventQuery,
) -> Result<(), String> {
let mut distinct_tokens = HashSet::new();

Expand All @@ -161,7 +154,11 @@ pub async fn process_events(
return Err(String::from("Number of distinct tokens in batch > 1"));
}

let events: Vec<ProcessedEvent> = match events.iter().map(|e| process_single_event(e, query)).collect() {
let events: Vec<ProcessedEvent> = match events
.iter()
.map(|e| process_single_event(e, query))
.collect()
{
Err(_) => return Err(String::from("Failed to process all events")),
Ok(events) => events,
};
Expand All @@ -187,14 +184,12 @@ pub async fn process_events(
Ok(())
}



#[cfg(test)]
mod tests {
use std::collections::HashMap;
use serde_json::json;
use crate::capture::extract_and_verify_token;
use crate::event::RawEvent;
use serde_json::json;
use std::collections::HashMap;

#[tokio::test]
async fn all_events_have_same_token() {
Expand Down
2 changes: 1 addition & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mod tests {
use base64::Engine as _;
use bytes::Bytes;

use super::{RawEvent, EventQuery};
use super::{EventQuery, RawEvent};

#[test]
fn decode_bytes() {
Expand Down
6 changes: 3 additions & 3 deletions tests/django_compat.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use assert_json_diff::assert_json_eq;
use async_trait::async_trait;
use axum::http::StatusCode;
use axum_test_helper::TestClient;
Expand All @@ -9,11 +10,10 @@ use capture::router::router;
use capture::sink::EventSink;
use capture::time::TimeSource;
use serde::Deserialize;
use serde_json::{json, Value};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::sync::{Arc, Mutex};
use assert_json_diff::assert_json_eq;
use serde_json::{json, Value};

#[derive(Debug, Deserialize)]
struct RequestDump {
Expand Down Expand Up @@ -111,7 +111,7 @@ async fn it_matches_django_capture_behaviour() -> anyhow::Result<()> {
res.json().await
);
assert_eq!(sink.len(), case.output.len());
assert_json_eq!(json!(case.output),json!(sink.events()))
assert_json_eq!(json!(case.output), json!(sink.events()))
}
Ok(())
}

0 comments on commit c301b84

Please sign in to comment.