uspto_xml 0.1.0

A library for downloading patent and application XML from the USPTO.
Documentation
//! An un-exported module containing various types for interacting with the USPTO's API.

use reqwest::header::{HeaderMap, HeaderValue};
use serde::Deserialize;

use crate::UsptoApiError;

/// Response returned by the USPTO Patent File Wrapper search API.
#[derive(Debug, Deserialize)]
pub struct FileWrapperResponse {
  pub count: u32,
  #[serde(rename = "patentFileWrapperDataBag")]
  pub patent_file_wrapper_data_bag: Vec<FileWrapperBag>,
}

impl FileWrapperResponse {
  pub const fn is_empty(&self) -> bool {
    self.count == 0
  }
}

/// Represents a file wrapper entry containing metadata.
#[derive(Debug, Deserialize)]
pub struct FileWrapperBag {
  #[serde(rename = "grantDocumentMetaData")]
  pub grant_document_meta_data: Option<GrantDocMeta>,
}

/// Metadata describing a grant document, includes the URI for its XML.
#[derive(Debug, Deserialize)]
pub struct GrantDocMeta {
  #[serde(rename = "fileLocationURI")]
  pub file_location_uri: String,
}

/// Prepares HTTP headers that include the provided `api_key` as `X-API-KEY` to authenticate
/// with the USPTO.
///
/// Returns a [`HeaderMap`] of headers or [`UsptoApiError::MissingApiKey`] if `api_key` is
/// empty.
pub fn construct_headers(api_key: &str) -> Result<HeaderMap, UsptoApiError> {
  let mut headers = HeaderMap::new();
  if api_key.is_empty() {
    return Err(UsptoApiError::MissingApiKey);
  }
  headers.insert("X-API-KEY", HeaderValue::from_str(api_key)?);

  Ok(headers)
}

/// Prepares query parameters containing the provided `patent_number` as a query string and
/// indicating that only one page containing one result should be returned.
///
/// Returns an array of query keys and values.
pub fn construct_query_params(patent_number: &str) -> [(&str, String); 3] {
  let q = format!("applicationMetaData.patentNumber:{patent_number}");
  [
    ("q", q),
    ("pagination[offset]", "0".to_string()),
    ("pagination[limit]", "1".to_string()),
  ]
}