From 0df3f9fa2b0e48a288c54dcc67c364ec9a454a95 Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Mon, 27 Aug 2018 16:06:00 -0400 Subject: [PATCH] Rework the `Registration` and `App` APIs This puts `register` back to the way it was, and changes the "new" `register` to `build`. --- README.md | 2 +- examples/register.rs | 2 +- src/apps.rs | 18 +++++++++++++++ src/lib.rs | 2 +- src/registration.rs | 54 ++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 71 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f25c9f4..e89d1dd 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ fn main() -> Result<(), Box> { fn register() -> Result> { let registration = Registration::new("https://mastodon.social") .client_name("elefren-examples") - .register()?; + .build()?; let url = registration.authorize_url()?; println!("Click this link to authorize on Mastodon: {}", url); diff --git a/examples/register.rs b/examples/register.rs index c413b99..f375921 100644 --- a/examples/register.rs +++ b/examples/register.rs @@ -33,7 +33,7 @@ pub fn register() -> Result> { .client_name("elefren-examples") .scopes(Scopes::All) .website("https://github.com/pwoolcoc/elefren") - .register()?; + .build()?; let url = registration.authorize_url()?; println!("Click this link to authorize on Mastodon: {}", url); diff --git a/src/apps.rs b/src/apps.rs index 11f729f..39c1124 100644 --- a/src/apps.rs +++ b/src/apps.rs @@ -1,5 +1,7 @@ use std::{borrow::Cow, fmt}; +use try_from::TryInto; + use errors::{Error, Result}; /// Represents an application that can be registered with a mastodon instance @@ -97,6 +99,22 @@ impl<'a> AppBuilder<'a> { } } +impl TryInto for App { + type Err = Error; + + fn try_into(self) -> Result { + Ok(self) + } +} + +impl<'a> TryInto for AppBuilder<'a> { + type Err = Error; + + fn try_into(self) -> Result { + Ok(self.build()?) + } +} + /// Permission scope of the application. /// [Details on what each permission provides][1] /// [1]: https://github.com/tootsuite/documentation/blob/master/Using-the-API/OAuth-details.md) diff --git a/src/lib.rs b/src/lib.rs index 8b2da1f..b7b9770 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ //! //! let registration = Registration::new("https://mastodon.social") //! .client_name("elefren_test") -//! .register()?; +//! .build()?; //! let url = registration.authorize_url()?; //! // Here you now need to open the url in the browser //! // And handle a the redirect url coming back with the code. diff --git a/src/registration.rs b/src/registration.rs index 5a50cd3..f7787eb 100644 --- a/src/registration.rs +++ b/src/registration.rs @@ -1,5 +1,6 @@ use std::borrow::Cow; +use try_from::TryInto; use reqwest::{Client, RequestBuilder, Response}; use apps::{App, AppBuilder, Scopes}; @@ -8,6 +9,7 @@ use Data; use Mastodon; use MastodonBuilder; use Result; +use Error; const DEFAULT_REDIRECT_URI: &'static str = "urn:ietf:wg:oauth:2.0:oob"; @@ -89,6 +91,46 @@ impl<'a, H: HttpSend> Registration<'a, H> { Ok(self.http_sender.send(&self.client, req)?) } + /// Register the given application + /// + /// ```no_run + /// # extern crate elefren; + /// # fn main () -> elefren::Result<()> { + /// use elefren::prelude::*; + /// use elefren::apps::App; + /// + /// let mut app = App::builder(); + /// app.client_name("elefren_test"); + /// + /// let registration = Registration::new("https://mastodon.social") + /// .register(app)?; + /// let url = registration.authorize_url()?; + /// // Here you now need to open the url in the browser + /// // And handle a the redirect url coming back with the code. + /// let code = String::from("RETURNED_FROM_BROWSER"); + /// let mastodon = registration.complete(&code)?; + /// + /// println!("{:?}", mastodon.get_home_timeline()?.initial_items); + /// # Ok(()) + /// # } + /// ``` + pub fn register>(&mut self, app: I) -> Result> + where Error: From<>::Err> + { + let app = app.try_into()?; + let oauth = self.send_app(&app)?; + + Ok(Registered { + base: self.base.clone(), + client: self.client.clone(), + client_id: oauth.client_id, + client_secret: oauth.client_secret, + redirect: oauth.redirect_uri, + scopes: app.scopes(), + http_sender: self.http_sender.clone(), + }) + } + /// Register the application with the server from the `base` url. /// /// ```no_run @@ -98,7 +140,7 @@ impl<'a, H: HttpSend> Registration<'a, H> { /// /// let registration = Registration::new("https://mastodon.social") /// .client_name("elefren_test") - /// .register()?; + /// .build()?; /// let url = registration.authorize_url()?; /// // Here you now need to open the url in the browser /// // And handle a the redirect url coming back with the code. @@ -109,10 +151,9 @@ impl<'a, H: HttpSend> Registration<'a, H> { /// # Ok(()) /// # } /// ``` - pub fn register(&mut self) -> Result> { + pub fn build(&mut self) -> Result> { let app: App = self.app_builder.clone().build()?; - let url = format!("{}/api/v1/apps", self.base); - let oauth: OAuth = self.send(self.client.post(&url).form(&app))?.json()?; + let oauth = self.send_app(&app)?; Ok(Registered { base: self.base.clone(), @@ -124,6 +165,11 @@ impl<'a, H: HttpSend> Registration<'a, H> { http_sender: self.http_sender.clone(), }) } + + fn send_app(&self, app: &App) -> Result { + let url = format!("{}/api/v1/apps", self.base); + Ok(self.send(self.client.post(&url).form(&app))?.json()?) + } } impl Registered {