A shared/src/api/macros.rs => shared/src/api/macros.rs +21 -0
@@ 0,0 1,21 @@
+/*
+ * This file is part of laurelin/shared
+ * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
+ *
+ * Licensed under GPL-3.0-only.
+ * See LICENSE for licensing information.
+ */
+
+macro_rules! extract_cookie {
+ ($resp:expr) => {{
+ let mut cookie = String::from("");
+ for c in $resp.cookies() {
+ if c.name() == "id" {
+ cookie = c.value().to_string();
+ }
+ }
+ cookie
+ }};
+}
+
+pub(super) use extract_cookie;
M shared/src/api/mod.rs => shared/src/api/mod.rs +2 -0
@@ 13,6 13,8 @@ use serde::{Deserialize, Serialize};
pub mod types;
pub mod user;
+mod macros;
+
#[derive(Debug, Deserialize, Serialize)]
pub struct APIError {
pub id: u16,
M shared/src/api/user/mod.rs => shared/src/api/user/mod.rs +3 -17
@@ 11,7 11,7 @@ use serde::{Deserialize, Serialize};
use crate::error::api::APIError;
-use super::types::User;
+use super::{macros::extract_cookie, types::User};
#[derive(Serialize)]
pub struct PostLogin {
@@ 44,16 44,9 @@ pub fn login(api_address: &str, email: &str, password: &str) -> ResponseLoginWra
.send()
.unwrap();
- let mut cookie = String::from("");
- for c in resp.cookies() {
- if c.name() == "id" {
- cookie = c.value().to_string();
- }
- }
-
ResponseLoginWrapper {
+ cookie: extract_cookie!(resp),
response: resp.json().unwrap(),
- cookie,
}
}
@@ 95,15 88,8 @@ pub fn register(
.send()
.unwrap();
- let mut cookie = String::from("");
- for c in resp.cookies() {
- if c.name() == "id" {
- cookie = c.value().to_string();
- }
- }
-
ResponseRegisterWrapper {
+ cookie: extract_cookie!(resp),
response: resp.json().unwrap(),
- cookie,
}
}