Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic structure #19

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ license = "MIT"

[badges]
appveyor = { repository = "{{username}}/{{repository}}", branch = "main", service = "github" }

[dependencies]
reqwest = "0.11"
serde = { version = "1", features = ["derive"] }

[dev-dependencies]
reqwest = "0.11"
tokio = { version = "1", features = ["full"] }
wiremock = "0.5.19"
31 changes: 31 additions & 0 deletions src/api_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use reqwest;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Post {
userId: u32,
id: u32,
title: String,
body: String,
}

pub struct ApiClient {
base_url: String,
client: reqwest::Client,
}

impl ApiClient {
pub fn new(base_url: &str) -> Self {
let client = reqwest::Client::new();
ApiClient {
base_url: base_url.to_string(),
client,
}
}

pub async fn get_post(&self, post_id: u32) -> Result<Post, reqwest::Error> {
let url = format!("{}/posts/{}", self.base_url, post_id);
let response = self.client.get(&url).send().await?;
response.json().await
}
}
34 changes: 34 additions & 0 deletions src/bin/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use reqwest;
use tokio::time::Duration;
use wiremock::{Mock, MockServer, ResponseTemplate};

#[tokio::test]
async fn integration_test_api_client() {
let mock_server = MockServer::start().await;
let mock = Mock::given(wiremock::matchers::method("GET"))
.and(wiremock::matchers::path("/posts/1"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"userId": 1,
"id": 1,
"title": "Test Post",
"body": "This is a test post."
})))
.expect(1)
.mount(&mock_server)
.await;

let base_url = mock_server.uri();
let api_client = your_module::ApiClient::new(&base_url);

match api_client.get_post(1).await {
Ok(post) => {
assert_eq!(post.title, "Test Post");
}
Err(err) => {
eprintln!("Error: {}", err);
panic!("Test failed");
}
}

mock.assert().await;
}
19 changes: 19 additions & 0 deletions src/bin/post.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mod api_client;

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
// Create an instance of the API client
let api_client = api_client::ApiClient::new("https://jsonplaceholder.typicode.com");

// Example: Get a post by ID
match api_client.get_post(1).await {
Ok(post) => {
println!("Post: {:?}", post);
}
Err(err) => {
eprintln!("Error: {}", err);
}
}

Ok(())
}
1 change: 0 additions & 1 deletion src/lib.rs

This file was deleted.

Loading