Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
d-k-bo committed Aug 27, 2023
0 parents commit e424d58
Show file tree
Hide file tree
Showing 27 changed files with 1,899 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
RUST_TEST_THREADS = "1"
31 changes: 31 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
on: [push, pull_request]

name: CI

jobs:
fmt:
name: cargo fmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo fmt --all -- --check

clippy:
name: cargo clippy
runs-on: ubuntu-latest
strategy:
matrix:
features: ["", "app", "client", "full"]
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo clippy --features ${{ matrix.features }}

test:
name: cargo test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- run: cargo test --all-features
19 changes: 19 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Release

permissions:
contents: write

on:
push:
tags:
- v[0-9]+.*

jobs:
create-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: taiki-e/create-gh-release-action@v1
with:
branch: main
token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
58 changes: 58 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[package]
name = "gotify"
version = "0.1.0"
edition = "2021"
description = "Idiomatic client for the Gotify API"
repository = "https://github.com/d-k-bo/gotify-rs"
authors = ["d-k-bo <[email protected]>"]
license = "MIT"
categories = ["api-bindings"]
keywords = ["gotify", "notify", "notifications", "push-notifications"]

[features]
default = []
# Enable all features
full = ["app", "client"]
# Create messages
app = []
# Manage the server, use `manage-*` or `websocket` for finer grained control
client = [
"manage-applications",
"manage-clients",
"manage-messages",
"manage-plugins",
"manage-users",
"websocket",
]
client-core = []
# Create, read, update and delete applications or modify application images
manage-applications = ["client-core"]
# List, create, update or delete clients
manage-clients = ["client-core"]
# List or delete messages
manage-messages = ["client-core"]
# List or configure plugins
manage-plugins = ["client-core"]
# List, create, update or delete users
manage-users = ["client-core"]
# Subscribe to newly created messages via a websocket
websocket = ["client-core", "async-stream", "futures-util", "tokio-tungstenite"]

[dependencies]
async-stream = { version = "0.3.5", optional = true }
futures-util = { version = "0.3.28", optional = true }
paste = "1.0.14"
reqwest = { version = "0.11.12", features = ["json", "multipart"] }
serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.86"
thiserror = "1.0.37"
time = { version = "0.3.25", features = ["serde", "parsing", "formatting"] }
tokio-tungstenite = { version = "0.20.0", optional = true }
url = "2.3.1"

[dev-dependencies]
eyre = "0.6.8"
futures-util = "0.3.28"
macro_rules_attribute = "0.2.0"
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread", "time"] }
zip = { version = "0.6.6", default-features = false, features = ["deflate"] }
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 d-k-bo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# gotify-rs

[![Build Status](https://github.com/d-k-bo/gotify/workflows/CI/badge.svg)](https://github.com/d-k-bo/gotify/actions?query=workflow%3ACI)
[![Crates.io](https://img.shields.io/crates/v/gotify)](https://lib.rs/crates/gotify)
[![Documentation](https://img.shields.io/docsrs/gotify)](https://docs.rs/gotify)
[![License: MIT](https://img.shields.io/crates/l/gotify)](LICENSE)

<!-- cargo-rdme start -->

An idiomatic Rust client for Gotify.

### Overview

By default, this crate only exposes the `Client::health()`, `Client::version()` methods.
All other categories of endpoints must be enabled by the correspondig feature flags.

| Feature flag | Enabled methods | Note |
| ------------ | --------------- | ---- |
| `app` | `Client::create_message()` | |
| `manage-clients` | `Client::get_clients()`, `Client::create_client()`, `Client::update_client()`, `Client::delete_client()` | |
| `manage-messages` | `Client::get_application_messages()`, `Client::delete_application_messages()`, `Client::get_messages()`, `Client::delete_messages()`, `Client::delete_message()` | doesn't include `Client::create_message()` and `Client::message_stream()` |
| `manage-plugins` | `Client::get_plugins()`, `Client::get_plugin_config()`, `Client::update_plugin_config()`, `Client::disable_plugin()`, `Client::get_plugin_display()`, `Client::enable_plugin()` | |
| `websocket` | `Client::message_stream()` | enables additional dependencies (mainly [`tokio-tungstenite`](https://docs.rs/tokio-tungstenite)) |

### Examples

#### Creating a message

```rust
let client: gotify::AppClient = gotify::Client::new(GOTIFY_URL, GOTIFY_APP_TOKEN)?;

client.create_message("Lorem ipsum dolor sit amet").with_title("Lorem Ipsum").await?;
```

#### Listening for new messages

```rust
use futures_util::StreamExt;

let client: gotify::ClientClient = gotify::Client::new(GOTIFY_URL, GOTIFY_CLIENT_TOKEN)?;

let mut messages = client.message_stream().await?;

while let Some(result) = messages.next().await {
let message = result?;

println!("{message:#?}")
}
```

<!-- cargo-rdme end -->

## License

This project is licensed under the MIT License.

See [LICENSE](LICENSE) for more information.
Binary file added data/gotify.db
Binary file not shown.
11 changes: 11 additions & 0 deletions examples/create_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[tokio::main]
async fn main() -> eyre::Result<()> {
let client: gotify::AppClient =
gotify::Client::new(env!("GOTIFY_URL"), env!("GOTIFY_APP_TOKEN"))?;

client
.create_message("Lorem ipsum dolor sit amet")
.with_title("Lorem Ipsum")
.await?;
Ok(())
}
13 changes: 13 additions & 0 deletions examples/websocket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use futures_util::StreamExt;

#[tokio::main]
async fn main() -> eyre::Result<()> {
let client: gotify::ClientClient =
gotify::Client::new(env!("GOTIFY_URL"), env!("GOTIFY_CLIENT_TOKEN"))?;
let mut messages = client.message_stream().await?;
while let Some(result) = messages.next().await {
let message = result?;
println!("{message:#?}")
}
Ok(())
}
65 changes: 65 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::collections::HashMap;

use reqwest::Method;

use crate::{models::Message, utils::request_builder, AppClient};

/// Create messages.
impl AppClient {
/// Create a message.
pub fn create_message(&self, message: impl Into<String>) -> MessageBuilder {
MessageBuilder::new(self, message)
}
}

request_builder! {
name = MessageBuilder,
client_type = AppClient,
method = Method::POST,
uri = ["message"],
return_type = Message,
required_fields = {
message: impl Into<String> => .into() => String,
},
optional_fields = {
title: impl Into<String> => .into() => String,
extras: impl Into<HashMap<String, serde_json::Value>> => .into() => HashMap<String, serde_json::Value>,
priority: u8 => u8,
}
}

#[cfg(test)]
pub(crate) mod tests {
use crate::testsuite::*;

#[apply(run_test_server!)]
#[test]
async fn create_message() -> eyre::Result<()> {
let client = app_client();

let message = client.create_message("Hello World").await?;
assert_eq!(message.message, "Hello World");

let message = client
.create_message("Hello World")
.with_title("Hi")
.with_priority(7)
.with_extras([("foo".into(), "bar".into())])
.await?;
assert_eq!(message.title.as_deref(), Some("Hi"));
assert_eq!(message.message, "Hello World");
assert_eq!(message.priority, 7);
assert_eq!(
message
.extras
.unwrap()
.get("foo")
.unwrap()
.as_str()
.unwrap(),
"bar"
);

Ok(())
}
}
Loading

0 comments on commit e424d58

Please sign in to comment.