fedora/session.rs
1//! This module contains the definition of the [`Session`] type, and associated methods for building
2//! anonymous or authenticated sessions.
3
4use reqwest::Client;
5use url::Url;
6
7use crate::anonymous::AnonymousSessionBuilder;
8use crate::openid::{OpenIDSessionBuilder, OpenIDSessionKind};
9
10#[derive(Debug)]
11/// This type is a thin newtype wrapper around [`reqwest::Client`] with implementations for
12/// constructing both a generic / unauthenticated session, and a session pre-authenticated via
13/// an OpenID provider.
14pub struct Session {
15 pub(crate) client: Client,
16}
17
18impl Session {
19 /// This method returns a reference to the wrapped [`reqwest::Client`]:
20 ///
21 /// ```
22 /// # use fedora::Session;
23 /// let session = Session::anonymous().build();
24 /// let client: &reqwest::Client = session.session();
25 /// ```
26 pub fn session(&self) -> &Client {
27 &self.client
28 }
29
30 /// This method returns a new builder for an anonymous session.
31 ///
32 /// ```
33 /// # use fedora::Session;
34 /// let anon_session: Session = Session::anonymous().build();
35 /// ```
36 pub fn anonymous<'a>() -> AnonymousSessionBuilder<'a> {
37 AnonymousSessionBuilder::new()
38 }
39
40 /// This method returns a new builder for a session that will need to be authenticated via an
41 /// OpenID provider.
42 ///
43 /// ```
44 /// # use fedora::Session;
45 /// use fedora::{OpenIDSessionKind, OpenIDSessionLogin};
46 /// use url::Url;
47 ///
48 /// let login: OpenIDSessionLogin = Session::openid_auth(
49 /// Url::parse("https://bodhi.fedoraproject.org/login").unwrap(),
50 /// OpenIDSessionKind::Default
51 /// ).build();
52 pub fn openid_auth<'a>(login_url: Url, kind: OpenIDSessionKind) -> OpenIDSessionBuilder<'a> {
53 OpenIDSessionBuilder::new(login_url, kind)
54 }
55}