lib: implemented get_publications_list function

main
Guus van Meerveld 5 months ago
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,4 +1,14 @@
use crate::{error::Result, Token}; use std::collections::HashMap;
use chrono::{Duration, Utc};
use graphql_client::GraphQLQuery;
use crate::{
constants::{AUTH_URL, GRAPHQL_URL, LOCALE},
error::Result,
queries::{get_publications_list, GetPublicationsList, GraphqlResponse},
tokens::{RefreshTokenResponse, Token},
};
pub struct Client { pub struct Client {
base_url: String, base_url: String,
@ -15,7 +25,75 @@ impl Client {
} }
} }
pub async fn get_publications(&self) -> Result<()> { 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(&params)?;
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::new(
tokens.access_token,
Utc::now() + Duration::seconds(tokens.expires_in),
);
let client = Client::new(GRAPHQL_URL, access_token);
let new_refresh_token = Token::new(
tokens.refresh_token,
Utc::now() + Duration::seconds(tokens.refresh_expires_in),
);
Ok((client, new_refresh_token))
}
pub async fn get_publications_list(
&self,
max: i64,
) -> Result<get_publications_list::ResponseData> {
let variables = get_publications_list::Variables {
order_by: Some(get_publications_list::HousingPublicationsOrder::STARTDATE_ASC),
first: Some(max),
locale: Some(String::from(LOCALE)),
after: None,
where_: None,
};
let request_body = GetPublicationsList::build_query(variables);
let response = self
.http_client
.post(&self.base_url)
.json(&request_body)
.send()
.await?;
let body = response
.json::<GraphqlResponse<get_publications_list::ResponseData>>()
.await?;
Ok(body.data)
}
pub async fn reply_to_publication<I: AsRef<str>>(&self, publication_id: I) -> Result<()> {
Ok(()) Ok(())
} }
} }

@ -2,3 +2,5 @@ pub const AUTH_URL: &str =
"https://auth.embracecloud.nl/auth/realms/sshn/protocol/openid-connect/token"; "https://auth.embracecloud.nl/auth/realms/sshn/protocol/openid-connect/token";
pub const GRAPHQL_URL: &str = "https://gateway.embracecloud.nl/graphql"; pub const GRAPHQL_URL: &str = "https://gateway.embracecloud.nl/graphql";
pub const LOCALE: &str = "nl-NL";

@ -1,90 +1,32 @@
use std::{collections::HashMap, fmt::Debug};
mod client; mod client;
mod constants; mod constants;
pub mod error; pub mod error;
mod queries;
mod tokens;
use chrono::{DateTime, Duration, Utc}; pub use crate::client::Client;
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(&params)?;
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))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{constants::GRAPHQL_URL, tokens::Token};
use super::*; use super::*;
#[tokio::test] #[tokio::test]
async fn test_refresh_token_login() { async fn test_refresh_token_login() {
let refresh_token = ""; 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…
Cancel
Save