forked from MightyPork/elefren-fork
Rework the Registration and App APIs
This puts `register` back to the way it was, and changes the "new" `register` to `build`.
This commit is contained in:
@@ -58,7 +58,7 @@ fn main() -> Result<(), Box<Error>> {
|
|||||||
fn register() -> Result<Mastodon, Box<Error>> {
|
fn register() -> Result<Mastodon, Box<Error>> {
|
||||||
let registration = Registration::new("https://mastodon.social")
|
let registration = Registration::new("https://mastodon.social")
|
||||||
.client_name("elefren-examples")
|
.client_name("elefren-examples")
|
||||||
.register()?;
|
.build()?;
|
||||||
let url = registration.authorize_url()?;
|
let url = registration.authorize_url()?;
|
||||||
|
|
||||||
println!("Click this link to authorize on Mastodon: {}", 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")
|
.client_name("elefren-examples")
|
||||||
.scopes(Scopes::All)
|
.scopes(Scopes::All)
|
||||||
.website("https://github.com/pwoolcoc/elefren")
|
.website("https://github.com/pwoolcoc/elefren")
|
||||||
.register()?;
|
.build()?;
|
||||||
let url = registration.authorize_url()?;
|
let url = registration.authorize_url()?;
|
||||||
|
|
||||||
println!("Click this link to authorize on Mastodon: {}", url);
|
println!("Click this link to authorize on Mastodon: {}", url);
|
||||||
|
|||||||
+18
@@ -1,5 +1,7 @@
|
|||||||
use std::{borrow::Cow, fmt};
|
use std::{borrow::Cow, fmt};
|
||||||
|
|
||||||
|
use try_from::TryInto;
|
||||||
|
|
||||||
use errors::{Error, Result};
|
use errors::{Error, Result};
|
||||||
|
|
||||||
/// Represents an application that can be registered with a mastodon instance
|
/// 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.
|
/// Permission scope of the application.
|
||||||
/// [Details on what each permission provides][1]
|
/// [Details on what each permission provides][1]
|
||||||
/// [1]: https://github.com/tootsuite/documentation/blob/master/Using-the-API/OAuth-details.md)
|
/// [1]: https://github.com/tootsuite/documentation/blob/master/Using-the-API/OAuth-details.md)
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@
|
|||||||
//!
|
//!
|
||||||
//! let registration = Registration::new("https://mastodon.social")
|
//! let registration = Registration::new("https://mastodon.social")
|
||||||
//! .client_name("elefren_test")
|
//! .client_name("elefren_test")
|
||||||
//! .register()?;
|
//! .build()?;
|
||||||
//! let url = registration.authorize_url()?;
|
//! let url = registration.authorize_url()?;
|
||||||
//! // Here you now need to open the url in the browser
|
//! // Here you now need to open the url in the browser
|
||||||
//! // And handle a the redirect url coming back with the code.
|
//! // And handle a the redirect url coming back with the code.
|
||||||
|
|||||||
+53
-7
@@ -1,5 +1,6 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
use try_from::TryInto;
|
||||||
use reqwest::{Client, RequestBuilder, Response};
|
use reqwest::{Client, RequestBuilder, Response};
|
||||||
|
|
||||||
use apps::{App, AppBuilder, Scopes};
|
use apps::{App, AppBuilder, Scopes};
|
||||||
@@ -8,6 +9,7 @@ use Data;
|
|||||||
use Mastodon;
|
use Mastodon;
|
||||||
use MastodonBuilder;
|
use MastodonBuilder;
|
||||||
use Result;
|
use Result;
|
||||||
|
use Error;
|
||||||
|
|
||||||
const DEFAULT_REDIRECT_URI: &'static str = "urn:ietf:wg:oauth:2.0:oob";
|
const DEFAULT_REDIRECT_URI: &'static str = "urn:ietf:wg:oauth:2.0:oob";
|
||||||
|
|
||||||
@@ -89,16 +91,19 @@ impl<'a, H: HttpSend> Registration<'a, H> {
|
|||||||
Ok(self.http_sender.send(&self.client, req)?)
|
Ok(self.http_sender.send(&self.client, req)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register the application with the server from the `base` url.
|
/// Register the given application
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// # extern crate elefren;
|
/// # extern crate elefren;
|
||||||
/// # fn main () -> elefren::Result<()> {
|
/// # fn main () -> elefren::Result<()> {
|
||||||
/// use elefren::prelude::*;
|
/// use elefren::prelude::*;
|
||||||
|
/// use elefren::apps::App;
|
||||||
|
///
|
||||||
|
/// let mut app = App::builder();
|
||||||
|
/// app.client_name("elefren_test");
|
||||||
///
|
///
|
||||||
/// let registration = Registration::new("https://mastodon.social")
|
/// let registration = Registration::new("https://mastodon.social")
|
||||||
/// .client_name("elefren_test")
|
/// .register(app)?;
|
||||||
/// .register()?;
|
|
||||||
/// let url = registration.authorize_url()?;
|
/// let url = registration.authorize_url()?;
|
||||||
/// // Here you now need to open the url in the browser
|
/// // Here you now need to open the url in the browser
|
||||||
/// // And handle a the redirect url coming back with the code.
|
/// // And handle a the redirect url coming back with the code.
|
||||||
@@ -109,10 +114,11 @@ impl<'a, H: HttpSend> Registration<'a, H> {
|
|||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn register(&mut self) -> Result<Registered<H>> {
|
pub fn register<I: TryInto<App>>(&mut self, app: I) -> Result<Registered<H>>
|
||||||
let app: App = self.app_builder.clone().build()?;
|
where Error: From<<I as TryInto<App>>::Err>
|
||||||
let url = format!("{}/api/v1/apps", self.base);
|
{
|
||||||
let oauth: OAuth = self.send(self.client.post(&url).form(&app))?.json()?;
|
let app = app.try_into()?;
|
||||||
|
let oauth = self.send_app(&app)?;
|
||||||
|
|
||||||
Ok(Registered {
|
Ok(Registered {
|
||||||
base: self.base.clone(),
|
base: self.base.clone(),
|
||||||
@@ -124,6 +130,46 @@ impl<'a, H: HttpSend> Registration<'a, H> {
|
|||||||
http_sender: self.http_sender.clone(),
|
http_sender: self.http_sender.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Register the application with the server from the `base` url.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// # extern crate elefren;
|
||||||
|
/// # fn main () -> elefren::Result<()> {
|
||||||
|
/// use elefren::prelude::*;
|
||||||
|
///
|
||||||
|
/// let registration = Registration::new("https://mastodon.social")
|
||||||
|
/// .client_name("elefren_test")
|
||||||
|
/// .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.
|
||||||
|
/// let code = String::from("RETURNED_FROM_BROWSER");
|
||||||
|
/// let mastodon = registration.complete(&code)?;
|
||||||
|
///
|
||||||
|
/// println!("{:?}", mastodon.get_home_timeline()?.initial_items);
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
pub fn build(&mut self) -> Result<Registered<H>> {
|
||||||
|
let app: App = self.app_builder.clone().build()?;
|
||||||
|
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(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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> {
|
impl<H: HttpSend> Registered<H> {
|
||||||
|
|||||||
Reference in New Issue
Block a user