lib: implemented get_publications_list function
parent
04a5caf213
commit
25e4ba16e4
@ -0,0 +1,61 @@
|
||||
query GetPublicationsList(
|
||||
$orderBy: HousingPublicationsOrder
|
||||
$first: Int = 20
|
||||
$after: String
|
||||
$where: HousingWherePublicationsInput
|
||||
$locale: String = "en-US"
|
||||
) {
|
||||
housingPublications(
|
||||
orderBy: $orderBy
|
||||
first: $first
|
||||
after: $after
|
||||
where: $where
|
||||
locale: $locale
|
||||
) {
|
||||
nodes {
|
||||
pageInfo {
|
||||
startCursor
|
||||
endCursor
|
||||
hasNextPage
|
||||
hasPreviousPage
|
||||
}
|
||||
edges {
|
||||
cursor
|
||||
node {
|
||||
...PublicationListItem
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fragment PublicationListItem on HousingPublication {
|
||||
id
|
||||
startTime
|
||||
totalNumberOfApplications
|
||||
|
||||
url {
|
||||
value
|
||||
}
|
||||
|
||||
unit {
|
||||
rentBenefit
|
||||
externalUrl {
|
||||
value
|
||||
}
|
||||
grossRent {
|
||||
exact
|
||||
}
|
||||
location {
|
||||
city {
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
# allocationProcess {
|
||||
# ...PublicationListAllocationProcess
|
||||
# }
|
||||
applicantSpecific {
|
||||
allocationChance
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,90 +1,32 @@
|
||||
use std::{collections::HashMap, fmt::Debug};
|
||||
|
||||
mod client;
|
||||
mod constants;
|
||||
pub mod error;
|
||||
mod queries;
|
||||
mod tokens;
|
||||
|
||||
use chrono::{DateTime, Duration, Utc};
|
||||
use client::Client;
|
||||
use constants::{AUTH_URL, GRAPHQL_URL};
|
||||
use error::Result;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct RefreshTokenResponse {
|
||||
access_token: String,
|
||||
expires_in: i64,
|
||||
refresh_expires_in: i64,
|
||||
refresh_token: String,
|
||||
// token_type: String,
|
||||
// id_token: String,
|
||||
// session_state: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Token {
|
||||
token: String,
|
||||
expires: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Token {
|
||||
pub fn token(&self) -> &str {
|
||||
&self.token
|
||||
}
|
||||
|
||||
pub fn expires(&self) -> DateTime<Utc> {
|
||||
self.expires
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn login_with_refresh_token<R: AsRef<str>>(refresh_token: R) -> Result<(Client, Token)> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let mut params = HashMap::new();
|
||||
|
||||
params.insert("grant_type", "refresh_token");
|
||||
params.insert("refresh_token", refresh_token.as_ref());
|
||||
params.insert("client_id", "portal-legacy");
|
||||
|
||||
let body = serde_urlencoded::to_string(¶ms)?;
|
||||
|
||||
let response = client
|
||||
.post(AUTH_URL)
|
||||
.body(body)
|
||||
.header(
|
||||
reqwest::header::CONTENT_TYPE,
|
||||
"application/x-www-form-urlencoded",
|
||||
)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let tokens = response.json::<RefreshTokenResponse>().await?;
|
||||
|
||||
let access_token = Token {
|
||||
token: tokens.access_token,
|
||||
expires: Utc::now() + Duration::seconds(tokens.expires_in),
|
||||
};
|
||||
|
||||
let client = Client::new(GRAPHQL_URL, access_token);
|
||||
|
||||
let new_refresh_token = Token {
|
||||
token: tokens.refresh_token,
|
||||
expires: Utc::now() + Duration::seconds(tokens.refresh_expires_in),
|
||||
};
|
||||
|
||||
Ok((client, new_refresh_token))
|
||||
}
|
||||
pub use crate::client::Client;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{constants::GRAPHQL_URL, tokens::Token};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_refresh_token_login() {
|
||||
let refresh_token = "";
|
||||
|
||||
let (client, new_refresh_token) = login_with_refresh_token(refresh_token).await.unwrap();
|
||||
let (client, _new_refresh_token) = Client::login_with_refresh_token(refresh_token)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_publications() {
|
||||
let client = Client::new(GRAPHQL_URL, Token::default());
|
||||
|
||||
let data = client.get_publications_list(30).await.unwrap();
|
||||
|
||||
println!("{:?}", new_refresh_token);
|
||||
println!("{:?}", data);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,19 @@
|
||||
use graphql_client::GraphQLQuery;
|
||||
use serde::Deserialize;
|
||||
|
||||
type Cursor = String;
|
||||
type Decimal = f64;
|
||||
type DateTimeOffset = String;
|
||||
|
||||
#[derive(GraphQLQuery)]
|
||||
#[graphql(
|
||||
schema_path = "schema.graphql",
|
||||
query_path = "queries.graphql",
|
||||
response_derives = "Debug"
|
||||
)]
|
||||
pub struct GetPublicationsList;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct GraphqlResponse<T> {
|
||||
pub data: T,
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct RefreshTokenResponse {
|
||||
pub access_token: String,
|
||||
pub expires_in: i64,
|
||||
pub refresh_expires_in: i64,
|
||||
pub refresh_token: String,
|
||||
// token_type: String,
|
||||
// id_token: String,
|
||||
// session_state: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Token {
|
||||
content: String,
|
||||
expires: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Default for Token {
|
||||
fn default() -> Self {
|
||||
Token {
|
||||
content: String::new(),
|
||||
expires: Utc::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for Token {
|
||||
fn as_ref(&self) -> &str {
|
||||
&self.content
|
||||
}
|
||||
}
|
||||
|
||||
impl Token {
|
||||
pub fn new(content: String, expires: DateTime<Utc>) -> Self {
|
||||
Self { content, expires }
|
||||
}
|
||||
|
||||
pub fn content(&self) -> &str {
|
||||
&self.content
|
||||
}
|
||||
|
||||
pub fn expires(&self) -> DateTime<Utc> {
|
||||
self.expires
|
||||
}
|
||||
}
|
Loading…
Reference in new issue