Skip to content

Commit

Permalink
Add all missing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenwong97 committed Oct 19, 2023
1 parent 6940daf commit 0a4f10e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
#![warn(missing_docs)]

//! API endpoint structures.
//!
//! The types in this module are meant to aid in constructing the appropriate calls using type-safe
//! Rust idioms.
//!
//! All endpoints use the builder pattern and have their members as private so that there are no
//! API implications of adding new members for additional query parameters in future GitLab
//! releases.
//!
//! # Example
//!
//! ```rust,no_run
//! use serde::Deserialize;
//! use marketstack::Marketstack;
//! use marketstack::api::{self, Eod, Query};
//!
//! // The return type of an `EodData`. Note that Marketstack may contain more information, but you can
//! // define your structure to only fetch what is needed.
//! #[derive(Serialize, Deserialize, Debug, Clone)]
//! pub struct EodData {
//! pub pagination: PaginationInfo,
//! pub data: Vec<EodDataItem>,
//! }
//!
//! // Create the client.
//! let client = Marketstack::new("api.marketstack.com", "private-token").unwrap();
//!
//! // OR create an insecure token (if on the Free plan).
//! let client = Marketstack::new_insecure("api.marketstack.com", "private-token").unwrap();
//!
//! // Create a simple endpoint. This one gets the "eod" for the AAPL symbol.
//! let endpoint = eod::Eod::builder().symbol("AAPL").build().unwrap();
//! // Call the endpoint. The return type decides how to represent the value.
//! let eod_date: EodData = endpoint.query(&client).unwrap();
//!
//! // Some endpoints support pagination. Since Marketstack does pagination through query
//! // params, we simply specify them in the endpoint builder.
//! // Note that there are limits defined, and therefore, limit(5) is fallible and returns
//! // a Result.
//! let pageable_endpoint = eod::Eod::builder().symbol("AAPL").limit(5)?.build().unwrap();
//! ```

mod basic;
mod client;
mod endpoint;
Expand Down
1 change: 1 addition & 0 deletions src/api/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Default for SortOrder {
}
}
impl SortOrder {
/// The string representation of the sort order.
pub fn as_str(self) -> &'static str {
match self {
SortOrder::Ascending => "ASC",
Expand Down
1 change: 1 addition & 0 deletions src/api/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::de::DeserializeOwned;

use crate::api::{query, ApiError, AsyncClient, AsyncQuery, BodyError, Client, Query, QueryParams};

/// A trait for providing the necessary information for a single REST API endpoint.
pub trait Endpoint {
/// The HTTP method to use for the endpoint.
fn method(&self) -> Method;
Expand Down
8 changes: 8 additions & 0 deletions src/api/eod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Implemented endpoints for eod, eod/latest and eod/[date].

use std::collections::BTreeSet;

use chrono::NaiveDate;
Expand Down Expand Up @@ -35,6 +37,7 @@ pub struct Eod<'a> {
}

impl<'a> Eod<'a> {
/// Create a builder for the endpoint.
pub fn builder() -> EodBuilder<'a> {
EodBuilder::default()
}
Expand Down Expand Up @@ -65,6 +68,7 @@ impl<'a> EodBuilder<'a> {
self
}

/// Limit the number of results returned.
pub fn limit(&mut self, limit: u16) -> Result<&mut Self, ApiError<PaginationError>> {
let new = self;
new.limit = Some(Some(PageLimit::new(limit)?));
Expand Down Expand Up @@ -119,6 +123,7 @@ pub struct EodLatest<'a> {
}

impl<'a> EodLatest<'a> {
/// Create a builder for the endpoint.
pub fn builder() -> EodLatestBuilder<'a> {
EodLatestBuilder::default()
}
Expand Down Expand Up @@ -149,6 +154,7 @@ impl<'a> EodLatestBuilder<'a> {
self
}

/// Limit the number of results returned.
pub fn limit(&mut self, limit: u16) -> Result<&mut Self, ApiError<PaginationError>> {
let new = self;
new.limit = Some(Some(PageLimit::new(limit)?));
Expand Down Expand Up @@ -203,6 +209,7 @@ pub struct EodDate<'a> {
}

impl<'a> EodDate<'a> {
/// Create a builder for the endpoint.
pub fn builder() -> EodDateBuilder<'a> {
EodDateBuilder::default()
}
Expand Down Expand Up @@ -233,6 +240,7 @@ impl<'a> EodDateBuilder<'a> {
self
}

/// Limit the number of results returned.
pub fn limit(&mut self, limit: u16) -> Result<&mut Self, ApiError<PaginationError>> {
let new = self;
new.limit = Some(Some(PageLimit::new(limit)?));
Expand Down
7 changes: 7 additions & 0 deletions src/api/paged.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//! Pagination related types and functions.
//!
//! Pagination is done simply for Marketstack, but this allows setting
//! page limits to have safety guarantees provided by the new-type pattern.

use thiserror::Error;

use crate::api::ApiError;
Expand All @@ -19,9 +24,11 @@ impl PageLimit {
}
}

/// Errors which may occur with pagination.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum PaginationError {
/// Pagination exceeds the limit allowed by Marketstack.
#[error("pagination exceeds limit error")]
ExceedLimit,
}
Expand Down

0 comments on commit 0a4f10e

Please sign in to comment.