forked from Soroban-Pulse/SorobanPulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch3.py
More file actions
93 lines (84 loc) · 3.12 KB
/
patch3.py
File metadata and controls
93 lines (84 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
with open('src/handlers.rs', 'r') as f:
c = f.read()
# ETag injection into offset path of get_events_by_contract
c = c.replace(
""" let count: i64 =
sqlx::query_scalar("SELECT COUNT(*) FROM events WHERE contract_id = $1")
.bind(&contract_id)
.fetch_one(&state.pool)
.await?;
state.contract_count_cache.insert(contract_id.clone(), count).await;
count
}
} else {
let count_str = format!("SELECT COUNT(*) FROM events {}", where_clause);
let mut cq = sqlx::query_scalar::<_, i64>(&count_str).bind(&contract_id);
if let Some(fl) = params.from_ledger {
cq = cq.bind(fl);
}
if let Some(tl) = params.to_ledger {
cq = cq.bind(tl);
}
cq.fetch_one(&state.read_pool).await?
};
let mut response = json!({""",
""" let count: i64 =
sqlx::query_scalar("SELECT COUNT(*) FROM events WHERE contract_id = $1")
.bind(&contract_id)
.fetch_one(&state.pool)
.await?;
state.contract_count_cache.insert(contract_id.clone(), count).await;
count
}
} else {
let count_str = format!("SELECT COUNT(*) FROM events {}", where_clause);
let mut cq = sqlx::query_scalar::<_, i64>(&count_str).bind(&contract_id);
if let Some(fl) = params.from_ledger {
cq = cq.bind(fl);
}
if let Some(tl) = params.to_ledger {
cq = cq.bind(tl);
}
cq.fetch_one(&state.read_pool).await?
};
let etag = rows.first().and_then(|row| {
let id: Option<uuid::Uuid> = row.try_get("id").ok();
let created_at: Option<chrono::DateTime<chrono::Utc>> = row.try_get("created_at").ok();
id.zip(created_at)
.map(|(id, ca)| compute_etag(&id, &ca, Some(total)))
});
if let Some(ref tag) = etag {
if let Some(inm) = headers.get("if-none-match").and_then(|v| v.to_str().ok()) {
if inm == tag {
let resp = axum::http::Response::builder()
.status(axum::http::StatusCode::NOT_MODIFIED)
.header("ETag", tag.as_str())
.header("Cache-Control", "no-cache")
.body(axum::body::Body::empty())
.unwrap();
return Ok(resp.into_response());
}
}
}
let mut response = json!({""")
# Modify return statement of offset path
c = c.replace(
""" if let Some(tl) = params.to_ledger {
response["to_ledger"] = json!(tl);
}
return Ok(Json(response));
}
""",
""" if let Some(tl) = params.to_ledger {
response["to_ledger"] = json!(tl);
}
let mut resp = Json(response).into_response();
if let Some(ref tag) = etag {
resp.headers_mut().insert("ETag", tag.parse().unwrap());
resp.headers_mut().insert("Cache-Control", "no-cache".parse().unwrap());
}
return Ok(resp);
}
""")
with open('src/handlers.rs', 'w') as f:
f.write(c)