use crate::client::Client;
use crate::error::Result;
use crate::types::{PhoneNumber, PhoneNumbersResponse, SuccessResponse};
use serde::{Deserialize, Serialize};
pub struct PhoneNumbersApi {
client: Client,
}
impl PhoneNumbersApi {
pub(crate) fn new(client: Client) -> Self {
Self { client }
}
pub async fn list(&self, waba_id: &str) -> Result<PhoneNumbersResponse> {
let url = self.client.endpoint_url(&format!("{}/phone_numbers", waba_id));
self.client.get(&url).await
}
pub async fn get(&self, phone_number_id: &str) -> Result<PhoneNumber> {
let url = self.client.endpoint_url(phone_number_id);
self.client.get(&url).await
}
pub async fn register(&self, pin: &str) -> Result<SuccessResponse> {
let body = RegisterRequest {
messaging_product: "whatsapp".to_string(),
pin: pin.to_string(),
};
let url = format!("{}/register", self.client.base_url());
self.client.post(&url, &body).await
}
pub async fn deregister(&self) -> Result<SuccessResponse> {
let url = format!("{}/deregister", self.client.base_url());
self.client.post(&url, &serde_json::json!({})).await
}
pub async fn request_verification_code(
&self,
code_method: &str,
language: &str,
) -> Result<SuccessResponse> {
let body = RequestCodeRequest {
code_method: code_method.to_string(),
language: language.to_string(),
};
let url = format!("{}/request_code", self.client.base_url());
self.client.post(&url, &body).await
}
pub async fn verify_code(&self, code: &str) -> Result<SuccessResponse> {
let body = VerifyCodeRequest {
code: code.to_string(),
};
let url = format!("{}/verify_code", self.client.base_url());
self.client.post(&url, &body).await
}
pub async fn set_two_step_verification(&self, pin: &str) -> Result<SuccessResponse> {
let body = TwoStepRequest {
pin: pin.to_string(),
};
let url = self.client.base_url();
self.client.post(&url, &body).await
}
pub async fn get_business_profile(&self) -> Result<BusinessProfileResponse> {
let url = format!(
"{}/whatsapp_business_profile?fields=about,address,description,email,profile_picture_url,websites,vertical",
self.client.base_url()
);
self.client.get(&url).await
}
pub async fn update_business_profile(
&self,
profile: &BusinessProfileUpdate,
) -> Result<SuccessResponse> {
let url = format!("{}/whatsapp_business_profile", self.client.base_url());
self.client.post(&url, profile).await
}
}
#[derive(Debug, Serialize)]
struct RegisterRequest {
messaging_product: String,
pin: String,
}
#[derive(Debug, Serialize)]
struct RequestCodeRequest {
code_method: String,
language: String,
}
#[derive(Debug, Serialize)]
struct VerifyCodeRequest {
code: String,
}
#[derive(Debug, Serialize)]
struct TwoStepRequest {
pin: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BusinessProfileResponse {
pub data: Vec<BusinessProfile>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BusinessProfile {
#[serde(default)]
pub messaging_product: Option<String>,
#[serde(default)]
pub about: Option<String>,
#[serde(default)]
pub address: Option<String>,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub email: Option<String>,
#[serde(default)]
pub profile_picture_url: Option<String>,
#[serde(default)]
pub websites: Option<Vec<String>>,
#[serde(default)]
pub vertical: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BusinessProfileUpdate {
pub messaging_product: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub about: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub address: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub profile_picture_handle: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub websites: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub vertical: Option<String>,
}
impl Default for BusinessProfileUpdate {
fn default() -> Self {
Self {
messaging_product: "whatsapp".to_string(),
about: None,
address: None,
description: None,
email: None,
profile_picture_handle: None,
websites: None,
vertical: None,
}
}
}
impl BusinessProfileUpdate {
pub fn new() -> Self {
Self::default()
}
pub fn about(mut self, about: impl Into<String>) -> Self {
self.about = Some(about.into());
self
}
pub fn address(mut self, address: impl Into<String>) -> Self {
self.address = Some(address.into());
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn email(mut self, email: impl Into<String>) -> Self {
self.email = Some(email.into());
self
}
pub fn websites(mut self, websites: Vec<String>) -> Self {
self.websites = Some(websites);
self
}
pub fn vertical(mut self, vertical: impl Into<String>) -> Self {
self.vertical = Some(vertical.into());
self
}
}