Skip to content

Commit

Permalink
Merge pull request #10 from abhi3700/users
Browse files Browse the repository at this point in the history
Add Users (with tests) module
  • Loading branch information
abhi3700 authored Nov 2, 2024
2 parents 40fafc3 + 2c18f4c commit 7b55ec8
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 26 deletions.
109 changes: 109 additions & 0 deletions api-requests/users.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
@host=https://dummyjson.com/users

###
# @name GetAllUsers
GET {{host}}
Accept: application/json
Content-Type: application/json

###
# @name LoginUserGetTokens
POST {{host}}/login
Accept: application/json
Content-Type: application/json

{
"username": "emilys",
"password": "emilyspass",
"expiresInMins": 30
}

###
# @name GetCurrentAuthenticatedUser
@YOUR_ACCESS_TOKEN={{LoginUserGetTokens.response.body.accessToken}}
GET {{host}}/me
Accept: application/json
Content-Type: application/json
Authorization: Bearer {{YOUR_ACCESS_TOKEN}}

###
# @name GetUserById
@id=1
GET {{host}}/{{id}}
Accept: application/json
Content-Type: application/json

###
# @name SearchUsers
GET {{host}}/search?q=john
Accept: application/json
Content-Type: application/json

###
# @name FilterUsers
GET {{host}}/filter?key=hair.color&value=Brown
Accept: application/json
Content-Type: application/json

###
# @name LimitSkipUsers
GET {{host}}?limit=3&skip=10&select=firstName,age
Accept: application/json
Content-Type: application/json

###
# @name SortUsers
GET {{host}}/?sortBy=age&order=desc
Accept: application/json
Content-Type: application/json

###
# @name GetUserCartsByUserId
@id=6
GET {{host}}/{{id}}/carts
Accept: application/json
Content-Type: application/json

###
# @name GetUserPostsByUserId
@id=6
GET {{host}}/{{id}}/posts
Accept: application/json
Content-Type: application/json

###
# @name GetUserTodosByUserId
@id=6
GET {{host}}/{{id}}/todos
Accept: application/json
Content-Type: application/json

###
# @name AddUser
POST {{host}}/add
Accept: application/json
Content-Type: application/json

{
"firstName": "John",
"lastName": "Doe",
"age": 25
}

###
# @name UpdateUser
PUT {{host}}/{{id}}
Accept: application/json
Content-Type: application/json

{
"lastName": "Owais",
"age": 27
}

###
# @name DeleteUser
@id=6
DELETE {{host}}/{{id}}
Accept: application/json
Content-Type: application/json
50 changes: 27 additions & 23 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ static AUTH_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/auth", API_BASE_UR
pub struct LoginRequest {
pub username: String,
pub password: String,
/// Expiration time in minutes
#[serde(rename = "expiresInMins")]
pub expires_in_mins: u32,
pub expires_in_mins: Option<u32>,
}

/// Login response
Expand All @@ -41,35 +40,40 @@ pub struct LoginResponse {
pub refresh_token: String,
}

#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct User {
pub id: u32,
#[serde(flatten)]
pub other_fields: AddUserPayload,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct AddUserPayload {
#[serde(rename = "firstName")]
pub first_name: String,
pub first_name: Option<String>,
#[serde(rename = "lastName")]
pub last_name: String,
pub last_name: Option<String>,
#[serde(rename = "maidenName")]
pub maiden_name: String,
pub age: u8,
pub gender: String,
pub email: String,
pub phone: String,
pub username: String,
pub password: String,
pub maiden_name: Option<String>,
pub age: Option<u8>,
pub gender: Option<String>,
pub email: Option<String>,
pub phone: Option<String>,
pub username: Option<String>,
pub password: Option<String>,
#[serde(rename = "birthDate")]
pub birth_date: String,
pub image: String,
pub birth_date: Option<String>,
pub image: Option<String>,
#[serde(rename = "bloodGroup")]
pub blood_group: String,
pub height: f32,
pub weight: f32,
pub blood_group: Option<String>,
pub height: Option<f32>,
pub weight: Option<f32>,
#[serde(rename = "eyeColor")]
pub eye_color: String,
pub hair: Hair,
// TODO: Other fields
pub eye_color: Option<String>,
pub hair: Option<Hair>,
// TODO: Add other fields
}

#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct Hair {
pub color: String,
#[serde(rename = "type")]
Expand All @@ -91,7 +95,7 @@ impl DummyJsonClient {
&self,
username: &str,
password: &str,
expires_in_mins: u32,
expires_in_mins: Option<u32>,
) -> Result<LoginResponse, reqwest::Error> {
let payload: LoginRequest = LoginRequest {
username: username.to_string(),
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod products;
mod quotes;
mod recipes;
mod todos;
mod users;

pub use auth::*;
pub use carts::*;
Expand All @@ -16,6 +17,7 @@ pub use quotes::*;
pub use recipes::*;
use reqwest::Client;
pub use todos::*;
pub use users::*;

const API_BASE_URL: &str = "https://dummyjson.com";

Expand Down
Loading

0 comments on commit 7b55ec8

Please sign in to comment.