Skip to main content

grammarbot_io/
request.rs

1//! The grammarly request.
2
3/// The URL to send the requests to.
4pub const GRAMMARLY_CHECK_URL: &str = "https://grammarbot.p.rapidapi.com/check";
5
6/// Grammarly's api key strong type.
7#[derive(Debug, Default, Clone, serde::Serialize)]
8pub struct ApiKey(pub String);
9
10impl<T> From<T> for ApiKey
11where
12    T: AsRef<str>,
13{
14    fn from(s: T) -> ApiKey {
15        ApiKey(s.as_ref().to_owned())
16    }
17}
18
19/// Represents an English language variation.
20#[derive(Debug, Clone, serde::Serialize)]
21pub enum EnglishLanguageVariation {
22    /// GB
23    British,
24    /// US
25    American,
26}
27
28/// Represents the language to be passed to the service.
29#[derive(Debug, Clone, serde::Serialize)]
30pub enum Language {
31    /// The English language with variations.
32    English(EnglishLanguageVariation),
33}
34
35impl Language {
36    /// Serializes the object for passing inside a request.
37    pub fn to_short_string(&self) -> &'static str {
38        match self {
39            Language::English(EnglishLanguageVariation::British) => "en-GB",
40            Language::English(EnglishLanguageVariation::American) => "en-US",
41        }
42    }
43}
44
45impl Default for Language {
46    fn default() -> Language {
47        Language::English(EnglishLanguageVariation::British)
48    }
49}
50
51/// The request data. Used to send data to the service.
52#[derive(Debug, Default, Clone, serde::Serialize)]
53pub struct RequestData {
54    /// The language we need to check for.
55    pub language: String,
56    /// The text we are sending to the service for checking.
57    pub text: String,
58}
59
60/// The request object. Used to send data to the service.
61#[derive(Debug, Default, Clone, serde::Serialize)]
62pub struct Request {
63    api_key: ApiKey,
64    data: RequestData,
65}
66
67impl<T> From<T> for Request
68where
69    T: AsRef<str>,
70{
71    fn from(s: T) -> Request {
72        Request {
73            data: RequestData {
74                text: s.as_ref().to_owned(),
75                ..Default::default()
76            },
77            ..Default::default()
78        }
79    }
80}
81
82impl Request {
83    /// Mutates the object setting the api key.
84    /// Not necessary to use as service allows performing requests without one.
85    pub fn api_key<T: Into<ApiKey>>(&mut self, key: T) -> &mut Request {
86        self.api_key = key.into();
87        self
88    }
89
90    /// Mutates the object setting the language to check grammar for.
91    pub fn language<T: Into<Language>>(&mut self, language: T) -> &mut Request {
92        self.data.language = language.into().to_short_string().to_owned();
93        self
94    }
95}
96
97/// The http request object. Used to pack grammarly request so that
98/// it can be used to send requests from other request crates (`reqwest`/`hyper`/etc).
99/// The main use case is when the library is built without the `client` feature, so
100/// you still can perform requests using your own implementation.
101#[derive(Debug, Clone)]
102pub struct HttpRequest {
103    /// Request URL.
104    pub url: String,
105    /// Key-value pairs of the request headers.
106    pub headers: Vec<(String, String)>,
107    /// Key-value pairs of the request data.
108    pub data: RequestData,
109}
110
111impl From<&Request> for HttpRequest {
112    fn from(r: &Request) -> HttpRequest {
113        HttpRequest {
114            url: GRAMMARLY_CHECK_URL.to_owned(),
115            headers: vec![
116                (
117                    "x-rapidapi-host".to_owned(),
118                    "grammarbot.p.rapidapi.com".to_owned(),
119                ),
120                ("x-rapidapi-key".to_owned(), r.api_key.0.clone()),
121                ("useQueryString".to_owned(), "true".to_owned()),
122            ],
123            data: r.data.clone(),
124        }
125    }
126}