markdown 使用Reqwest和dotenv #rust的Rust客户端
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 使用Reqwest和dotenv #rust的Rust客户端相关的知识,希望对你有一定的参考价值。
# Rust Client using Reqwest
```rust
extern crate dotenv;
extern crate hyper;
use http::header::{HeaderName, HeaderValue};
use reqwest::{Client, Method, Url};
fn get_env_vars() -> (HeaderName, HeaderValue, Url) {
// use dotenv to pull the values from .env
let cmc_api_key = dotenv::var("CMC_API_KEY").expect("CMC_API_KEY not found");
let cmc_api_endpoint = dotenv::var("CMC_API_ENDPOINT").expect("CMC_API_ENDPOINT not found");
let cmc_header_name = dotenv::var("CMC_HEADER_NAME").expect("CMC_HEADER_NAME not found");
// parse and unwrap the values
let header_name = cmc_header_name.parse::<HeaderName>().unwrap();
let header_value = cmc_api_key.parse::<HeaderValue>().unwrap();
let url: Url = cmc_api_endpoint.parse().unwrap();
// return a tuple of the values
(header_name, header_value, url)
}
fn fetch_cmc_data(name: HeaderName, value: HeaderValue, url: Url) -> reqwest::Response {
let client = Client::new();
let request = client
.request(Method::GET, url)
.header(name, value)
.build()
.unwrap();
client.execute(request).unwrap()
}
fn main() {
let (header_name, header_value, url) = get_env_vars();
let mut response = fetch_cmc_data(header_name, header_value, url);
println!("{:?}", response.text());
}
```
This is a Rust client using the libraries `reqwest` and `dotenv` . It calls an API endpoint at coinmarketcap.com and returns a list of cryptocurrencies, the first 60+ lines are found at the bottom of this post.
1. starting from `fn main()`, a tuple of the `header_name`, `header_value`, and `url` are returned from `get_env_vars()`
2. `get_env_vars()` pulls the values from a `.env` file in the same directory
- `CMC_API_KEY` - a [coinmarketcap.com](http://coinmarketcap.com) API key. Get yours at: {link here}
- `CMC_API_ENDPOINT` - [https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest](https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest)
- `CMC_HEADER_NAME` - which is `"X-CMC_PRO_API_KEY"`
3. the contents of the tuple are passed into `fn fetch_cmc_data()` , which then returns `reqwest::Response` into `mut response` .
- a `reqwest::Client` is instantiated first
- a `reqwest::RequestBuilder` receives the `url` in `request()` , and the header name and value are passed into `header()`
- `.build()` and `.unwrap()` are called immediately to create a `reqwest::Request`
- `client` now executes the `request`, and calls `unwrap()` , but you should handle a client error here
4. `text()` output from `mut response` is fed into a printing function. Boom!
### Response
This is the first part of the response from the above client request:
```
{
"status": {
"timestamp": "2019-02-16T08: 28: 15.822Z",
"error_code": 0,
"error_message": null,
"elapsed": 14,
"credit_count": 1
},
"data": [
{
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"circulating_supply": 17540550,
"total_supply": 17540550,
"max_supply": 21000000,
"date_added": "2013-04-28T00: 00: 00.000Z",
"num_market_pairs": 6653,
"tags": [
"mineable"
],
"platform": null,
"cmc_rank": 1,
"last_updated": "2019-02-16T08: 27: 28.000Z",
"quote": {
"USD": {
"price": 3635.49176252,
"volume_24h": 6060386037.82371,
"percent_change_1h": 0.0488409,
"percent_change_24h": 0.14031,
"percent_change_7d": -0.810947,
"market_cap": 63768525035.07019,
"last_updated": "2019-02-16T08: 27: 28.000Z"
}
}
},
{
"id": 1027,
"name": "Ethereum",
"symbol": "ETH",
"slug": "ethereum",
"circulating_supply": 104879559.8741,
"total_supply": 104879559.8741,
"max_supply": null,
"date_added": "2015-08-07T00: 00: 00.000Z",
"num_market_pairs": 4759,
"tags": [
"mineable"
],
"platform": null,
"cmc_rank": 2,
"last_updated": "2019-02-16T08: 27: 19.000Z",
"quote": {
"USD": {
"price": 123.164946188,
"volume_24h": 2964473039.45236,
"percent_change_1h": 0.0820388,
"percent_change_24h": 0.194405,
"percent_change_7d": 3.483,
"market_cap": 12917485348.11465,
"last_updated": "2019-02-16T08: 27: 19.000Z"
}
}
},
...
```
以上是关于markdown 使用Reqwest和dotenv #rust的Rust客户端的主要内容,如果未能解决你的问题,请参考以下文章