Move shared API code to library project
This commit is contained in:
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -294,6 +294,7 @@ dependencies = [
|
|||||||
"rusqlite",
|
"rusqlite",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"shared_api_lib",
|
||||||
"tokio",
|
"tokio",
|
||||||
"warp",
|
"warp",
|
||||||
]
|
]
|
||||||
@@ -1012,6 +1013,14 @@ dependencies = [
|
|||||||
"digest",
|
"digest",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "shared_api_lib"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
|
"log",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "shlex"
|
name = "shlex"
|
||||||
version = "1.3.0"
|
version = "1.3.0"
|
||||||
@@ -1060,6 +1069,7 @@ dependencies = [
|
|||||||
"rusqlite",
|
"rusqlite",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"shared_api_lib",
|
||||||
"tokio",
|
"tokio",
|
||||||
"warp",
|
"warp",
|
||||||
]
|
]
|
||||||
|
|||||||
14
Cargo.toml
14
Cargo.toml
@@ -1,7 +1,15 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
members = [
|
members = ["src/electricity_api", "src/shared_api_lib", "src/solar_api"]
|
||||||
"src/electricity_api"
|
|
||||||
, "src/solar_api"]
|
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
|
chrono = "0.4.41"
|
||||||
|
clap = "4.5.46"
|
||||||
|
colog = "1.3.0"
|
||||||
|
log = "0.4.27"
|
||||||
|
rusqlite = "0.37.0"
|
||||||
|
serde = "1.0.219"
|
||||||
|
serde_json = "1.0.143"
|
||||||
|
shared_api_lib = { path = "../shared_api_lib" }
|
||||||
|
tokio = "1"
|
||||||
|
warp = "0.4"
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4.41"
|
chrono = { workspace = true }
|
||||||
clap = { version = "4.5.46", features = ["derive"] }
|
clap = { workspace = true, features = ["derive"] }
|
||||||
colog = "1.3.0"
|
colog = { workspace = true }
|
||||||
log = "0.4.27"
|
log = { workspace = true }
|
||||||
rusqlite = { version = "0.37.0", features = ["bundled"] }
|
rusqlite = { workspace = true, features = ["bundled"] }
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { workspace = true, features = ["derive"] }
|
||||||
serde_json = "1.0.143"
|
serde_json = { workspace = true }
|
||||||
tokio = { version = "1", features = ["full"] }
|
shared_api_lib = { path = "../shared_api_lib" }
|
||||||
warp = { version = "0.4", features = ["server"] }
|
tokio = { workspace = true, features = ["full"] }
|
||||||
|
warp = { workspace = true, features = ["server"] }
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
use chrono::NaiveDate;
|
|
||||||
|
|
||||||
pub fn try_parse_query_date(value: Option<&String>) -> Option<NaiveDate> {
|
|
||||||
match value {
|
|
||||||
Some(string_value) => match chrono::NaiveDate::parse_from_str(string_value, "%Y-%m-%d") {
|
|
||||||
Ok(date) => Some(date),
|
|
||||||
Err(error) => {
|
|
||||||
log::error!("Failed to parse date {} with error {}", string_value, error);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,6 @@ use warp::Filter;
|
|||||||
use warp::http::Response;
|
use warp::http::Response;
|
||||||
|
|
||||||
mod database;
|
mod database;
|
||||||
mod helpers;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@@ -55,7 +54,7 @@ async fn serve(configuration: &std::sync::Arc<CommandLineArgs>) {
|
|||||||
.body(String::from("Forbidden"));
|
.body(String::from("Forbidden"));
|
||||||
}
|
}
|
||||||
|
|
||||||
match helpers::try_parse_query_date(query.get("date")) {
|
match shared_api_lib::query_helpers::try_parse_query_date(query.get("date")) {
|
||||||
Some(date) => {
|
Some(date) => {
|
||||||
let json = get_day_power_json(&date, &configuration.database_path);
|
let json = get_day_power_json(&date, &configuration.database_path);
|
||||||
return Response::builder()
|
return Response::builder()
|
||||||
@@ -92,14 +91,16 @@ async fn serve(configuration: &std::sync::Arc<CommandLineArgs>) {
|
|||||||
.body(String::from("Forbidden"));
|
.body(String::from("Forbidden"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_start = helpers::try_parse_query_date(query.get("start"));
|
let maybe_start =
|
||||||
|
shared_api_lib::query_helpers::try_parse_query_date(query.get("start"));
|
||||||
if maybe_start.is_none() {
|
if maybe_start.is_none() {
|
||||||
return Response::builder()
|
return Response::builder()
|
||||||
.status(400)
|
.status(400)
|
||||||
.body(String::from("Unsupported \"start\" param in query."));
|
.body(String::from("Unsupported \"start\" param in query."));
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_stop = helpers::try_parse_query_date(query.get("stop"));
|
let maybe_stop =
|
||||||
|
shared_api_lib::query_helpers::try_parse_query_date(query.get("stop"));
|
||||||
if maybe_stop.is_none() {
|
if maybe_stop.is_none() {
|
||||||
return Response::builder()
|
return Response::builder()
|
||||||
.status(400)
|
.status(400)
|
||||||
|
|||||||
8
src/shared_api_lib/Cargo.toml
Normal file
8
src/shared_api_lib/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "shared_api_lib"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
chrono = { workspace = true }
|
||||||
|
log = { workspace = true }
|
||||||
2
src/shared_api_lib/src/lib.rs
Normal file
2
src/shared_api_lib/src/lib.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod query_helpers;
|
||||||
|
pub mod year_month;
|
||||||
@@ -4,12 +4,13 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4.41"
|
chrono = { workspace = true }
|
||||||
clap = { version = "4.5.46", features = ["derive"] }
|
clap = { workspace = true, features = ["derive"] }
|
||||||
colog = "1.3.0"
|
colog = { workspace = true }
|
||||||
log = "0.4.27"
|
log = { workspace = true }
|
||||||
rusqlite = { version = "0.37.0", features = ["bundled"] }
|
rusqlite = { workspace = true, features = ["bundled"] }
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { workspace = true, features = ["derive"] }
|
||||||
serde_json = "1.0.143"
|
serde_json = { workspace = true }
|
||||||
tokio = { version = "1", features = ["full"] }
|
shared_api_lib = { path = "../shared_api_lib" }
|
||||||
warp = { version = "0.4", features = ["server"] }
|
tokio = { workspace = true, features = ["full"] }
|
||||||
|
warp = { workspace = true, features = ["server"] }
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::year_month::YearMonth;
|
use shared_api_lib::year_month::YearMonth;
|
||||||
|
|
||||||
pub fn get_day_solar_entities(date: &chrono::NaiveDate, database_path: &str) -> DayEntities {
|
pub fn get_day_solar_entities(date: &chrono::NaiveDate, database_path: &str) -> DayEntities {
|
||||||
let mut result = DayEntities {
|
let mut result = DayEntities {
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
|
use chrono;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use warp::Filter;
|
use warp::Filter;
|
||||||
use warp::http::Response;
|
use warp::http::Response;
|
||||||
|
|
||||||
use crate::year_month::YearMonth;
|
|
||||||
|
|
||||||
mod database;
|
mod database;
|
||||||
mod query_helpers;
|
|
||||||
mod year_month;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@@ -38,7 +35,7 @@ async fn serve(configuration: &std::sync::Arc<CommandLineArgs>) {
|
|||||||
}))
|
}))
|
||||||
.map(
|
.map(
|
||||||
|query: HashMap<String, String>, configuration: std::sync::Arc<CommandLineArgs>| {
|
|query: HashMap<String, String>, configuration: std::sync::Arc<CommandLineArgs>| {
|
||||||
match query_helpers::try_parse_query_date(query.get("date")) {
|
match shared_api_lib::query_helpers::try_parse_query_date(query.get("date")) {
|
||||||
Some(date) => {
|
Some(date) => {
|
||||||
let json = get_day_solar_json(&date, &configuration.database_path);
|
let json = get_day_solar_json(&date, &configuration.database_path);
|
||||||
return Response::builder()
|
return Response::builder()
|
||||||
@@ -61,14 +58,16 @@ async fn serve(configuration: &std::sync::Arc<CommandLineArgs>) {
|
|||||||
}))
|
}))
|
||||||
.map(
|
.map(
|
||||||
|query: HashMap<String, String>, configuration: std::sync::Arc<CommandLineArgs>| {
|
|query: HashMap<String, String>, configuration: std::sync::Arc<CommandLineArgs>| {
|
||||||
let maybe_start = query_helpers::try_parse_query_date(query.get("start"));
|
let maybe_start =
|
||||||
|
shared_api_lib::query_helpers::try_parse_query_date(query.get("start"));
|
||||||
if maybe_start.is_none() {
|
if maybe_start.is_none() {
|
||||||
return Response::builder()
|
return Response::builder()
|
||||||
.status(400)
|
.status(400)
|
||||||
.body(String::from("Unsupported \"start\" param in query."));
|
.body(String::from("Unsupported \"start\" param in query."));
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_stop = query_helpers::try_parse_query_date(query.get("stop"));
|
let maybe_stop =
|
||||||
|
shared_api_lib::query_helpers::try_parse_query_date(query.get("stop"));
|
||||||
if maybe_stop.is_none() {
|
if maybe_stop.is_none() {
|
||||||
return Response::builder()
|
return Response::builder()
|
||||||
.status(400)
|
.status(400)
|
||||||
@@ -108,14 +107,16 @@ async fn serve(configuration: &std::sync::Arc<CommandLineArgs>) {
|
|||||||
}))
|
}))
|
||||||
.map(
|
.map(
|
||||||
|query: HashMap<String, String>, configuration: std::sync::Arc<CommandLineArgs>| {
|
|query: HashMap<String, String>, configuration: std::sync::Arc<CommandLineArgs>| {
|
||||||
let maybe_start = query_helpers::try_parse_query_month_year(query.get("start"));
|
let maybe_start =
|
||||||
|
shared_api_lib::query_helpers::try_parse_query_month_year(query.get("start"));
|
||||||
if maybe_start.is_none() {
|
if maybe_start.is_none() {
|
||||||
return Response::builder()
|
return Response::builder()
|
||||||
.status(400)
|
.status(400)
|
||||||
.body(String::from("Unsupported \"start\" param in query."));
|
.body(String::from("Unsupported \"start\" param in query."));
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_stop = query_helpers::try_parse_query_month_year(query.get("stop"));
|
let maybe_stop =
|
||||||
|
shared_api_lib::query_helpers::try_parse_query_month_year(query.get("stop"));
|
||||||
if maybe_stop.is_none() {
|
if maybe_stop.is_none() {
|
||||||
return Response::builder()
|
return Response::builder()
|
||||||
.status(400)
|
.status(400)
|
||||||
@@ -239,7 +240,11 @@ struct DaysResponseItem {
|
|||||||
zever_total_watts: i32,
|
zever_total_watts: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_months_solar_json(start: &YearMonth, stop: &YearMonth, database_path: &str) -> String {
|
fn get_months_solar_json(
|
||||||
|
start: &shared_api_lib::year_month::YearMonth,
|
||||||
|
stop: &shared_api_lib::year_month::YearMonth,
|
||||||
|
database_path: &str,
|
||||||
|
) -> String {
|
||||||
let summaries = database::get_month_solar_summaries(&start, &stop, &database_path);
|
let summaries = database::get_month_solar_summaries(&start, &stop, &database_path);
|
||||||
let mut response_model = MonthsResponse {
|
let mut response_model = MonthsResponse {
|
||||||
month_logs: Vec::new(),
|
month_logs: Vec::new(),
|
||||||
|
|||||||
Reference in New Issue
Block a user