Skip to main content

cima_rs/endpoints/
documents.rs

1use crate::api_client::CimaClient;
2use crate::models::{DocumentType, Section};
3use anyhow::{Context, Result};
4
5impl CimaClient {
6    /// Get document sections list (without content)
7    pub async fn get_document_sections(
8        &self,
9        doc_type: DocumentType,
10        registration_number: &str,
11    ) -> Result<Vec<Section>> {
12        let endpoint = format!("docSegmentado/secciones/{}", doc_type as u8);
13        let params = vec![("nregistro", registration_number.to_string())];
14
15        self.get_with_params(&endpoint, &params)
16            .await
17            .context("Failed to get document sections")
18    }
19
20    /// Get document section content
21    pub async fn get_document_content(
22        &self,
23        doc_type: DocumentType,
24        registration_number: &str,
25        section: Option<&str>,
26    ) -> Result<Vec<Section>> {
27        let endpoint = format!("docSegmentado/contenido/{}", doc_type as u8);
28        let mut params = vec![("nregistro", registration_number.to_string())];
29
30        if let Some(sec) = section {
31            params.push(("seccion", sec.to_string()));
32        }
33
34        self.get_with_params(&endpoint, &params)
35            .await
36            .context("Failed to get document content")
37    }
38
39    /// Get complete technical data sheet in HTML
40    pub async fn get_technical_sheet_html(&self, registration_number: &str) -> Result<String> {
41        let url = format!(
42            "https://cima.aemps.es/cima/dochtml/ft/{}/FichaTecnica.html",
43            registration_number
44        );
45
46        self.client
47            .get(&url)
48            .send()
49            .await
50            .with_context(|| "Failed to fetch technical sheet HTML".to_string())?
51            .text()
52            .await
53            .context("Failed to read technical sheet HTML")
54    }
55
56    /// Get a specific section of the technical data sheet in HTML
57    pub async fn get_technical_sheet_section_html(
58        &self,
59        registration_number: &str,
60        section: &str,
61    ) -> Result<String> {
62        let url = format!(
63            "https://cima.aemps.es/cima/dochtml/ft/{}/{}/FichaTecnica.html",
64            registration_number, section
65        );
66
67        self.client
68            .get(&url)
69            .send()
70            .await
71            .with_context(|| "Failed to fetch technical sheet section HTML".to_string())?
72            .text()
73            .await
74            .context("Failed to read technical sheet section HTML")
75    }
76
77    /// Get complete package leaflet in HTML
78    pub async fn get_package_leaflet_html(&self, registration_number: &str) -> Result<String> {
79        let url = format!(
80            "https://cima.aemps.es/cima/dochtml/p/{}/Prospecto.html",
81            registration_number
82        );
83
84        self.client
85            .get(&url)
86            .send()
87            .await
88            .with_context(|| "Failed to fetch package leaflet HTML".to_string())?
89            .text()
90            .await
91            .context("Failed to read package leaflet HTML")
92    }
93
94    /// Get a specific section of the package leaflet in HTML
95    pub async fn get_package_leaflet_section_html(
96        &self,
97        registration_number: &str,
98        section: &str,
99    ) -> Result<String> {
100        let url = format!(
101            "https://cima.aemps.es/cima/dochtml/p/{}/{}/Prospecto.html",
102            registration_number, section
103        );
104
105        self.client
106            .get(&url)
107            .send()
108            .await
109            .with_context(|| "Failed to fetch package leaflet section HTML".to_string())?
110            .text()
111            .await
112            .context("Failed to read package leaflet section HTML")
113    }
114}