Rework the `Registration` and `App` APIs

This puts `register` back to the way it was, and changes the "new"
`register` to `build`.
master
Paul Woolcock 6 years ago
parent 39e5aacfb5
commit 0df3f9fa2b
  1. 2
      README.md
  2. 2
      examples/register.rs
  3. 18
      src/apps.rs
  4. 2
      src/lib.rs
  5. 54
      src/registration.rs

@ -58,7 +58,7 @@ fn main() -> Result<(), Box<Error>> {
fn register() -> Result<Mastodon, Box<Error>> {
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);

@ -33,7 +33,7 @@ pub fn register() -> Result<Mastodon, Box<Error>> {
.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);

@ -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<App> for App {
type Err = Error;
fn try_into(self) -> Result<App> {
Ok(self)
}
}
impl<'a> TryInto<App> for AppBuilder<'a> {
type Err = Error;
fn try_into(self) -> Result<App> {
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)

@ -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.

@ -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<I: TryInto<App>>(&mut self, app: I) -> Result<Registered<H>>
where Error: From<<I as TryInto<App>>::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<Registered<H>> {
pub fn build(&mut self) -> Result<Registered<H>> {
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<OAuth> {
let url = format!("{}/api/v1/apps", self.base);
Ok(self.send(self.client.post(&url).form(&app))?.json()?)
}
}
impl<H: HttpSend> Registered<H> {

Loading…
Cancel
Save