use std::borrow::Cow;
use crate::Endpoint;
use crate::api::domains::{DomainDetails, DomainId};
use crate::api::endpoint_with_path_segment;
use serde::Serialize;
use typed_builder::TypedBuilder;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
#[derive(TypedBuilder)]
pub struct GetDomainRequest {
#[builder(setter(into))]
#[serde(skip)]
pub domain_id: DomainId,
}
impl Endpoint for GetDomainRequest {
type Request = GetDomainRequest;
type Response = DomainDetails;
fn endpoint(&self) -> Cow<'static, str> {
endpoint_with_path_segment("/domains", &self.domain_id.to_string())
}
fn body(&self) -> &Self::Request {
self
}
fn method(&self) -> http::Method {
http::Method::GET
}
}
#[cfg(test)]
mod tests {
use httptest::matchers::request;
use httptest::{Expectation, Server, responders::*};
use serde_json::json;
use crate::Query;
use crate::reqwest::PostmarkClient;
use super::*;
const DOMAIN_ID: i64 = 36735;
#[tokio::test]
pub async fn get_domain() {
let server = Server::run();
server.expect(
Expectation::matching(request::method_path("GET", format!("/domains/{DOMAIN_ID}")))
.respond_with(json_encoded(json!({
"Name": "postmarkapp.com",
"SPFVerified": true,
"SPFHost": "postmarkapp.com",
"SPFTextValue": "v=spf1 a mx include:spf.mtasv.net ~all",
"DKIMVerified": false,
"WeakDKIM": false,
"DKIMHost": "jan2013pm._domainkey.postmarkapp.com",
"DKIMTextValue": "k=rsa;p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDJ...",
"DKIMPendingHost": "20131031155228pm._domainkey.postmarkapp.com",
"DKIMPendingTextValue": "k=rsa;p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCFn...",
"DKIMRevokedHost": "",
"DKIMRevokedTextValue": "",
"SafeToRemoveRevokedKeyFromDNS": false,
"DKIMUpdateStatus": "Pending",
"ReturnPathDomain": "pm-bounces.postmarkapp.com",
"ReturnPathDomainVerified": false,
"ReturnPathDomainCNAMEValue": "pm.mtasv.net",
"ID": 36735
}))),
);
let client = PostmarkClient::builder()
.base_url(server.url("/").to_string())
.build();
let req = GetDomainRequest::builder().domain_id(DOMAIN_ID).build();
let resp = req
.execute(&client)
.await
.expect("Should get a response and be able to json decode it");
assert_eq!(resp.name, "postmarkapp.com");
assert_eq!(resp.id, 36735);
}
}