From 5dd96b31794072ea75e2c412333304163e16749d Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Wed, 20 Oct 2021 19:19:27 +0200 Subject: [PATCH] Add ListenBrainz::new_with_url constructor Analogous to raw::Client::new_with_url --- src/lib.rs | 18 +++++++++++++++++- src/wrapper.rs | 8 ++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 905a588..381a727 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,12 +4,15 @@ //! It contains functionality for direct access to the API in the [`raw`] module, as well //! as a more convenient [`ListenBrainz`] client which is easier to use. //! +//! [ListenBrainz HTTP API]: https://listenbrainz.readthedocs.io/en/production/dev/api/ +//! //! Generally, using the `raw` functionality is more cumbersome, as its types and functions //! map one-to-one to the HTTP API's JSON input- and response data. Using the `ListenBrainz` //! type is therefore recommended. //! //! # Example //! +//! Submit a currently playing song to ListenBrainz.org: //! ```no_run //! # use listenbrainz::ListenBrainz; //! # @@ -22,7 +25,20 @@ //! .expect("Could not submit 'playing now' request"); //! ``` //! -//! [ListenBrainz HTTP API]: https://listenbrainz.readthedocs.io/en/production/dev/api/ +//! Use a custom API URL, for example to submit songs to [Maloja]: +//! ```no_run +//! # use listenbrainz::ListenBrainz; +//! # +//! let mut client = ListenBrainz::new_with_url("http://maloja.example.com/apis/listenbrainz"); +//! +//! client.authenticate("MALOJA API KEY") +//! .expect("Could not authenticate with Maloja"); +//! +//! client.listen("Lymbyc Systym", "Split Stones", "Split Stones") +//! .expect("Could not submit listen"); +//! ``` +//! +//! [Maloja](https://github.com/krateng/maloja) #![deny( missing_docs, diff --git a/src/wrapper.rs b/src/wrapper.rs index 96aee1f..f51e8bc 100644 --- a/src/wrapper.rs +++ b/src/wrapper.rs @@ -32,6 +32,14 @@ impl ListenBrainz { } } + /// Construct a new ListenBrainz client with a custom API URL that is not authenticated. + pub fn new_with_url(url: &str) -> Self { + Self { + client: Client::new_with_url(url), + auth: None, + } + } + /// Check if this client is authenticated. pub fn is_authenticated(&self) -> bool { self.auth.is_some()