|
1 | | -# servicestack-rust |
2 | | -ServiceStack Client Rust Library |
| 1 | +# ServiceStack Rust Client Library |
| 2 | + |
| 3 | +A Rust client library for ServiceStack services, providing type-safe HTTP communication with async/await support. |
| 4 | + |
| 5 | +[](https://crates.io/crates/servicestack) |
| 6 | +[](https://docs.rs/servicestack) |
| 7 | +[](LICENSE) |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- 🚀 **Async/await support** - Built on tokio for efficient async operations |
| 12 | +- 📦 **Type-safe** - Leverages Rust's type system with serde for serialization |
| 13 | +- 🔧 **Flexible** - Support for all HTTP methods (GET, POST, PUT, DELETE, PATCH) |
| 14 | +- 🛡️ **Reliable** - Built on reqwest for robust HTTP communication |
| 15 | +- 📖 **Well-documented** - Comprehensive API documentation and examples |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +Add this to your `Cargo.toml`: |
| 20 | + |
| 21 | +```toml |
| 22 | +[dependencies] |
| 23 | +servicestack = "0.1.0" |
| 24 | +tokio = { version = "1.0", features = ["full"] } |
| 25 | +serde = { version = "1.0", features = ["derive"] } |
| 26 | +``` |
| 27 | + |
| 28 | +## Quick Start |
| 29 | + |
| 30 | +```rust |
| 31 | +use servicestack::{ServiceStackClient, Result}; |
| 32 | +use serde::{Deserialize, Serialize}; |
| 33 | + |
| 34 | +#[derive(Serialize)] |
| 35 | +struct HelloRequest { |
| 36 | + name: String, |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Deserialize)] |
| 40 | +struct HelloResponse { |
| 41 | + result: String, |
| 42 | +} |
| 43 | + |
| 44 | +#[tokio::main] |
| 45 | +async fn main() -> Result<()> { |
| 46 | + // Create a new client |
| 47 | + let client = ServiceStackClient::new("https://example.org"); |
| 48 | + |
| 49 | + // Make a POST request |
| 50 | + let request = HelloRequest { |
| 51 | + name: "World".to_string() |
| 52 | + }; |
| 53 | + let response: HelloResponse = client.post("/hello", &request).await?; |
| 54 | + |
| 55 | + println!("{}", response.result); |
| 56 | + Ok(()) |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Usage Examples |
| 61 | + |
| 62 | +### GET Request |
| 63 | + |
| 64 | +```rust |
| 65 | +use servicestack::{ServiceStackClient, Result}; |
| 66 | +use serde::Deserialize; |
| 67 | + |
| 68 | +#[derive(Deserialize)] |
| 69 | +struct User { |
| 70 | + id: u64, |
| 71 | + name: String, |
| 72 | +} |
| 73 | + |
| 74 | +async fn get_user(client: &ServiceStackClient, id: u64) -> Result<User> { |
| 75 | + client.get(&format!("/users/{}", id)).await |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### POST Request |
| 80 | + |
| 81 | +```rust |
| 82 | +use servicestack::{ServiceStackClient, Result}; |
| 83 | +use serde::{Deserialize, Serialize}; |
| 84 | + |
| 85 | +#[derive(Serialize)] |
| 86 | +struct CreateUserRequest { |
| 87 | + name: String, |
| 88 | + email: String, |
| 89 | +} |
| 90 | + |
| 91 | +#[derive(Deserialize)] |
| 92 | +struct CreateUserResponse { |
| 93 | + id: u64, |
| 94 | + name: String, |
| 95 | +} |
| 96 | + |
| 97 | +async fn create_user(client: &ServiceStackClient) -> Result<CreateUserResponse> { |
| 98 | + let request = CreateUserRequest { |
| 99 | + name: "John Doe".to_string(), |
| 100 | + email: "john@example.com".to_string(), |
| 101 | + }; |
| 102 | + client.post("/users", &request).await |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### Custom Client Configuration |
| 107 | + |
| 108 | +```rust |
| 109 | +use servicestack::ServiceStackClient; |
| 110 | +use reqwest::Client; |
| 111 | +use std::time::Duration; |
| 112 | + |
| 113 | +let custom_client = Client::builder() |
| 114 | + .timeout(Duration::from_secs(60)) |
| 115 | + .build() |
| 116 | + .unwrap(); |
| 117 | + |
| 118 | +let client = ServiceStackClient::with_client( |
| 119 | + "https://api.example.com", |
| 120 | + custom_client |
| 121 | +); |
| 122 | +``` |
| 123 | + |
| 124 | +## API Documentation |
| 125 | + |
| 126 | +For detailed API documentation, visit [docs.rs/servicestack](https://docs.rs/servicestack). |
| 127 | + |
| 128 | +## License |
| 129 | + |
| 130 | +This project is licensed under the BSD-3-Clause License - see the LICENSE file for details. |
| 131 | + |
| 132 | +## Contributing |
| 133 | + |
| 134 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 135 | + |
| 136 | +## About ServiceStack |
| 137 | + |
| 138 | +[ServiceStack](https://servicestack.net) is a simple, fast, versatile and highly-productive full-featured Web and Web Services Framework. |
0 commit comments