10 个稳定版本 (3 个主要版本)
5.0.5+20240614 | 2024 年 6 月 27 日 |
---|---|
5.0.4+20240222 | 2024 年 3 月 5 日 |
5.0.3+20230117 | 2023 年 8 月 24 日 |
5.0.2+20230117 | 2023 年 3 月 16 日 |
2.0.4+20210319 | 2021 年 4 月 15 日 |
#2300 in 网页编程
在 google-retail2-cli 中使用
1MB
13K SLoC
“google-retail2”库允许访问 Google Cloud Retail 服务(协议 v2)的所有功能。
本文档是从 Cloud Retail crate 版本 5.0.5+20240614 生成的,其中 20240614 是由 mako 代码生成器 v5.0.5 构建的 retail:v2 架构的确切修订版本。
关于 Cloud Retail v2 API 的其他信息可以在 官方文档网站 找到。
功能
轻松从中央 中心 处理以下 资源...
- 项目
- locations catalogs attributes config add catalog attribute, locations catalogs attributes config remove catalog attribute, locations catalogs attributes config replace catalog attribute, locations catalogs branches operations get, locations catalogs branches products add fulfillment places, locations catalogs branches products add local inventories, locations catalogs branches products create, locations catalogs branches products delete, locations catalogs branches products get, locations catalogs branches products import, locations catalogs branches products list, locations catalogs branches products patch, locations catalogs branches products purge, locations catalogs branches products remove fulfillment places, locations catalogs branches products remove local inventories, locations catalogs branches products set inventory, locations catalogs complete query, locations catalogs completion data import, locations catalogs controls create, locations catalogs controls delete, locations catalogs controls get, locations catalogs controls list, locations catalogs controls patch, locations catalogs export analytics metrics, locations catalogs get attributes config, locations catalogs get completion config, locations catalogs get default branch, locations catalogs list, locations catalogs models create, locations catalogs models delete, locations catalogs models get, locations catalogs models list, locations catalogs models patch, locations catalogs models pause, locations catalogs models resume, locations catalogs models tune, locations catalogs operations get, locations catalogs operations list, locations catalogs patch, locations catalogs placements predict, locations catalogs placements search, locations catalogs serving configs add control, locations catalogs serving configs create, locations catalogs serving configs delete, locations catalogs serving configs get, locations catalogs serving configs list, locations catalogs serving configs patch, locations catalogs serving configs predict, locations catalogs serving configs remove control, locations catalogs serving configs search, locations catalogs set default branch, locations catalogs update attributes config, locations catalogs update completion config, locations catalogs user events collect, locations catalogs user events import, locations catalogs user events purge, locations catalogs user events rejoin, locations catalogs user events write, locations operations get, locations operations list, operations get and operations list
本库的结构
API 被结构化为以下主要项目
所有 结构 都用适用的特质标记,以进一步分类它们并简化浏览。
一般来说,您可以通过以下方式调用 活动
let r = hub.resource().activity(...).doit().await
或具体来说...
let r = hub.projects().locations_catalogs_branches_operations_get(...).doit().await
let r = hub.projects().locations_catalogs_branches_products_add_fulfillment_places(...).doit().await
let r = hub.projects().locations_catalogs_branches_products_add_local_inventories(...).doit().await
let r = hub.projects().locations_catalogs_branches_products_import(...).doit().await
let r = hub.projects().locations_catalogs_branches_products_purge(...).doit().await
let r = hub.projects().locations_catalogs_branches_products_remove_fulfillment_places(...).doit().await
let r = hub.projects().locations_catalogs_branches_products_remove_local_inventories(...).doit().await
let r = hub.projects().locations_catalogs_branches_products_set_inventory(...).doit().await
let r = hub.projects().locations_catalogs_completion_data_import(...).doit().await
let r = hub.projects().locations_catalogs_models_create(...).doit().await
let r = hub.projects().locations_catalogs_models_tune(...).doit().await
let r = hub.projects().locations_catalogs_operations_get(...).doit().await
let r = hub.projects().locations_catalogs_user_events_import(...).doit().await
let r = hub.projects().locations_catalogs_user_events_purge(...).doit().await
let r = hub.projects().locations_catalogs_user_events_rejoin(...).doit().await
let r = hub.projects().locations_catalogs_export_analytics_metrics(...).doit().await
let r = hub.projects().locations_operations_get(...).doit().await
let r = hub.projects().operations_get(...).doit().await
resource()
和 activity(...)
调用创建 建造者。后者处理 Activities
并支持各种配置即将进行的操作的方法(此处未显示)。它设计为必须立即指定所有必需参数(即 (...)
),而所有可选参数都可以按需 构建。doit()
方法执行与服务器的实际通信并返回相应的结果。
使用方法
设置项目
要使用这个库,您需要将以下行放入您的 Cargo.toml
文件中
[dependencies]
google-retail2 = "*"
serde = "^1.0"
serde_json = "^1.0"
完整示例
extern crate hyper;
extern crate hyper_rustls;
extern crate google_retail2 as retail2;
use retail2::api::GoogleCloudRetailV2Model;
use retail2::{Result, Error};
use std::default::Default;
use retail2::{CloudRetail, oauth2, hyper, hyper_rustls, chrono, FieldMask};
// Get an ApplicationSecret instance by some means. It contains the `client_id` and
// `client_secret`, among other things.
let secret: oauth2::ApplicationSecret = Default::default();
// Instantiate the authenticator. It will choose a suitable authentication flow for you,
// unless you replace `None` with the desired Flow.
// Provide your own `AuthenticatorDelegate` to adjust the way it operates and get feedback about
// what's going on. You probably want to bring in your own `TokenStorage` to persist tokens and
// retrieve them from storage.
let auth = oauth2::InstalledFlowAuthenticator::builder(
secret,
oauth2::InstalledFlowReturnMethod::HTTPRedirect,
).build().await.unwrap();
let mut hub = CloudRetail::new(hyper::Client::builder().build(hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().unwrap().https_or_http().enable_http1().build()), auth);
// As the method needs a request, you would usually fill it with the desired information
// into the respective structure. Some of the parts shown here might not be applicable !
// Values shown here are possibly random and not representative !
let mut req = GoogleCloudRetailV2Model::default();
// You can configure optional parameters by calling the respective setters at will, and
// execute the final call using `doit()`.
// Values shown here are possibly random and not representative !
let result = hub.projects().locations_catalogs_models_create(req, "parent")
.dry_run(true)
.doit().await;
match result {
Err(e) => match e {
// The Error enum provides details about what exactly happened.
// You can also just use its `Debug`, `Display` or `Error` traits
Error::HttpError(_)
|Error::Io(_)
|Error::MissingAPIKey
|Error::MissingToken(_)
|Error::Cancelled
|Error::UploadSizeLimitExceeded(_, _)
|Error::Failure(_)
|Error::BadRequest(_)
|Error::FieldClash(_)
|Error::JsonDecodeError(_, _) => println!("{}", e),
},
Ok(res) => println!("Success: {:?}", res),
}
处理错误
系统产生的所有错误都作为 Result 枚举提供,作为 doit() 方法的返回值,或者作为中间结果传递给 Hub Delegate 或 Authenticator Delegate。
当委托处理错误或中间值时,它们有机会指导系统重试。这使得系统可能对所有类型的错误都具有弹性。
上传和下载
如果方法支持下载,则应该读取响应体(它是 Result 的一部分)以获取媒体。如果此类方法还支持 Response Result,则默认返回该结果。您可以将其视为实际媒体的元数据。要触发媒体下载,您必须通过此调用设置建造者:.param("alt", "media")
。
支持上传的方法可以使用最多 2 种不同的协议进行:简单 和 可恢复。每种协议的独特性由定制的 doit(...)
方法表示,分别命名为 upload(...)
和 upload_resumable(...)
。
自定义和回调
您可以通过在最终调用 doit() 之前向 Method Builder 提供一个 委托 来改变 doit() 方法调用的方式。相应的方法将被调用以提供进度信息,并确定系统在失败时是否应该重试。
delegate trait 具有默认实现,允许您以最小的努力进行自定义。
服务器请求中的可选部分
本库提供的所有结构都旨在通过json进行编码和解码。可选参数用于表示部分请求和响应是有效的。大多数可选参数被视为部分,这些部分可以通过名称识别,将被发送到服务器以指示请求的集合部分或响应中所需的部分。
构建器参数
使用方法构建器,您可以通过反复调用其方法来准备操作调用。这些方法始终接受单个参数,以下陈述适用于这些参数。
- 简单对象(PODs)通过复制传递
- 字符串作为
&str
传递 - 请求值被移动
参数将始终复制或克隆到构建器中,以使它们与其原始生命周期独立。
Cargo功能
utoipa
- 添加对utoipa的支持,并为所有类型派生utoipa::ToSchema
。您必须在#[openapi(schemas(...))]
中导入和注册所需的类型,否则生成的openapi
规范将是无效的。
许可证
retail2库由Sebastian Thiel生成,并置于MIT许可证下。您可以在存储库的许可证文件中阅读全文。
依赖项
~12-23MB
~343K SLoC