Skip to content

Commit

Permalink
Migrate to new API endpoints (#30)
Browse files Browse the repository at this point in the history
* Add get_key method
  • Loading branch information
Jonxslays committed Dec 31, 2023
1 parent 539cb0d commit 7a7a2a6
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 80 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
# v0.3.0 (Dec 2023)

## Breaking changes

- `VerifyKeyRequest` now requires an `api_id`.
- `ListKeysRequest` no longer has an `offset` field.

## Additions

- Add `Conflict` variant to `ErrorCode` enum.
- Add `get_key` method to `KeyService`.
- Add `cursor` field to `ListKeysRequest`.
- Add `Refill`, `RefillInterval`, and `UpdateOp` models/enums.
- Add `key_id` property onto `ApiKeyVerification`.
- Add `refill` property onto `ApiKeyMeta` and `ApiKeyVerification`.
- Add support for `refill` when creating and updating a key.
- Add `update_remaining` method to `KeyService` and corresponding `Route`.

## Changes

- Refactor internal routes to use new API endpoints.
- Use new headers requested by Unkey.

---

# v0.2.0 (Sep 2023)

## Additions
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "unkey"
description = "An asynchronous Rust SDK for the Unkey API."
version = "0.2.0"
version = "0.3.0"
edition = "2021"
authors = ["Jonxslays"]
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $ cargo add unkey
Add the following to your `Cargo.toml` dependencies array:

```toml
unkey = "0.1" # I won't forget to update this™
unkey = "0.3" # I won't forget to update this™
```

## Examples
Expand All @@ -38,7 +38,7 @@ use unkey::Client;

async fn verify_key() {
let c = Client::new("unkey_ABC");
let req = VerifyKeyRequest::new("test_DEF");
let req = VerifyKeyRequest::new("test_DEF", "api_JJJ");

match c.verify_key(req).await {
Wrapped::Ok(res) => println!("{res:?}"),
Expand Down
31 changes: 30 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::models::ApiKey;
use crate::models::CreateKeyRequest;
use crate::models::CreateKeyResponse;
use crate::models::GetApiRequest;
use crate::models::GetApiResponse;
use crate::models::GetKeyRequest;
use crate::models::ListKeysRequest;
use crate::models::ListKeysResponse;
use crate::models::RevokeKeyRequest;
Expand Down Expand Up @@ -122,7 +124,7 @@ impl Client {
/// # use unkey::models::VerifyKeyRequest;
/// # use unkey::models::Wrapped;
/// let c = Client::new("abc123");
/// let req = VerifyKeyRequest::new("test_KEYABC");
/// let req = VerifyKeyRequest::new("test_KEYABC", "api_123123");
///
/// match c.verify_key(req).await {
/// Wrapped::Ok(res) => println!("{:?}", res),
Expand Down Expand Up @@ -268,6 +270,33 @@ impl Client {
pub async fn update_key(&self, req: UpdateKeyRequest) -> Wrapped<()> {
self.keys.update_key(&self.http, req).await
}

/// Retrieves information for the given api id.
///
/// # Arguments
/// - `req`: The get api request to send.
///
/// # Returns
/// A wrapper containing the response, or an [`HttpError`].
///
/// # Example
/// ```no_run
/// # async fn get() {
/// # use unkey::Client;
/// # use unkey::models::UpdateKeyRequest;
/// # use unkey::models::Wrapped;
/// let c = Client::new("abc123");
/// let req = UpdateKeyRequest::new("api_id").set_remaining(Some(100));
///
/// match c.update_key(req).await {
/// Wrapped::Ok(res) => println!("{:?}", res),
/// Wrapped::Err(err) => println!("{:?}", err),
/// }
/// # }
/// ````
pub async fn get_key(&self, req: GetKeyRequest) -> Wrapped<ApiKey> {
self.keys.get_key(&self.http, req).await
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use models::Wrapped;
/// The wrapped error.
macro_rules! response_error {
($code:expr, $err:expr) => {
crate::models::Wrapped::Err(crate::models::HttpError::new($code, $err.to_string()))
$crate::models::Wrapped::Err($crate::models::HttpError::new($code, $err.to_string()))
};
}

Expand Down
23 changes: 13 additions & 10 deletions src/models/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub struct ListKeysRequest {
/// The optional number of keys to return, up to 100.
pub limit: Option<usize>,

/// The pagination offset.
pub offset: Option<usize>,
/// The pagination cursor indicating the last key that was returned.
pub cursor: Option<String>,
}

impl ListKeysRequest {
Expand All @@ -35,7 +35,7 @@ impl ListKeysRequest {
///
/// assert_eq!(r.api_id, String::from("test"));
/// assert_eq!(r.limit, None);
/// assert_eq!(r.offset, None);
/// assert_eq!(r.cursor, None);
/// assert_eq!(r.owner_id, None);
/// ```
#[must_use]
Expand All @@ -44,14 +44,14 @@ impl ListKeysRequest {
api_id: api_id.into(),
owner_id: None,
limit: None,
offset: None,
cursor: None,
}
}

/// Sets the limit for the request.
///
/// # Arguments
/// - `limit`: The limit to set, defaults to 100.
/// - `limit`: The limit to set.
///
/// # Returns
/// Self for chained calls.
Expand All @@ -72,21 +72,21 @@ impl ListKeysRequest {
/// Sets the pagination offset for the request.
///
/// # Arguments
/// - `offset`: The pagination offset to set, defaults to 0.
/// - `cursor`: The pagination offset cursor to set.
///
/// # Returns
/// Self for chained calls.
///
/// # Example
/// ```
/// # use unkey::models::ListKeysRequest;
/// let r = ListKeysRequest::new("test").set_offset(4);
/// let r = ListKeysRequest::new("test").set_cursor("abcabc");
///
/// assert_eq!(r.offset.unwrap(), 4);
/// assert_eq!(r.cursor.unwrap(), String::from("abcabc"));
/// ```
#[must_use]
pub fn set_offset(mut self, offset: usize) -> Self {
self.offset = Some(offset);
pub fn set_cursor<T: Into<String>>(mut self, cursor: T) -> Self {
self.cursor = Some(cursor.into());
self
}

Expand Down Expand Up @@ -120,6 +120,9 @@ pub struct ListKeysResponse {

/// The total number of api keys.
pub total: usize,

/// The cursor indicating the last key that was returned.
pub cursor: Option<String>,
}

/// An outgoing get api request.
Expand Down
3 changes: 3 additions & 0 deletions src/models/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum ErrorCode {
// The identifier is in use by another resource.
NotUnique,

// Another resource already uses this identifier.
Conflict,

/// Reserved for unknown interactions.
#[serde(other)]
Unknown,
Expand Down
Loading

0 comments on commit 7a7a2a6

Please sign in to comment.