pub struct EstatApi { /* private fields */ }Expand description
Client for the e-Stat API v3.0 (JSON).
The application ID is issued after registering on e-Stat and is sent as
the appId query parameter.
§Example
use estat_api::{Dimension, EstatApi, StatsDataParams, StatsListParams};
let api = EstatApi::from_env()?;
let list = api.stats_list(&StatsListParams::new().search_word("人口推計").limit(5)).await?;
for table in &list.datalist_inf.table_inf {
println!("{} {:?}", table.id, table.title.as_ref().map(|t| &t.name));
}
let data = api
.stats_data_all(&StatsDataParams::table("0003448237").code(Dimension::Area, "13000"))
.await?;
for value in data.values() {
println!("{:?} {}", data.label(value, Dimension::Time), value.value);
}Implementations§
Source§impl EstatApi
impl EstatApi
Sourcepub fn with_app_id<T: Into<String>>(app_id: T) -> Self
pub fn with_app_id<T: Into<String>>(app_id: T) -> Self
Client with the given application ID. Requests time out after 5 minutes.
Sourcepub fn from_env() -> Result<Self, EstatError>
pub fn from_env() -> Result<Self, EstatError>
Client with the application ID in the ESTAT_APP_ID environment variable.
§Errors
EstatError::InvalidArgument when the variable is not set or empty.
Sourcepub fn with_client(self, client: Client) -> Self
pub fn with_client(self, client: Client) -> Self
Uses the given HTTP client, for example one with a proxy. The 5 minute default no longer applies, so set a timeout on the client if you want one.
Sourcepub fn with_timeout(self, timeout: Duration) -> Self
pub fn with_timeout(self, timeout: Duration) -> Self
Timeout for each request, including reading the body.
Sourcepub fn with_lang(self, lang: Lang) -> Self
pub fn with_lang(self, lang: Lang) -> Self
Language of the responses. Without it e-Stat answers in Japanese.
Sourcepub fn with_base_url<T: Into<String>>(self, base_url: T) -> Self
pub fn with_base_url<T: Into<String>>(self, base_url: T) -> Self
Changes the API base URL (default https://api.e-stat.go.jp/rest/3.0/app/json).
Mainly for tests against a mock server.
Sourcepub async fn stats_list(
&self,
params: &StatsListParams,
) -> Result<StatsList, EstatError>
pub async fn stats_list( &self, params: &StatsListParams, ) -> Result<StatsList, EstatError>
Searches statistical tables (getStatsList, 統計表情報取得).
§Errors
EstatError::Apiwhen e-Stat reports an error, such as100for a wrong application ID or102for an invalid parameter value.EstatError::Connectionon a network failure or timeout,EstatError::HttpStatuswhen the server answers with an error page,EstatError::JsonParseorEstatError::UnexpectedResponsewhen the body is not the expected JSON.
Sourcepub async fn meta_info(
&self,
params: impl Into<MetaInfoParams>,
) -> Result<MetaInfo, EstatError>
pub async fn meta_info( &self, params: impl Into<MetaInfoParams>, ) -> Result<MetaInfo, EstatError>
Dimensions and items of a table (getMetaInfo, メタ情報取得).
Takes a table ID or MetaInfoParams.
§Errors
EstatError::Api with 300 (see EstatError::is_not_found) when
no table has the ID. Otherwise the same as stats_list.
Sourcepub async fn stats_data(
&self,
params: &StatsDataParams,
) -> Result<StatsData, EstatError>
pub async fn stats_data( &self, params: &StatsDataParams, ) -> Result<StatsData, EstatError>
One page of values of a table or dataset (getStatsData, 統計データ取得).
A page holds up to limit values (100,000 by default). When there are
more, StatsData::next_key is the start of the next page; pass it to
StatsDataParams::start_position, or use
stats_data_all.
§Errors
Same as meta_info.
Sourcepub async fn stats_data_all(
&self,
params: &StatsDataParams,
) -> Result<StatsData, EstatError>
pub async fn stats_data_all( &self, params: &StatsDataParams, ) -> Result<StatsData, EstatError>
All values, following NEXT_KEY until the last page.
The first page is requested with params as given; later pages are
requested without metadata. The result has the table and metadata of
the first page and the values of all pages, with NEXT_KEY cleared.
Large tables take many requests, so narrow them down with filters.
§Errors
Same as stats_data, and
EstatError::UnexpectedResponse if NEXT_KEY does not move forward.
Sourcepub async fn data_catalog(
&self,
params: &DataCatalogParams,
) -> Result<DataCatalog, EstatError>
pub async fn data_catalog( &self, params: &DataCatalogParams, ) -> Result<DataCatalog, EstatError>
Searches the data catalog: Excel, CSV and PDF files and database
tables on e-Stat (getDataCatalog, データカタログ情報取得).
§Errors
Same as stats_list.
Sourcepub async fn stats_datas(
&self,
params: &StatsDatasParams,
) -> Result<StatsDatas, EstatError>
pub async fn stats_datas( &self, params: &StatsDatasParams, ) -> Result<StatsDatas, EstatError>
Values of several tables or datasets in one request (getStatsDatas,
統計データ一括取得). The requests together may return at most 100,000
values, and there is no paging; use stats_data
for more.
§Errors
EstatError::Api with 400 when the requests select too many
values, or 401 when one of them fails (the message names the
request and its error). Otherwise the same as stats_list.
Sourcepub async fn datasets(
&self,
params: &DatasetsParams,
) -> Result<DatasetList, EstatError>
pub async fn datasets( &self, params: &DatasetsParams, ) -> Result<DatasetList, EstatError>
Datasets you can use: your own and those others made public
(refDataset without an ID, データセット参照).
§Errors
Same as stats_list.
Sourcepub async fn dataset(
&self,
params: impl Into<DatasetParams>,
) -> Result<RefDataset, EstatError>
pub async fn dataset( &self, params: impl Into<DatasetParams>, ) -> Result<RefDataset, EstatError>
One dataset with its filters (refDataset with an ID, データセット参照).
Takes a dataset ID or DatasetParams.
§Errors
EstatError::Api with 300 when there is no dataset with that ID.
Otherwise the same as stats_list.
Sourcepub async fn post_dataset(
&self,
params: &PostDatasetParams,
) -> Result<PostDataset, EstatError>
pub async fn post_dataset( &self, params: &PostDatasetParams, ) -> Result<PostDataset, EstatError>
Registers, updates or deletes a dataset (postDataset, データセット登録).
This changes data on e-Stat under your application ID. e-Stat answers
this call in XML only, and it is sent to postDataset next to the
json directory of the base URL.
§Errors
EstatError::Api with 101 to 104 for missing or invalid
parameters, 301 when the dataset belongs to another application ID,
302 when the filters select nothing. Otherwise the same as
stats_list.