Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Client Constructors #43

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,50 @@ impl YahooConnector {
search_url: YSEARCH_URL,
}
}

/// Constructs a new instance of the object.
///
/// # Parameters
/// - `url`: Proxy url
/// - `auth`: Opt(username, password)
/// # Example
/// ```
/// use yahoo_finance_api as yf;
/// let instance = yf::YahooConnector::new_w_proxy(
/// "https://secure.example",
/// Some(("user123", "password123"))
/// );
/// ```
pub fn new_w_proxy(url: &str, auth: Option<(&str, &str)>) -> Result<Self, YahooError> {
let client = if auth.is_some() {
let auth = auth.expect("Conditioned");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, use something like .map_err(...)? instead. It is no good practice to let a library panic.

let proxy = reqwest::Proxy::all(url)?
.basic_auth(auth.0, auth.1);

reqwest::Client::builder()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Client::builder() instead of reqwest::Client::builder(). Client is already imported and direct reference here uses only the non-blocking Client implementation.

.proxy(proxy)
.build()?
} else {
reqwest::Client::builder()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

.proxy(reqwest::Proxy::all(url)?)
.build()?
};

Ok(YahooConnector {
client,
url: YCHART_URL,
search_url: YSEARCH_URL,
})

}

pub fn new_w_client(client: Client) -> Self {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment here.

YahooConnector {
client,
url: YCHART_URL,
search_url: YSEARCH_URL,
}
}

pub fn builder() -> YahooConnectorBuilder {
YahooConnectorBuilder {
Expand Down
Loading