Skip to content

Commit

Permalink
Merge pull request #2 from abhi3700/todos
Browse files Browse the repository at this point in the history
Add todos module & its tests
  • Loading branch information
abhi3700 authored Oct 30, 2024
2 parents 642fd39 + b4c3f68 commit 755c999
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 4 deletions.
8 changes: 4 additions & 4 deletions api-requests/auth.http
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@host=https://dummyjson.com
@host=https://dummyjson.com/auth

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

Expand All @@ -15,15 +15,15 @@ Content-Type: application/json
###
# @name GetUser
@YOUR_ACCESS_TOKEN={{LoginUser.response.body.accessToken}}
GET {{host}}/auth/me
GET {{host}}/me
Accept: application/json
Content-Type: application/json
Authorization: Bearer {{YOUR_ACCESS_TOKEN}}

###
# @name RefreshAuthSession
@YOUR_REFRESH_TOKEN={{LoginUser.response.body.refreshToken}}
POST {{host}}/auth/refresh
POST {{host}}/refresh
Accept: application/json
Content-Type: application/json

Expand Down
65 changes: 65 additions & 0 deletions api-requests/todos.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
@host=https://dummyjson.com/todos

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

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

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

###
# @name GetRandomTodos
GET {{host}}/random/3
Accept: application/json
Content-Type: application/json

###
# @name LimitSkipTodos
GET {{host}}?limit=3&skip=10
Accept: application/json
Content-Type: application/json

###
# @name GetAllTodosOfUser
GET {{host}}/user/1
Accept: application/json
Content-Type: application/json

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

{
"todo": "Use DummyJSON in the project",
"completed": false,
"userId": 5
}

###
# @name UpdateTodo
PUT {{host}}/1
Accept: application/json
Content-Type: application/json

{
"completed": true
}

###
# @name DeleteTodo
DELETE {{host}}/1
Accept: application/json
Content-Type: application/json
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mod auth;
mod todos;

pub use auth::*;
use reqwest::Client;
pub use todos::*;

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

Expand Down
98 changes: 98 additions & 0 deletions src/todos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//! Todos module
//! https://dummyjson.com/docs/todos
use crate::{DummyJsonClient, API_BASE_URL};
use once_cell::sync::Lazy;
use reqwest::Error;
use serde::Deserialize;
use serde_json::json;

static TODOS_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/todos", API_BASE_URL));

/// Todo
#[derive(Deserialize, Debug)]
pub struct Todo {
pub id: u32,
pub todo: String,
pub completed: bool,
#[serde(rename = "userId")]
pub user_id: u32,
}

/// All Todos response
#[derive(Deserialize, Debug)]
pub struct AllTodos {
pub todos: Vec<Todo>,
pub total: u32,
pub skip: u32,
pub limit: u32,
}

/// Delete todo response
#[derive(Deserialize, Debug)]
pub struct DeleteTodoResponse {
#[serde(flatten)]
pub todo: Todo,
#[serde(rename = "isDeleted")]
pub is_deleted: bool,
#[serde(rename = "deletedOn")]
pub deleted_on: String,
}

impl DummyJsonClient {
/// Get all todos
pub async fn get_all_todos(&self) -> Result<AllTodos, Error> {
let url = &*TODOS_BASE_URL;
self.client.get(url).send().await?.json().await
}

/// Get todo by id
pub async fn get_todo_by_id(&self, id: u32) -> Result<Todo, Error> {
let url = format!("{}/{}", *TODOS_BASE_URL, id);
self.client.get(url).send().await?.json().await
}

/// Get random todo
pub async fn get_random_todo(&self) -> Result<Todo, Error> {
let url = format!("{}/random", *TODOS_BASE_URL);
self.client.get(url).send().await?.json().await
}

/// Get random todos
pub async fn get_random_todos(&self, count: u32) -> Result<Vec<Todo>, Error> {
let url = format!("{}/random/{}", *TODOS_BASE_URL, count);
self.client.get(url).send().await?.json().await
}

/// Limit and skip todos
pub async fn limit_skip_todos(&self, limit: u32, skip: u32) -> Result<AllTodos, Error> {
let url = format!("{}/?limit={}&skip={}", *TODOS_BASE_URL, limit, skip);
self.client.get(url).send().await?.json().await
}

/// Get all todos of user
pub async fn get_all_todos_of_user(&self, user_id: u32) -> Result<AllTodos, Error> {
let url = format!("{}/user/{}", *TODOS_BASE_URL, user_id);
self.client.get(url).send().await?.json().await
}

/// Add todo
pub async fn add_todo(&self, todo: &str, completed: bool, user_id: u32) -> Result<Todo, Error> {
let url = format!("{}/add", *TODOS_BASE_URL);
let payload = json!({ "todo": todo, "completed": completed, "userId": user_id });
self.client.post(url).json(&payload).send().await?.json().await
}

/// Update todo
pub async fn update_todo(&self, id: u32, completed: bool) -> Result<Todo, Error> {
let url = format!("{}/{}", *TODOS_BASE_URL, id);
let payload = json!({ "completed": completed });
self.client.put(url).json(&payload).send().await?.json().await
}

/// Delete todo
pub async fn delete_todo(&self, id: u32) -> Result<DeleteTodoResponse, Error> {
let url = format!("{}/{}", *TODOS_BASE_URL, id);
self.client.delete(url).send().await?.json().await
}
}
90 changes: 90 additions & 0 deletions tests/todos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
mod todos {
use dummy_json_rs::DummyJsonClient;

#[tokio::test]
async fn get_all_todos() {
let client = DummyJsonClient::default();
let response = client.get_all_todos().await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn get_todo_by_id() {
let client = DummyJsonClient::default();
let response = client.get_todo_by_id(1).await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn get_todo_by_id_fails_with_invalid_id() {
let client = DummyJsonClient::default();
let response = client.get_todo_by_id(24332).await;
assert!(response.is_err());
}

#[tokio::test]
async fn get_random_todo() {
let client = DummyJsonClient::default();
let response = client.get_random_todo().await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn get_random_todos() {
let client = DummyJsonClient::default();
let response = client.get_random_todos(3).await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn limit_skip_todos() {
let client = DummyJsonClient::default();
let response = client.limit_skip_todos(3, 10).await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn limit_skip_todos_get_all_todos() {
let client = DummyJsonClient::default();
let response = client.limit_skip_todos(0, 0).await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn get_all_todos_of_user() {
let client = DummyJsonClient::default();
let response = client.get_all_todos_of_user(1).await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn add_todo() {
let client = DummyJsonClient::default();
let response = client.add_todo("Use DummyJSON in the project", false, 5).await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn update_todo() {
let client = DummyJsonClient::default();
let response = client.update_todo(1, true).await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn delete_todo() {
let client = DummyJsonClient::default();
let response = client.delete_todo(1).await;
// assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}
}

0 comments on commit 755c999

Please sign in to comment.