Skip to content

Commit

Permalink
feat(xrpc): Remove client implementations from XRPC (#68)
Browse files Browse the repository at this point in the history
* Remove clients from xrpc

* Add docs
  • Loading branch information
sugyan committed Nov 10, 2023
1 parent 6e44665 commit 3bee67a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 54 deletions.
1 change: 0 additions & 1 deletion atrium-xrpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ keywords = ["atproto", "bluesky"]
[dependencies]
async-trait = "0.1.68"
http = "0.2.9"
reqwest = "0.11.16"
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0.96"
serde_qs = "0.12.0"
Expand Down
7 changes: 1 addition & 6 deletions atrium-xrpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@
[![Rust](https://github.com/sugyan/atrium/actions/workflows/xrpc.yml/badge.svg?branch=main)](https://github.com/sugyan/atrium/actions/workflows/xrpc.yml)

Definitions for ATProto's [XRPC](https://atproto.com/specs/xrpc) request/response, and their associated errors.
And a client using [`reqwest`](https://crates.io/crates/reqwest) that can be used as its default implementation is included.

```rust
use atrium_xrpc::client::reqwest::ReqwestClient;

let client = ReqwestClient::new("https://bsky.social".into());
```
The `XrpcClient` trait inherits from and uses `HttpClient` to provide a default implementation for handling XRPC requests. So developers can create their own Client for XRPC by implementing an `HttpClient` that sends asynchronous HTTP requests according to this interface.
2 changes: 0 additions & 2 deletions atrium-xrpc/src/client.rs

This file was deleted.

43 changes: 0 additions & 43 deletions atrium-xrpc/src/client/reqwest.rs

This file was deleted.

6 changes: 5 additions & 1 deletion atrium-xrpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use http::StatusCode;
use std::fmt::Debug;

/// [Custom error codes and descriptions](https://atproto.com/specs/xrpc#custom-error-codes-and-descriptions)
/// [A standard error response schema](https://atproto.com/specs/xrpc#error-responses)
///
/// ```typescript
/// export const errorResponseBody = z.object({
Expand All @@ -17,6 +17,9 @@ pub struct ErrorResponseBody {
pub message: Option<String>,
}

/// An enum of possible XRPC error kinds.
///
/// Error defined in Lexicon schema or other standard error.
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(untagged)]
pub enum XrpcErrorKind<E> {
Expand All @@ -31,6 +34,7 @@ pub struct XrpcError<E> {
pub error: Option<XrpcErrorKind<E>>,
}

/// An enum of possible error kinds.
#[derive(thiserror::Error, Debug)]
pub enum Error<E> {
#[error("xrpc response error: {0:?}")]
Expand Down
13 changes: 12 additions & 1 deletion atrium-xrpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#![doc = include_str!("../README.md")]
pub mod client;
pub mod error;

use crate::error::{Error, XrpcError, XrpcErrorKind};
use async_trait::async_trait;
use http::{Method, Request, Response};
use serde::{de::DeserializeOwned, Serialize};

/// A type which can be used as a parameter of [`XrpcRequest`].
///
/// JSON serializable data or raw bytes.
pub enum InputDataOrBytes<T>
where
T: Serialize,
Expand All @@ -15,6 +17,9 @@ where
Bytes(Vec<u8>),
}

/// A type which can be used as a return value of [`XrpcClient::send_xrpc()`].
///
/// JSON deserializable data or raw bytes.
pub enum OutputDataOrBytes<T>
where
T: DeserializeOwned,
Expand All @@ -23,6 +28,7 @@ where
Bytes(Vec<u8>),
}

/// An abstract HTTP client.
#[async_trait]
pub trait HttpClient {
async fn send_http(
Expand All @@ -31,6 +37,7 @@ pub trait HttpClient {
) -> Result<Response<Vec<u8>>, Box<dyn std::error::Error + Send + Sync + 'static>>;
}

/// A request which can be executed with [`XrpcClient::send_xrpc()`].
pub struct XrpcRequest<P, I>
where
I: Serialize,
Expand All @@ -44,6 +51,10 @@ where

pub type XrpcResult<O, E> = Result<OutputDataOrBytes<O>, self::Error<E>>;

/// An abstract XRPC client.
///
/// [`send_xrpc()`](XrpcClient::send_xrpc) method has a default implementation,
/// which wraps the [`HttpClient::send_http()`]` method to handle input and output as an XRPC Request.
#[async_trait]
pub trait XrpcClient: HttpClient {
fn base_uri(&self) -> &str;
Expand Down

0 comments on commit 3bee67a

Please sign in to comment.