From 8d57f5d9de3b704a09f7f75caa73dfa2bf501285 Mon Sep 17 00:00:00 2001 From: Scott Lyons Date: Fri, 9 Aug 2024 00:12:47 -0700 Subject: [PATCH] Adding initial code for Suzurubooru client --- .gitignore | 1 + .pre-commit-config.yaml | 15 + Cargo.lock | 1892 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 25 + docker-compose.yml | 0 src/client.rs | 1220 +++++++++++++++++++++++++ src/errors.rs | 199 ++++ src/lib.rs | 16 + src/models.rs | 925 +++++++++++++++++++ src/tests.rs | 72 ++ src/tokens.rs | 617 +++++++++++++ 11 files changed, 4982 insertions(+) create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 docker-compose.yml create mode 100644 src/client.rs create mode 100644 src/errors.rs create mode 100644 src/lib.rs create mode 100644 src/models.rs create mode 100644 src/tests.rs create mode 100644 src/tokens.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..63f913e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,15 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files +- repo: https://github.com/FeryET/pre-commit-rust + rev: v1.1.0 + hooks: + - id: fmt + - id: cargo-check diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..4917489 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1892 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "addr2line" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "assert-json-diff" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "backtrace" +version = "0.3.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "bytes" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952" + +[[package]] +name = "cc" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aba8f4e9906c7ce3c73463f62a7f0c65183ada1a2d47e397cc8810827f9694f" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "serde", + "wasm-bindgen", + "windows-targets 0.52.6", +] + +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "darling" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "derive_builder" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0350b5cb0331628a5916d6c5c0b72e97393b8b6b03b47a9284f4e7f5a405ffd7" +dependencies = [ + "derive_builder_macro", +] + +[[package]] +name = "derive_builder_core" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d48cda787f839151732d396ac69e3473923d54312c070ee21e9effcaa8ca0b1d" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "derive_builder_macro" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "206868b8242f27cecce124c19fd88157fbd0dd334df2587f36417bafbc85097b" +dependencies = [ + "derive_builder_core", + "syn", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "encoding_rs" +version = "0.8.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "fastrand" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "gimli" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" + +[[package]] +name = "h2" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.12", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "h2" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa82e28a107a8cc405f0839610bdc9b15f1e25ec7d696aa5cf173edbcb1486ab" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http 1.1.0", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.12", + "pin-project-lite", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http 1.1.0", +] + +[[package]] +name = "http-body-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" +dependencies = [ + "bytes", + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "0.14.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a152ddd61dfaec7273fe8419ab357f33aee0d914c5f4efbf0d96fa749eea5ec9" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2 0.3.26", + "http 0.2.12", + "http-body 0.4.6", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "h2 0.4.5", + "http 1.1.0", + "http-body 1.0.1", + "httparse", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155" +dependencies = [ + "futures-util", + "http 1.1.0", + "hyper 1.4.1", + "hyper-util", + "rustls", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tower-service", +] + +[[package]] +name = "hyper-tls" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" +dependencies = [ + "bytes", + "http-body-util", + "hyper 1.4.1", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ab92f4f49ee4fb4f997c784b7a2e0fa70050211e0b6a287f898c3c9785ca956" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "hyper 1.4.1", + "pin-project-lite", + "socket2", + "tokio", + "tower", + "tower-service", + "tracing", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "ipnet" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "js-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mime_guess" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "miniz_oxide" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +dependencies = [ + "adler", +] + +[[package]] +name = "mio" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4569e456d394deccd22ce1c1913e6ea0e54519f577285001215d33557431afe4" +dependencies = [ + "hermit-abi", + "libc", + "wasi", + "windows-sys 0.52.0", +] + +[[package]] +name = "mockito" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6e023aa5bdf392aa06c78e4a4e6d498baab5138d0c993503350ebbc37bf1e" +dependencies = [ + "assert-json-diff", + "colored", + "futures-core", + "hyper 0.14.30", + "log", + "rand", + "regex", + "serde_json", + "serde_urlencoded", + "similar", + "tokio", +] + +[[package]] +name = "native-tls" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "object" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f203fa8daa7bb185f760ae12bd8e097f63d17041dcdcaf675ac54cdf863170e" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "openssl" +version = "0.10.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9529f4786b70a3e8c61e11179af17ab6188ad8d0ded78c5529441ed39d4bd9c1" +dependencies = [ + "bitflags 2.6.0", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f9e8deee91df40a943c71b917e5874b951d32a802526c85721ce3b776c929d6" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.52.6", +] + +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pin-project" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "redox_syscall" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +dependencies = [ + "bitflags 2.6.0", +] + +[[package]] +name = "regex" +version = "1.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "reqwest" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7d6d2a27d57148378eb5e111173f4276ad26340ecc5c49a4a2152167a2d6a37" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2 0.4.5", + "http 1.1.0", + "http-body 1.0.1", + "http-body-util", + "hyper 1.4.1", + "hyper-rustls", + "hyper-tls", + "hyper-util", + "ipnet", + "js-sys", + "log", + "mime", + "mime_guess", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + +[[package]] +name = "ring" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "rustix" +version = "0.38.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +dependencies = [ + "bitflags 2.6.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustls" +version = "0.23.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c58f8c84392efc0a126acce10fa59ff7b3d2ac06ab451a33f2741989b806b044" +dependencies = [ + "once_cell", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pemfile" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29993a25686778eb88d4189742cd713c9bce943bc54251a33509dc63cbacf73d" +dependencies = [ + "base64", + "rustls-pki-types", +] + +[[package]] +name = "rustls-pki-types" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d" + +[[package]] +name = "rustls-webpki" +version = "0.102.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e6b52d4fda176fd835fdc55a835d4a89b8499cad995885a21149d5ad62f852e" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "schannel" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "security-framework" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" +dependencies = [ + "bitflags 2.6.0", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75da29fe9b9b08fe9d6b22b5b4bcbc75d8db3aa31e639aa56bb62e9d46bfceaf" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "serde" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + +[[package]] +name = "similar" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e" + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "socket2" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "strum" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" + +[[package]] +name = "system-configuration" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "szurubooru-client" +version = "0.1.0" +dependencies = [ + "base64", + "bytes", + "chrono", + "derive_builder", + "hex", + "mockito", + "reqwest", + "serde", + "serde_json", + "sha1", + "strum", + "strum_macros", + "thiserror", + "tokio", + "tracing", + "url", + "urlencoding", +] + +[[package]] +name = "tempfile" +version = "3.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +dependencies = [ + "cfg-if", + "fastrand", + "rustix", + "windows-sys 0.52.0", +] + +[[package]] +name = "thiserror" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinyvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.39.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" +dependencies = [ + "rustls", + "rustls-pki-types", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "futures-core", + "futures-util", + "pin-project", + "pin-project-lite", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-layer" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" + +[[package]] +name = "tower-service" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + +[[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unicode-normalization" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" + +[[package]] +name = "web-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winreg" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8779e76 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "szurubooru-client" +version = "0.1.0" +edition = "2021" + +[dependencies] +base64 = "0.22.1" +bytes = "1.6.1" +chrono = { version = "0.4.38", features = ["serde"] } +derive_builder = "0.20.0" +hex = "0.4.3" +reqwest = { version = "0.12.5", features = ["json", "multipart"] } +serde = { version = "1.0.204", features = ["derive"] } +serde_json = "1.0.120" +sha1 = "0.10.6" +strum = { version = "0.26.3", features = ["derive", "strum_macros"] } +strum_macros = "0.26.4" +thiserror = "1.0.63" +tracing = "0.1.40" +url = "2.5.2" +urlencoding = "2.1.3" + +[dev-dependencies] +mockito = "1.4.0" +tokio = { version = "1.39.2", features = ["full"] } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e69de29 diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..476ad74 --- /dev/null +++ b/src/client.rs @@ -0,0 +1,1220 @@ +#![warn(missing_docs)] + +use crate::{errors::*, models::*, tokens::*}; +use base64::{engine::general_purpose::STANDARD, Engine as _}; +use reqwest::{ + header::{HeaderMap, ACCEPT, AUTHORIZATION}, + multipart::{Form, Part}, + Client, ClientBuilder, Method, RequestBuilder, +}; +use serde::{de::DeserializeOwned, Serialize}; +use sha1::{Digest, Sha1}; +use std::fmt::{Display, Formatter}; +use std::path::Path; +use std::{ + fs::File, + io::{BufReader, Read}, +}; +use url::Url; + +/// +/// The base Szurubooru Client +/// +/// Use this `struct` to create requests to run against a Szurubooru instance. +/// +#[derive(Debug)] +pub struct SzurubooruClient { + base_url: Url, + client: Client, + auth: SzurubooruAuth, +} + +impl SzurubooruClient { + /// + /// Construct a new `SzurubooruClient` using a username and token. + /// + /// * `host` - The host to connect to, including `http` or `https`. Any path fragments will + /// be stripped + /// * `username` - The username to authenticate as + /// * `token` - The token used to authenticate as `username` + /// * `allow_insecure` - Whether to disable SSL verification + /// + /// ## Returns + /// + /// A [SzurubooruResult] containing the client. May return a [SzurubooruClientError::UrlParseError] + /// if the host URL isn't a proper URL. + /// + /// ```no_run + /// use szurubooru_client::SzurubooruClient; + /// let client = SzurubooruClient::new_with_token("http://localhost:5001", "myuser", "sz-123456", true).unwrap(); + /// ``` + pub fn new_with_token( + host: &str, + username: &str, + token: &str, + allow_insecure: bool, + ) -> SzurubooruResult { + let encoded_auth = STANDARD.encode(format!("{username}:{token}").as_bytes()); + let token_header_value = format!("Token {encoded_auth}"); + let auth = SzurubooruAuth::TokenAuth(token_header_value); + SzurubooruClient::new(host, auth, allow_insecure) + } + + /// + /// Construct a new `SzurubooruClient` using a username and token. + /// + /// * `host` - The host to connect to, including `http` or `https` + /// * `username` - The username to authenticate as + /// * `password` - The password used to authenticate as `username` + /// * `allow_insecure` - Whether to disable SSL verification + /// + /// ## Returns + /// + /// A [SzurubooruResult] containing the client. May return a [SzurubooruClientError::UrlParseError] + /// if the host URL isn't a proper URL. + /// + /// ```no_run + /// use szurubooru_client::SzurubooruClient; + /// let client = SzurubooruClient::new_with_basic_auth("http://localhost:5001", "myuser", + /// "mypassword", true).unwrap(); + /// ``` + pub fn new_with_basic_auth( + host: &str, + username: &str, + password: &str, + allow_insecure: bool, + ) -> SzurubooruResult { + let auth = SzurubooruAuth::BasicAuth(username.to_string(), password.to_string()); + SzurubooruClient::new(host, auth, allow_insecure) + } + + fn new(host: &str, auth: SzurubooruAuth, allow_insecure: bool) -> SzurubooruResult { + let host = if host.ends_with("/") { + &host[0..host.len() - 1] + } else { + host + }; + let mut base_url = Url::parse(host).map_err(|e| SzurubooruClientError::UrlParseError { + source: e, + url: host.to_string(), + })?; + base_url.set_fragment(None); + + let mut header_map = HeaderMap::new(); + //header_map.append(AUTHORIZATION, token_header_value.parse().unwrap()); + header_map.append(ACCEPT, "application/json".parse().unwrap()); + + let client = ClientBuilder::new() + .danger_accept_invalid_certs(allow_insecure) + .default_headers(header_map) + .build() + .unwrap(); + + Ok(Self { + base_url, + client, + auth, + }) + } + + /// Construct a new request using the existing client auth and base URL + pub fn request(&self) -> SzurubooruRequest { + SzurubooruRequest::new(self) + } + + /// Construct a new request while selecting only the given fields + pub fn with_fields<'a>(&'a self, fields: &'a Vec) -> SzurubooruRequest { + self.request().fields(fields) + } + + /// Construct a new request with the given limit + pub fn limit(&self, limit: u32) -> SzurubooruRequest { + self.request().limit(limit) + } + + /// Construct a new request starting at the given offset + pub fn offset(&self, offset: u32) -> SzurubooruRequest { + self.request().offset(offset) + } +} + +#[derive(Debug)] +/// A type that represents a single Szurubooru request. +pub struct SzurubooruRequest<'a> { + fields: Option<&'a Vec>, + limit: Option, + offset: Option, + client: &'a SzurubooruClient, +} + +impl<'a> SzurubooruRequest<'a> { + pub(super) fn new(client: &'a SzurubooruClient) -> Self { + Self { + client, + fields: None, + limit: None, + offset: None, + } + } + + /// Select which fields to return from the query. + /// Most of the fields in [models] are [Option]s because they can potentially be omitted + /// using this method. + /// See [here](https://github.com/rr-/szurubooru/blob/master/doc/API.md#field-selecting) for + /// more details + pub fn fields(mut self, fields: &'a Vec) -> Self { + self.fields = Some(fields); + self + } + + /// Limit the number of returned results. Only applies to endpoints that return multiple + /// records + pub fn limit(mut self, limit: u32) -> Self { + self.limit = Some(limit); + self + } + + /// Skip a certain number of records. Only applies to endpoints that return + /// [PagedSearchResults](models::PagedSearchResult) + pub fn offset(mut self, offset: u32) -> Self { + self.offset = Some(offset); + self + } + + #[doc(hidden)] + fn prep_request( + &self, + method: Method, + path: T, + query: Option<&Vec>, + ) -> reqwest::RequestBuilder + where + T: AsRef + Display, + { + let mut req_url = self.client.base_url.clone(); + req_url.set_path(path.as_ref()); + + if let Some(query_vec) = query { + let mut qpm = req_url.query_pairs_mut(); + let query_string = query_vec.to_query_string(); + qpm.append_pair("query", &query_string); + } + + if let Some(fields) = self.fields { + let mut qpm = req_url.query_pairs_mut(); + let fields_list = fields.join(","); + qpm.append_pair("fields", &fields_list); + } + + if let Some(limit) = &self.limit { + let mut qpm = req_url.query_pairs_mut(); + qpm.append_pair("limit", &limit.to_string()); + } + + if let Some(offset) = &self.offset { + let mut qpm = req_url.query_pairs_mut(); + qpm.append_pair("offset", &offset.to_string()); + } + + // This doesn't detect the required `mut` for some reason + #[allow(unused_mut)] + let mut req = self.client.client.request(method, req_url); + match &self.client.auth { + SzurubooruAuth::TokenAuth(t) => { + let mut header_map = HeaderMap::new(); + header_map.append(AUTHORIZATION, t.parse().unwrap()); + + req.headers(header_map) + } + SzurubooruAuth::BasicAuth(u, p) => req.basic_auth(u, Some(p)), + SzurubooruAuth::None => req, + } + } + + #[tracing::instrument(skip(self), fields(base_url=self.client.base_url.to_string()))] + async fn do_request( + &self, + method: Method, + path: P, + query: Option<&Vec>, + body: Option<&B>, + ) -> SzurubooruResult + where + T: DeserializeOwned, + B: Serialize + std::fmt::Debug, + P: AsRef + Display + std::fmt::Debug, + { + let mut request = self.prep_request(method, path, query); + + if let Some(b) = body { + let b_str = + serde_json::to_string(b).map_err(SzurubooruClientError::JSONSerializationError)?; + request = request.body(b_str); + } + + self.handle_request(request).await + } + + async fn handle_request( + &self, + request: RequestBuilder, + ) -> SzurubooruResult { + let request = request + .build() + .map_err(SzurubooruClientError::RequestBuilderError)?; + + let response = self.client.client.execute(request).await; + + let response = response + .map_err(SzurubooruClientError::RequestError)? + .error_for_status() + .map_err(SzurubooruClientError::RequestError)?; + + let response_text = response + .text() + .await + .map_err(SzurubooruClientError::RequestError)?; + serde_json::from_str::>(&response_text) + .map_err(|e| SzurubooruClientError::ResponseParsingError(e, response_text))? + .into_result() + } + + /// Lists all tag categories. Doesn't use paging. + pub async fn list_tag_categories( + &self, + ) -> SzurubooruResult> { + self.do_request(Method::GET, "/tag-categories", None, None::<&String>) + .await + } + + /// Creates a new tag category using specified parameters. Name must match + /// `tag_category_name_regex` from server's configuration. First category created + /// becomes the default category. + pub async fn create_tag_category( + &self, + new_cat: &CreateUpdateTagCategory, + ) -> SzurubooruResult { + self.do_request(Method::POST, "/tag-categories", None, Some(new_cat)) + .await + } + + /// Updates an existing tag category using specified parameters. Name must match + /// `tag_category_name_regex` from server's configuration. All fields except + /// [version](models::TagCategoryResource::version) are optional - update concerns only provided fields. + pub async fn update_tag_category( + &self, + name: T, + resource: &TagCategoryResource, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/tag-category/{name}"); + self.do_request(Method::PUT, &path, None, Some(resource)) + .await + } + + /// Retrieves information about an existing tag category. + pub async fn get_tag_category(&self, name: T) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/tag-category/{name}"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Deletes existing tag category. The tag category to be deleted must have no usages. + pub async fn delete_tag_category(&self, name: T, version: u32) -> SzurubooruResult<()> + where + T: AsRef + Display, + { + let path = format!("/tag-category/{name}"); + let version_obj = ResourceVersion { version }; + self.do_request(Method::DELETE, &path, None, Some(&version_obj)) + .await + } + + /// Sets given tag category as default. All new tags created manually or automatically will + /// have this category. + pub async fn set_default_tag_category(&self, name: T) -> SzurubooruResult<()> + where + T: AsRef + Display, + { + let path = format!("/tag-category/{name}/default"); + self.do_request(Method::PUT, &path, None, None::<&String>) + .await + } + + /// Searches for tags. + /// See the (named tokens)[tokens::TagNamedToken] and (sort tokens)[tokens::TagSortToken] for + /// all possible query tokens, or use (QueryToken)[tokens::QueryToken] for a custom token + pub async fn list_tags( + &self, + query: &Vec, + ) -> SzurubooruResult> { + self.do_request(Method::GET, "/tags", Some(query), None::<&String>) + .await + } + + /// Creates a new tag using specified parameters. Names, suggestions and implications must + /// match `tag_name_regex` from server's configuration. Category must exist and is the same + /// as the `name` field within (TagCategoryResource)[models::TagCategoryResource] resource. + /// Suggestions and implications are optional. If specified implied tags or suggested tags do + /// not exist yet, they will be automatically created. Tags created automatically have no + /// implications, no suggestions, one name and their category is set to the first tag category + /// found. If there are no tag categories established yet, an error will be thrown. + pub async fn create_tag(&self, new_tag: &CreateUpdateTag) -> SzurubooruResult { + self.do_request(Method::POST, "/tags", None, Some(new_tag)) + .await + } + + /// Updates an existing tag using specified parameters. Names, suggestions and implications must + /// match `tag_name_regex` from server's configuration. Category must exist and is the same + /// as the `name` field within (TagCategoryResource)[models::TagCategoryResource] resource. + /// Suggestions and implications are optional. If specified implied tags or suggested tags do + /// not exist yet, they will be automatically created. Tags created automatically have no + /// implications, no suggestions, one name and their category is set to the first tag category + /// found. If there are no tag categories established yet, an error will be thrown. + pub async fn update_tag( + &self, + name: T, + update_tag: &CreateUpdateTag, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/tag/{name}"); + self.do_request(Method::PUT, &path, None, Some(update_tag)) + .await + } + + /// Retrieves information about an existing tag. + pub async fn get_tag(&self, name: T) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/tag/{name}"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Deletes existing tag. The tag to be deleted must have no usages. + pub async fn delete_tag(&self, name: T, version: u32) -> SzurubooruResult<()> + where + T: AsRef + Display, + { + let path = format!("/tag/{name}"); + let version_obj = ResourceVersion { version }; + self.do_request(Method::DELETE, &path, None, Some(&version_obj)) + .await + } + + /// Removes source tag and merges all of its usages, suggestions and implications to the + /// target tag. Other tag properties such as category and aliases do not get transferred + /// and are discarded. + pub async fn merge_tag(&self, merge_opts: &MergeTags) -> SzurubooruResult { + self.do_request(Method::POST, "/tag-merge", None, Some(merge_opts)) + .await + } + + /// Lists siblings of given tag, e.g. tags that were used in the same posts as the given tag. + /// The (occurrences)[models::TagSibling::occurrences] field signifies how many times a given + /// sibling appears with given tag. Results are sorted by occurrences count and the list is + /// truncated to the first 50 elements. Doesn't use paging. + pub async fn get_tag_siblings( + &self, + name: T, + ) -> SzurubooruResult> + where + T: AsRef + Display, + { + let path = format!("/tag-siblings/{name}"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Searches for posts. + /// See (PostNamedToken)[tokens::PostNamedToken], (PostSortToken)[tokens::PostSortToken] and + /// (PostSpecialToken)[tokens::PostSpecialToken] for valid tokens to use with this method, or + /// use (QueryToken)[tokens::QueryToken] to construct a custom token + pub async fn list_posts( + &self, + query: Option<&Vec>, + ) -> SzurubooruResult> { + self.do_request(Method::GET, "/posts", query, None::<&String>) + .await + } + + async fn create_update_post_from_url( + &self, + path: &str, + method: Method, + cupost: &CreateUpdatePost, + ) -> SzurubooruResult { + self.do_request(method, path, None, Some(cupost)).await + } + + /// Create a new post based on the `contentUrl` field, which the server will use to download + /// the image. + /// If specified tags do not exist yet, they will be automatically created. Tags created + /// automatically have no implications, no suggestions, one name and their category is set to + /// the first tag category found. (safety)[models::CreateUpdatePost::safety] must be any of + /// `safe`, `sketchy` or `unsafe`. + /// Relations must contain valid post IDs. If `flag` is omitted, they will be defined by + /// default (`"loop"` will be set for all video posts, and `"sound"` will be auto-detected). + /// Sending empty thumbnail will cause the post to use default thumbnail. If `anonymous` is set + /// to `true`, the uploader name won't be recorded (privilege verification still applies; + /// it's possible to disallow anonymous uploads completely from config.) + pub async fn create_post_from_url( + &self, + new_post: &CreateUpdatePost, + ) -> SzurubooruResult { + self.create_update_post_from_url("/posts", Method::POST, new_post) + .await + } + + /// Update an existing post based on the `contentUrl` field, which the server will use to + /// download and update the post + /// See [SzurubooruRequest::create_post_from_url] for more details about the fields in + /// (CreateUpdatePost)[models::CreateUpdatePost] + pub async fn update_post_from_url( + &self, + post_id: u32, + update_post: &CreateUpdatePost, + ) -> SzurubooruResult { + let path = format!("/posts/{post_id}"); + self.create_update_post_from_url(&path, Method::PUT, update_post) + .await + } + + fn part_from_file(&self, file: &mut File) -> SzurubooruResult { + let mut bytes = vec![]; + file.read_to_end(&mut bytes) + .map_err(SzurubooruClientError::IOError)?; + + Ok(Part::stream(bytes)) + } + + async fn create_update_post_from_file( + &self, + file: &mut File, + thumbnail: Option<&mut File>, + path: &str, + method: Method, + cupost: &CreateUpdatePost, + ) -> SzurubooruResult { + let request = self.prep_request(method, path, None); + + let metadata_str = + serde_json::to_string(cupost).map_err(SzurubooruClientError::JSONSerializationError)?; + let metadata_part = Part::text(metadata_str); + + let content_part = self.part_from_file(file)?; + + let mut form = Form::new() + .part("content", content_part) + .part("metadata", metadata_part); + + if let Some(thumbnail) = thumbnail { + let thumbnail_part = self.part_from_file(thumbnail)?; + form = form.part("thumbnail", thumbnail_part); + } + + self.handle_request(request.multipart(form)).await + } + + /// Create a new post from a file handle + /// See [SzurubooruRequest::create_post_from_url] for more details about the fields in + /// (CreateUpdatePost)[models::CreateUpdatePost] + pub async fn create_post_from_file( + &self, + file: &mut File, + thumbnail: Option<&mut File>, + new_post: &CreateUpdatePost, + ) -> SzurubooruResult { + self.create_update_post_from_file(file, thumbnail, "/posts", Method::POST, new_post) + .await + } + + /// Create a new post from a file path + /// See [SzurubooruRequest::create_post_from_url] for more details about the fields in + /// (CreateUpdatePost)[models::CreateUpdatePost] + pub async fn create_post_from_file_path( + &self, + file_path: impl AsRef, + thumbnail: Option>, + new_post: &CreateUpdatePost, + ) -> SzurubooruResult { + let mut file = File::open(file_path).map_err(SzurubooruClientError::IOError)?; + + let mut thumbnail_file = if let Some(t) = thumbnail { + Some(File::open(t).map_err(SzurubooruClientError::IOError)?) + } else { + None + }; + self.create_post_from_file(&mut file, thumbnail_file.as_mut(), new_post) + .await + } + + /// Update an existing post from an open File handle + /// See [SzurubooruRequest::create_post_from_url] for more details about the fields in + /// (CreateUpdatePost)[models::CreateUpdatePost] + pub async fn update_post_from_file( + &self, + post_id: u32, + file: &mut File, + thumbnail: Option<&mut File>, + update_post: &CreateUpdatePost, + ) -> SzurubooruResult { + let path = format!("/posts/{post_id}"); + self.create_update_post_from_file(file, thumbnail, &path, Method::PUT, update_post) + .await + } + + /// Update an existing post from a file path + /// See [SzurubooruRequest::create_post_from_url] for more details about the fields in + /// (CreateUpdatePost)[models::CreateUpdatePost] + pub async fn update_post_from_file_path( + &self, + post_id: u32, + file_path: impl AsRef, + thumbnail: Option>, + update_post: &CreateUpdatePost, + ) -> SzurubooruResult { + let mut file = File::open(file_path).map_err(SzurubooruClientError::IOError)?; + + let mut thumbnail_file = if let Some(t) = thumbnail { + Some(File::open(t).map_err(SzurubooruClientError::IOError)?) + } else { + None + }; + self.update_post_from_file(post_id, &mut file, thumbnail_file.as_mut(), update_post) + .await + } + + /// Retrieves posts that look like the input image + pub async fn reverse_search_file( + &self, + file: &mut File, + ) -> SzurubooruResult { + let request = self.prep_request(Method::POST, "/posts/reverse-search", None); + + let image_part = self.part_from_file(file)?; + let form = Form::new().part("content", image_part); + + self.handle_request(request.multipart(form)).await + } + + /// Retrieves posts that look like the input image from the given file path + pub async fn reverse_search_file_path( + &self, + file_path: impl AsRef, + ) -> SzurubooruResult { + let mut file = File::open(file_path).map_err(SzurubooruClientError::IOError)?; + + self.reverse_search_file(&mut file).await + } + + /// Searches for an exact match of a file based on the SHA1 checksum + pub async fn posts_for_file( + &self, + file: &mut File, + ) -> SzurubooruResult> { + let mut buffer = [0; 1024]; + let mut hasher = Sha1::new(); + let mut reader = BufReader::new(file); + + loop { + let n = reader + .read(&mut buffer) + .map_err(SzurubooruClientError::IOError)?; + hasher.update(buffer); + + if n == 0 { + break; + } + } + let result = hasher.finalize(); + let hex_string = hex::encode(result); + + let qt = QueryToken::token(PostNamedToken::ContentChecksum, hex_string); + self.list_posts(Some(&vec![qt])).await + } + + /// Searches for an exact match of a file path based on the SHA1 checksum + pub async fn posts_for_file_path( + &self, + file_path: impl AsRef, + ) -> SzurubooruResult> { + let mut file = File::open(file_path).map_err(SzurubooruClientError::IOError)?; + + self.posts_for_file(&mut file).await + } + + /// Retrieves information about an existing post. + pub async fn get_post(&self, post_id: i32) -> SzurubooruResult { + let path = format!("/post/{post_id}"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Retrieves information about posts that are before or after an existing post. + pub async fn get_around_post(&self, post_id: i32) -> SzurubooruResult { + let path = format!("/post/{post_id}/around"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Deletes existing post. Related posts and tags are kept. + pub async fn delete_post(&self, post_id: i32, version: u32) -> SzurubooruResult<()> { + let path = format!("/post/{post_id}"); + let version_obj = ResourceVersion { version }; + self.do_request(Method::DELETE, &path, None, Some(&version_obj)) + .await + } + + /// + /// Removes source post and merges all of its tags, relations, scores, favorites and comments to + /// the target post. If [MergePost::replace_content] is set to `true`, content of the target post + /// is replaced using the content of the source post; otherwise it remains unchanged. Source + /// post properties such as its safety, source, whether to loop the video and other scalar + /// values do not get transferred and are discarded. + /// + pub async fn merge_post(&self, merge_opts: &MergePost) -> SzurubooruResult { + self.do_request(Method::POST, "/post-merge/", None, Some(merge_opts)) + .await + } + + /// Updates score of authenticated user for given post. Valid scores are -1, 0 and 1. + pub async fn rate_post(&self, post_id: u32, score: i8) -> SzurubooruResult { + let rating_obj = RateResource { score }; + let path = format!("/post/{post_id}/score"); + self.do_request(Method::PUT, &path, None, Some(&rating_obj)) + .await + } + + /// Marks the post as favorite for authenticated user. + pub async fn favorite_post(&self, post_id: u32) -> SzurubooruResult { + let path = format!("/post/{post_id}/favorite"); + self.do_request(Method::POST, &path, None, None::<&String>) + .await + } + + /// Unmarks the post as favorite for authenticated user. + pub async fn unfavorite_post(&self, post_id: u32) -> SzurubooruResult { + let path = format!("/post/{post_id}/favorite"); + self.do_request(Method::DELETE, &path, None, None::<&String>) + .await + } + + /// Retrieves the post that is currently featured on the main page in web client. If no post is + /// featured, is [Option::None]. Note that this method exists mostly for compatibility + /// with setting featured post - most of the time, you'd want to use query global info which + /// contains more information. + pub async fn get_featured_post(&self) -> SzurubooruResult> { + self.do_request(Method::GET, "/featured-post", None, None::<&String>) + .await + } + + /// Features a post on the main page + pub async fn set_featured_post(&self, post_id: u32) -> SzurubooruResult { + let id_object = PostId { id: post_id }; + self.do_request(Method::POST, "/featured-post", None, Some(&id_object)) + .await + } + + /// Lists all pool categories. Doesn't use paging. + pub async fn list_pool_categories( + &self, + ) -> SzurubooruResult> { + self.do_request(Method::GET, "/pool-categories", None, None::<&String>) + .await + } + + /// Creates a new pool category using specified parameters. Name must match + /// `pool_category_name_regex` from server's configuration. First category created becomes + /// the default category. + pub async fn create_pool_category( + &self, + new_cat: &CreateUpdatePoolCategory, + ) -> SzurubooruResult { + self.do_request(Method::POST, "/pool-categories", None, Some(new_cat)) + .await + } + + /// Updates an existing tag category using specified parameters. Name must match + /// `tag_category_name_regex` from server's configuration. All fields except the + /// [version](models::CreateUpdatePoolCategory::version) field are optional - update concerns + /// only the provided fields. + pub async fn update_pool_category( + &self, + category_name: T, + update_cat: &CreateUpdatePoolCategory, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/pool-category/{category_name}"); + self.do_request(Method::PUT, &path, None, Some(update_cat)) + .await + } + + /// Retrieves information about an existing pool category. + pub async fn get_pool_category( + &self, + category_name: T, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/pool-category/{category_name}"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Deletes existing pool category. The pool category to be deleted must have no usages. + pub async fn delete_pool_category(&self, category_name: T) -> SzurubooruResult<()> + where + T: AsRef + Display, + { + let path = format!("/pool-category/{category_name}"); + self.do_request(Method::DELETE, &path, None, None::<&String>) + .await + } + + /// Sets given pool category as default. All new pools created manually or automatically will + /// have this category. + pub async fn set_default_pool_category( + &self, + category_name: T, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/pool-category/{category_name}/default"); + self.do_request(Method::PUT, &path, None, None::<&String>) + .await + } + + /// Searches for pools. + /// Anonymous tokens are the same as the [name](tokens::PoolNamedToken::Name) token + pub async fn list_pools( + &self, + query: Option<&Vec>, + ) -> SzurubooruResult> { + self.do_request(Method::GET, "/pools", query, None::<&String>) + .await + } + + /// Creates a new pool using specified parameters. Names, suggestions and implications must + /// match `pool_name_regex` from server's configuration. Category must exist and is the same as + /// [name](models::PoolCategoryResource::name) field. + /// [posts](models::CreateUpdatePool::posts) is an optional list of integer post IDs. If the + /// specified posts do not exist, an error will be thrown. + pub async fn create_pool( + &self, + create_update_pool: &CreateUpdatePool, + ) -> SzurubooruResult { + self.do_request(Method::POST, "/pool", None, Some(create_update_pool)) + .await + } + + /// Updates an existing pool using specified parameters. [name](models::CreateUpdatePool::name), + /// must match `pool_name_regex` from server's configuration. + /// [category](models::CreateUpdatePool::category) must exist and is the same as + /// [name](models::PoolCategoryResource::name) field. [posts](models::CreateUpdatePool::posts) + /// is an optional list of integer post IDs. If the specified posts do not exist yet, an error + /// will be thrown. The full list of post IDs must be provided if they are being updated, and + /// the previous list of posts will be replaced with the new one. All fields except + /// [version](models::CreateUpdatePool::version) are optional - update concerns only provided + /// fields. + pub async fn update_pool( + &self, + pool_id: u32, + create_update_pool: &CreateUpdatePool, + ) -> SzurubooruResult { + let path = format!("/pool/{pool_id}"); + self.do_request(Method::POST, &path, None, Some(create_update_pool)) + .await + } + + /// Retrieves information about an existing pool. + pub async fn get_pool(&self, pool_id: u32) -> SzurubooruResult { + let path = format!("/pool/{pool_id}"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Deletes existing pool. All posts in the pool will only have their relation to the pool + /// removed. + pub async fn delete_pool(&self, pool_id: u32) -> SzurubooruResult<()> { + let path = format!("/pool/{pool_id}"); + self.do_request(Method::DELETE, &path, None, None::<&String>) + .await + } + + /// Removes source pool and merges all of its posts with the target pool. Other pool properties + /// such as category and aliases do not get transferred and are discarded. + pub async fn merge_pools(&self, merge_pool: &MergePool) -> SzurubooruResult { + self.do_request(Method::POST, "/pool-merge", None, Some(merge_pool)) + .await + } + + /// Searches for comments. + /// Anonymous tokens are the same as the [text](tokens::CommentNamedToken::text) token + pub async fn list_comments( + &self, + query: Option<&Vec>, + ) -> SzurubooruResult> { + self.do_request(Method::GET, "/comments", query, None::<&String>) + .await + } + + /// Creates a new comment under given post + pub async fn create_comment( + &self, + new_comment: &CreateUpdateComment, + ) -> SzurubooruResult { + self.do_request(Method::POST, "/comments", None, Some(new_comment)) + .await + } + + /// Updates an existing comment text + pub async fn update_comment( + &self, + comment_id: u32, + update_comment: &CreateUpdateComment, + ) -> SzurubooruResult { + let path = format!("/comment/{comment_id}"); + self.do_request(Method::PUT, &path, None, Some(update_comment)) + .await + } + + /// Retrieves information about an existing comment + pub async fn get_comment(&self, comment_id: u32) -> SzurubooruResult { + let path = format!("/comment/{comment_id}"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Deletes existing comment + pub async fn delete_comment(&self, comment_id: u32, version: u32) -> SzurubooruResult<()> { + let path = format!("/comment/{comment_id}"); + let version_obj = ResourceVersion { version }; + self.do_request(Method::DELETE, &path, None, Some(&version_obj)) + .await + } + + /// Updates score of authenticated user for given comment. Valid scores are -1, 0 and 1. + pub async fn rate_comment(&self, comment_id: u32, score: i8) -> SzurubooruResult<()> { + let path = format!("/comment/{comment_id}/score"); + let rating = RateResource { score }; + self.do_request(Method::PUT, &path, None, Some(&rating)) + .await + } + + /// Searches for users + /// Anonymous tokens are the same as the [name](tokens::UserNamedToken) + /// See [UserNamedToken](tokens::UserNamedToken) and [UserSortToken](tokens::UserSortToken) + /// for type-safe tokens + pub async fn list_users( + &self, + query: Option<&Vec>, + ) -> SzurubooruResult> { + self.do_request(Method::GET, "/users", query, None::<&String>) + .await + } + + async fn create_update_user( + &self, + method: Method, + path: &str, + new_user: &CreateUpdateUser, + file: Option<&mut File>, + ) -> SzurubooruResult { + match file { + None => self.do_request(method, path, None, Some(new_user)).await, + Some(file) => { + let request = self.prep_request(method, path, None); + + let metadata_str = serde_json::to_string(&new_user) + .map_err(SzurubooruClientError::JSONSerializationError)?; + let metadata_part = Part::text(metadata_str); + + let content_part = self.part_from_file(file)?; + + let form = Form::new() + .part("avatar", content_part) + .part("metadata", metadata_part); + + self.handle_request(request.multipart(form)).await + } + } + } + + /// Creates a new user using specified parameters. Names and passwords must match + /// `user_name_regex` and `password_regex` from server's configuration, respectively. + /// Email address, rank and avatar fields are optional. Avatar style can be either + /// [gravatar](models::UserAvatarStyle::Gravatar) or [manual](models::UserAvatarStyle::Manual). + /// `manual` avatar style requires client to pass also the `avatar` file. + /// If the rank is empty and the user happens to be the first user ever created, + /// become an administrator, whereas subsequent users will be given the rank indicated by + /// `default_rank` in the server's configuration. + pub async fn create_user(&self, new_user: &CreateUpdateUser) -> SzurubooruResult { + self.do_request(Method::POST, "/users", None, Some(new_user)) + .await + } + + /// Create a [UserResource](models::UserResource) with the included Avatar file + /// See [create_user](SzurubooruRequest::create_user) for other applicable fields and + /// restrictions + pub async fn create_user_with_avatar_file( + &self, + avatar: &mut File, + new_user: &CreateUpdateUser, + ) -> SzurubooruResult { + self.create_update_user(Method::POST, "/users", new_user, Some(avatar)) + .await + } + + /// Create a [UserResource](models::UserResource) with the included Avatar file path + /// See [create_user](SzurubooruRequest::create_user) for other applicable fields and + /// restrictions + pub async fn create_user_with_avatar_path( + &self, + avatar_path: impl AsRef, + new_user: &CreateUpdateUser, + ) -> SzurubooruResult { + let mut file = File::open(avatar_path).map_err(SzurubooruClientError::IOError)?; + self.create_update_user(Method::POST, "/users", new_user, Some(&mut file)) + .await + } + + /// Updates user using specified parameters. Names and passwords must match + /// `user_name_regex` and `password_regex` from server's configuration, respectively. + /// Email address, rank and avatar fields are optional. Avatar style can be either + /// [gravatar](models::UserAvatarStyle::Gravatar) or [manual](models::UserAvatarStyle::Manual). + /// `manual` avatar style requires client to pass also the `avatar` file. + /// All fields except the [version](models::CreateUpdateUser::version) are optional + /// - update concerns only provided fields. + pub async fn update_user( + &self, + name: T, + update_user: &CreateUpdateUser, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/users/{name}"); + self.do_request(Method::POST, path, None, Some(update_user)) + .await + } + + /// Update a [UserResource](models::UserResource) with the included Avatar file + /// See [update_user](SzurubooruRequest::update_user) for other applicable fields and + /// restrictions + pub async fn update_user_with_avatar_file( + &self, + name: T, + avatar: &mut File, + update_user: &CreateUpdateUser, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/users/{name}"); + self.create_update_user(Method::POST, &path, update_user, Some(avatar)) + .await + } + + /// Update a [UserResource](models::UserResource) with the included Avatar file path + /// See [update_user](SzurubooruRequest::update_user) for other applicable fields and + /// restrictions + pub async fn update_user_with_avatar_path( + &self, + name: T, + avatar_path: impl AsRef, + new_user: &CreateUpdateUser, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/users/{name}"); + let mut file = File::open(avatar_path).map_err(SzurubooruClientError::IOError)?; + self.create_update_user(Method::POST, &path, new_user, Some(&mut file)) + .await + } + + /// Retrieves information about an existing user + pub async fn get_user(&self, name: T) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/users/{name}"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Deletes existing user + pub async fn delete_user(&self, name: T, version: u32) -> SzurubooruResult<()> + where + T: AsRef + Display, + { + let path = format!("/users/{name}"); + let version_obj = ResourceVersion { version }; + self.do_request(Method::DELETE, &path, None, Some(&version_obj)) + .await + } + + /// Listing user tokens for the given user. + pub async fn list_user_tokens( + &self, + name: T, + ) -> SzurubooruResult> + where + T: AsRef + Display, + { + let path = format!("/user-tokens/{name}"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Creates a new user token that can be used for authentication of API endpoints + /// instead of a password. + pub async fn create_user_token( + &self, + name: T, + create_token: &CreateUpdateUserAuthToken, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/user-tokens/{name}"); + self.do_request(Method::POST, &path, None, Some(create_token)) + .await + } + + /// Updates an existing user token using specified parameters. All fields except the + /// [version](models::CreateUpdateUserAuthToken::version) are optional - update concerns only + /// provided fields. + pub async fn update_user_token( + &self, + name: T, + token: T, + update_token: &CreateUpdateUserAuthToken, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let path = format!("/user-tokens/{name}/{token}"); + self.do_request(Method::PUT, &path, None, Some(update_token)) + .await + } + + /// Deletes an existing user token using specified parameters. All fields except the + /// [version](models::CreateUpdateUserAuthToken::version) are optional - update concerns only + /// provided fields. + pub async fn delete_user_token( + &self, + name: T, + token: T, + version: u32, + ) -> SzurubooruResult<()> + where + T: AsRef + Display, + { + let path = format!("/user-tokens/{name}/{token}"); + let version_obj = ResourceVersion { version }; + self.do_request(Method::DELETE, &path, None, Some(&version_obj)) + .await + } + + /// Sends a confirmation email to given user. The email contains link containing a token. The + /// token cannot be guessed, thus using such link proves that the person who requested to reset + /// the password also owns the mailbox, which is a strong indication they are the rightful + /// owner of the account. + /// Argument is either the user's username or email address + pub async fn password_reset_request(&self, email_or_name: T) -> SzurubooruResult<()> + where + T: AsRef + Display, + { + let encoded = STANDARD.encode(email_or_name.as_ref().as_bytes()); + let path = format!("/password-reset/{encoded}"); + self.do_request(Method::GET, &path, None, None::<&String>) + .await + } + + /// Generates a new password for given user. Password is sent as plain-text, so it is + /// recommended to connect through HTTPS. + pub async fn password_reset_confirm( + &self, + email_or_name: T, + token: impl AsRef, + ) -> SzurubooruResult + where + T: AsRef + Display, + { + let encoded = STANDARD.encode(email_or_name.as_ref().as_bytes()); + let path = format!("/password-reset/{encoded}"); + let token_obj = PasswordResetToken { + token: token.as_ref().to_string(), + }; + self.do_request(Method::POST, &path, None, Some(&token_obj)) + .await + } + + /// Lists recent resource snapshots. + /// See [SnapshotNamedToken](tokens::SnapshotNamedToken) for query tokens. + /// There are no sort tokens. The snapshots are always sorted by creation time. + pub async fn list_snapshots( + &self, + query: Option<&Vec>, + ) -> SzurubooruResult> { + self.do_request(Method::GET, "/snapshots", query, None::<&String>) + .await + } + + /// Retrieves simple statistics. [featured_post](models::GlobalInfo::featured_post) is + /// [None](Option::None) if there is no featured post yet. + /// [server_time](models::GlobalInfo::server_time) is pretty much the same as the Date HTTP + /// field, only formatted in a manner consistent with other dates. Values in config key are + /// taken directly from the server config, with the exception of privilege array keys being + /// converted to lower camel case to match the API convention. + pub async fn get_global_info(&self) -> SzurubooruResult { + self.do_request(Method::GET, "/info", None, None::<&String>) + .await + } + + /// Puts a file in temporary storage and assigns it a token that can be used in other requests. + /// The files uploaded that way are deleted after a short while so clients shouldn't use it + /// as a free upload service. + pub async fn upload_temporary_file( + &self, + file: &mut File, + ) -> SzurubooruResult { + let request = self.prep_request(Method::POST, "/uploads", None); + + let content_part = self.part_from_file(file)?; + + let form = Form::new().part("content", content_part); + + self.handle_request(request.multipart(form)).await + } +} + +/// Which kind of authentication is used. Automatically hides any sensitive information when printed +/// using [Debug](std::fmt::Debug) +enum SzurubooruAuth { + // The encoded token + TokenAuth(String), + BasicAuth(String, String), + #[allow(dead_code)] + None, +} + +impl std::fmt::Debug for SzurubooruAuth { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "SzurubooruAuth ()") + } +} diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..6670beb --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,199 @@ +//! Types that represent the various error states that can occur when interacting with +//! Szurubooru + +use crate::models::SzuruEither; +use base64::EncodeSliceError; +use serde::{Deserialize, Serialize}; +use thiserror::Error; +use url::ParseError as UParseError; + +/// Trait to support conversion into a [SzurubooruResult] +#[doc(hidden)] +pub trait IntoClientResult { + /// Convert `self` into a SzurubooruResult + fn into_result(self) -> SzurubooruResult; +} + +#[derive(Debug, Error)] +/// Type that represents the various error states that can occur when interacting with +/// Szurubooru +pub enum SzurubooruClientError { + /// Error occurred when trying to Bas64 encode the `username:token` string + #[error("Error encoding authentication token: {0}")] + Base64EncodingError(#[source] EncodeSliceError), + /// Error parsing the given host URL + #[error("Error parsing URL {url}: {source}")] + UrlParseError { + /// The resulting error + source: UParseError, + /// The URL in question + url: String, + }, + /// Error occurred building the request before it's sent to the server + #[error("Error building request {0}")] + RequestBuilderError(#[source] reqwest::Error), + /// Error occurred pas part of the request to the server + #[error("Request error {0}")] + RequestError(#[source] reqwest::Error), + /// Error parsing the JSON response from the server + #[error("Response Parsing error: {0}: {1}")] + ResponseParsingError( + /// The JSON parsing error + #[source] + serde_json::Error, + /// The string we attempted to parse + String, + ), + /// Error serializing an object as JSON + #[error("JSON Serialization error: {0}")] + JSONSerializationError(#[source] serde_json::Error), + /// Error occurred when reading a file + #[error("IO Error: {0}")] + IOError(#[source] std::io::Error), + /// Error returned by the Szurubooru server + #[error("Error returned from Szurubooru host: {0:?}")] + SzurubooruServerError(SzurubooruServerError), +} + +impl From for SzurubooruClientError { + fn from(value: SzurubooruServerError) -> Self { + SzurubooruClientError::SzurubooruServerError(value) + } +} + +/// Type used to represent success or a failure of some kind +pub type SzurubooruResult = Result; + +#[doc(hidden)] +impl IntoClientResult for SzuruEither { + fn into_result(self) -> SzurubooruResult { + match self { + SzuruEither::Left(v) => Ok(v), + SzuruEither::Right(e) => Err(SzurubooruClientError::SzurubooruServerError(e)), + } + } +} + +#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)] +/// An error type returned by the server +pub enum SzurubooruServerErrorType { + /// Missing required file + MissingRequiredFileError, + /// Missing required parameter + MissingRequiredParameterError, + /// Invalid parameter + InvalidParameterError, + /// Integrity error + IntegrityError, + /// Search error + SearchError, + /// Invalid authentication + AuthError, + /// Post with the given ID not found + PostNotFoundError, + /// Post is already featured + PostAlreadyFeaturedError, + /// Post is already uploaded + PostAlreadyUploadedError, + /// Invalid post ID + InvalidPostIdError, + /// Invalid Post Safety + InvalidPostSafetyError, + /// Invalid post source + InvalidPostSourceError, + /// Invalid post content + InvalidPostContentError, + /// Invalid post relation + InvalidPostRelationError, + /// Invalid post note + InvalidPostNoteError, + /// Invalid post flag + InvalidPostFlagError, + /// Invalid favorite target + InvalidFavoriteTargetError, + /// Invalid comment ID + InvalidCommentIdError, + /// Comment not found + CommentNotFoundError, + /// Empty comment text + EmptyCommentTextError, + /// Invalid score target + InvalidScoreTargetError, + /// Invalid score value + InvalidScoreValueError, + /// Tag category not found + TagCategoryNotFoundError, + /// Tag category already exists + TagCategoryAlreadyExistsError, + /// Tag category is in use + TagCategoryIsInUseError, + /// Invalid tag category name + InvalidTagCategoryNameError, + /// Invalid tag category color + InvalidTagCategoryColorError, + /// Tag not found + TagNotFoundError, + /// Tag already exists + TagAlreadyExistsError, + /// Tag is in use + TagIsInUseError, + /// Invalid tag name + InvalidTagNameError, + /// Invalid tag relation + InvalidTagRelationError, + /// Invalid tag category + InvalidTagCategoryError, + /// Invalid tag description + InvalidTagDescriptionError, + /// User not found + UserNotFoundError, + /// User already exists + UserAlreadyExistsError, + /// Invalid user name + InvalidUserNameError, + /// Invalid email + InvalidEmailError, + /// Invalid password + InvalidPasswordError, + /// Invalid rank + InvalidRankError, + /// Invalid avatar + InvalidAvatarError, + /// Processing error + ProcessingError, + /// Validation error + ValidationError, +} + +#[derive(Debug, Serialize, Deserialize)] +/// Type describing an error returned from Szurubooru +pub struct SzurubooruServerError { + /// The name (or type) of error + pub name: SzurubooruServerErrorType, + /// Title of the error + pub title: String, + /// More of a description of the error + pub description: String, +} + +#[cfg(test)] +mod test { + use super::*; + use serde_json; + + #[test] + fn test_parse_server_error() { + let json_response = r#"{ + "name": "ValidationError", + "title": "Validation Error", + "description": "Some sort of validation error" + }"#; + + let sse = serde_json::from_str::(json_response) + .expect("Failed to parse the JSON response"); + + assert_eq!(sse.name, SzurubooruServerErrorType::ValidationError); + assert_eq!(sse.title, "Validation Error"); + assert_eq!(sse.description, "Some sort of validation error"); + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..aa9ec4c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,16 @@ +//! The core library for SzurubooruClient +#![warn(missing_docs)] +#![warn(rustdoc::missing_crate_level_docs)] + +/// Core client module +pub mod client; +pub use client::SzurubooruClient; +pub use client::SzurubooruRequest; + +pub mod errors; +pub use errors::SzurubooruResult; +pub mod models; + +#[cfg(test)] +mod tests; +pub mod tokens; diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..7cd6967 --- /dev/null +++ b/src/models.rs @@ -0,0 +1,925 @@ +//! Types that represent the various API objects returned by Szurubooru. Many of the `Resource` +//! objects have all of their fields as [Option] types because the Server API supports field +//! selection. +//! +//! See [here](https://github.com/rr-/szurubooru/blob/master/doc/API.md#field-selecting) for +//! more information. + +use chrono::NaiveDateTime; +use derive_builder::Builder; +use serde::{Deserialize, Serialize}; +use strum_macros::AsRefStr; + +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(untagged)] +/// Enum used to represent something that's either `Left` or `Right` +pub enum SzuruEither { + /// Enum variant `Left` + Left(L), + /// Enum variant `Right` + Right(R), +} + +#[derive(Debug, Serialize, Deserialize)] +/// A result of search operation that doesn't involve paging +pub struct UnpagedSearchResult { + /// The total list of results + pub results: Vec, +} + +#[derive(Debug, Serialize, Deserialize)] +/// A result of search operation that involves paging +/// +/// Use (offset)[crate::SzurubooruRequest::offset] and (limit)[crate::SzurubooruRequest::limit] +/// to fetch the next page +pub struct PagedSearchResult { + /// The original query for the request + pub query: String, + /// The number of [T] to skip forward + pub offset: u32, + /// The maximum number of [T] to return + pub limit: u32, + /// The total number of [T] that match the [query](PagedSearchResult::query) + pub total: u32, + /// The results themselves + pub results: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +/// A [tag resource](TagResource) stripped down to `names`, `category` and `usages` fields. +pub struct MicroTagResource { + /// The tag names and aliases + pub names: Vec, + /// The category this tag belongs to + pub category: String, + /// The number of times this tag has been used + pub usages: u32, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +/// To prevent problems with concurrent resource modification, Szurubooru implements optimistic +/// locks using resource versions. Each modifiable resource has its version returned to the client +/// with `GET` requests. At the same time, each `PUT` and `DELETE` request sent by the client +/// must present the same version field to the server with value as it was given in `GET`. +/// +/// For example, given `GET /post/1`, the server responds like this: +/// +/// ```json +/// { +/// ..., +/// "version": 2 +/// } +/// ``` +/// +/// This means the client must then send `{"version": 2}` back too. If the client fails to do so, +/// the server will reject the request notifying about missing parameter. If someone has edited the +/// post in the meantime, the server will reject the request as well, in which case the client is +/// encouraged to notify the user about the situation. +pub struct ResourceVersion { + /// The version itself + pub version: u32, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +/// A single tag. Tags are used to let users search for posts. +pub struct TagResource { + /// resource version. See [versioning](ResourceVersion) + pub version: u32, + /// a list of tag names (aliases). Tagging a post with any name will automatically assign + /// the first name from this list. + pub names: Option>, + /// the name of the category the given tag belongs to + pub category: Option, + /// a list of implied tags, serialized as micro tag resource. Implied tags are automatically + /// appended by the web client on usage. + pub implications: Option>, + /// a list of suggested tags, serialized as micro tag resource. Suggested tags are shown to + /// the user by the web client on usage + pub suggestions: Option>, + /// time the tag was created + pub creation_time: Option, + /// time the tag was edited + pub last_edit_time: Option, + /// the number of posts the tag was used in + pub usages: Option, + /// the tag description (instructions how to use, history etc.) The client should render + /// is as Markdown + pub description: Option, +} + +/// Creates or updates a tag using specified parameters. Names, suggestions and implications must +/// match `tag_name_regex` from server's configuration. Category must exist and is the same as name +/// field within resource. Suggestions and implications are optional. If specified +/// implied tags or suggested tags do not exist yet, they will be automatically created. Tags +/// created automatically have no implications, no suggestions, one name and their category is set +/// to the first tag category found. If there are no tag categories established yet, an error +/// will be thrown. +/// +/// ```no_run +/// use szurubooru_client::models::CreateUpdateTagBuilder; +/// let cu_tag = CreateUpdateTagBuilder::default() +/// .version(1) +/// .names(vec!["foo_tag".to_string()]) +/// .build() +/// .expect("A new tag"); +/// ``` +#[derive(Debug, Clone, Serialize, Deserialize, Builder, Default)] +#[builder(setter(strip_option))] +pub struct CreateUpdateTag { + #[serde(skip_serializing_if = "Option::is_none")] + /// resource version. See [versioning](ResourceVersion) + pub version: Option, + #[serde(skip_serializing_if = "Option::is_none")] + /// Tag names and aliases, must match `tag_name_regex` from the server's configuration + pub names: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + /// Category that this tag belongs to. Must already exist + pub category: Option, + #[serde(skip_serializing_if = "Option::is_none")] + /// The tag description in Markdown format + pub description: Option, + #[serde(skip_serializing_if = "Option::is_none")] + /// Tags that should be implied when this tag is used + pub implications: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + /// Tags that should be suggested when this tag is used + pub suggestions: Option>, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +/// A single tag category. The primary purpose of tag categories is to distinguish certain tag +/// types (such as characters, media type etc.), which improves user experience. +pub struct TagCategoryResource { + /// resource version. See [versioning](ResourceVersion) + pub version: u32, + /// The name of the tag category + pub name: Option, + /// The display color of the tag category + pub color: Option, + /// How many tags is the given category used with + pub usages: Option, + /// The order in which tags with this category are displayed, ascending + pub order: Option, + /// Whether the tag category is the default one + pub default: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Builder)] +#[builder(setter(into))] +/// Used for creating or updating a Tag Category +pub struct CreateUpdateTagCategory { + /// The name of the category to create + pub name: String, + /// The display color to use for the category + pub color: String, + /// The order in which tags with this category are displayed, ascending + pub order: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Builder)] +#[serde(rename_all = "camelCase")] +#[builder(setter(into))] +/// Removes source tag and merges all of its usages, suggestions and implications to the target tag. +/// Other tag properties such as category and aliases do not get transferred and are discarded. +pub struct MergeTags { + /// Version of the tag to remove + pub remove_version: u32, + /// The name of the tag to remove + pub remove: String, + /// The version of the tag to merge TO + pub merge_to_version: u32, + /// The name of the tag to merge TO + pub merge_to: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +/// Lists siblings of given tag, e.g. tags that were used in the same posts as the given tag +pub struct TagSibling { + /// The related tag + pub tag: TagResource, + /// How many times a given tag appears with the given tag + pub occurrences: u32, +} + +#[derive(Debug, Clone, Serialize, Deserialize, AsRefStr)] +/// The type of post +pub enum PostType { + /// Image post + Image, + /// Animated post + Animation, + /// Alias of [Animation](PostType::Animation) + Animated, + /// Alias of [Animation](PostType::Animation) + Anim, + /// Flash animation + Flash, + /// Alias of [Flash](PostType::Flash) + Swf, + /// Video post of some type. See the mime type for more information + Video, + /// Webm container type + Webm, +} + +#[derive(Debug, Clone, Serialize, Deserialize, AsRefStr)] +/// How SFW/NSFW the post is +pub enum PostSafety { + /// Post is SFW + Safe, + /// Post is possibly NSFW + Sketchy, + /// Alias of (Sketchy)[PostSafety::Sketchy] + Questionable, + /// Post is NSFW + Unsafe, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +/// A post resource stripped down to `id` and `thumbnailUrl` fields. +pub struct MicroPostResource { + /// The ID of the post + pub id: u32, + /// The thumbnail URL of the post + pub thumbnail_url: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[doc(hidden)] +pub(crate) struct PostId { + pub id: u32, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +/// A post resource +pub struct PostResource { + /// Resource version. See [versioning](ResourceVersion) + pub version: Option, + /// The post identifier + pub id: Option, + /// Time the post was created + pub creation_time: Option, + /// Time the post was edited + pub last_edit_time: Option, + /// Whether the post is safe for work + pub safety: Option, + #[serde(rename = "type")] + /// The type of the post + pub post_type: Option, + /// Where the post was grabbed form, supplied by the user + pub source: Option, + /// The SHA1 file checksum. Used in snapshots to signify changes of the post content + pub checksum: Option, + #[serde(rename = "checksumMD5")] + /// The MD5 file checksum + pub checksum_md5: Option, + /// The original width of the post content. + pub canvas_width: Option, + /// The original height of the post content. + pub canvas_height: Option, + /// Where the post content is located + pub content_url: Option, + /// Where the post thumbnail is located + pub thumbnail_url: Option, + /// Various flags such as whether the post is looped + pub flags: Option>, + /// List of tags the post is tagged with + pub tags: Option>, + /// A list of related posts. + pub relations: Option>, + /// A list of post annotations + pub notes: Option>, + /// Who created the post + pub user: Option, + /// The collective score (+1/-1 rating) of the given post + pub score: Option, + /// Whether the authenticated user has given post in their favorites + pub own_favorite: Option, + /// How many tags the post is tagged with + pub tag_count: Option, + /// How many users have the post in their favorites + pub favorite_count: Option, + /// How many comments are filed under that post + pub comment_count: Option, + /// How many notes the post has + pub note_count: Option, + /// How many times has the post been featured + pub feature_count: Option, + /// How many posts are related to this post + pub relation_count: Option, + /// The last time the post was featured + pub last_feature_time: Option, + /// List of users who have favorited this post + pub favorited_by: Option>, + /// Whether the post uses custom thumbnail + pub has_custom_thumbnail: Option, + /// Subsidiary to [type](PostResource::post_type), used to tell exact content format; + /// useful for `