19个稳定版本 (4个主要版本)
5.0.5+20240623 | 2024年6月27日 |
---|---|
5.0.4+20240303 | 2024年3月5日 |
5.0.3+20230121 | 2023年8月23日 |
5.0.2+20230121 | 2023年3月16日 |
1.0.7+20171204 | 2018年2月1日 |
#2628 in 网页编程
210KB
2.5K SLoC
“google-language1”库允许访问 Google Cloud Natural Language 服务(协议 v1)的所有功能。
本文档是从 Cloud Natural Language crate 版本 5.0.5+20240623 生成的,其中 20240623 是由 mako 代码生成器 v5.0.5 构建的 language:v1 模式的确切修订版。
关于 Cloud Natural Language v1 API 的其他所有内容,都可以在 官方文档网站 上找到。
特性
轻松从中央 枢纽 处理以下 资源...
本库的结构
API 结构如下几个主要部分
所有 结构 都带有适用特性和进一步分类以及便于浏览。
一般来说,您可以像这样调用 活动
let r = hub.resource().activity(...).doit().await
或具体...
let r = hub.documents().analyze_entities(...).doit().await
let r = hub.documents().analyze_entity_sentiment(...).doit().await
let r = hub.documents().analyze_sentiment(...).doit().await
let r = hub.documents().analyze_syntax(...).doit().await
let r = hub.documents().annotate_text(...).doit().await
let r = hub.documents().classify_text(...).doit().await
let r = hub.documents().moderate_text(...).doit().await
资源(resource)和活动(activity)调用创建构建器。第二个处理Activities
的调用支持配置即将进行的操作的各种方法(此处未展示)。它设计成必须立即指定所有必需的参数(即(...)
),而所有可选参数可以按需构建。doit()
方法执行与服务器实际通信,并返回相应的结果。
用法
设置项目
要使用此库,您需要将以下行放入您的Cargo.toml
文件中
[dependencies]
google-language1 = "*"
serde = "^1.0"
serde_json = "^1.0"
完整示例
extern crate hyper;
extern crate hyper_rustls;
extern crate google_language1 as language1;
use language1::api::AnalyzeEntitiesRequest;
use language1::{Result, Error};
use std::default::Default;
use language1::{CloudNaturalLanguage, 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 = CloudNaturalLanguage::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 = AnalyzeEntitiesRequest::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.documents().analyze_entities(req)
.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(...)
。
定制和回调
您可以通过向Method Builder提供代理来改变doit()
方法的调用方式,在执行最后的doit()
调用之前。相应的方
该 委托特质 具有默认实现,让您可以以最小的努力对其进行自定义。
服务器请求中的可选部分
本库提供的所有结构都旨在通过 json 进行编码和 json 解码。可选部分用于表示部分请求和响应是有效的。大多数可选部分被视为 部分,这些部分可以通过名称识别,并将发送到服务器以指示请求的设置部分或响应中希望的部分。
构建器参数
通过使用 方法构建器,您可以通过重复调用其方法来准备一个操作调用。这些方法始终接受一个参数,以下陈述对它始终成立。
参数将始终复制或克隆到构建器中,以确保它们与其原始生命周期独立。
Cargo 功能
utoipa
- 添加对 utoipa 的支持,并派生所有类型的utoipa::ToSchema
。您必须在#[openapi(schemas(...))]
中导入和注册所需类型,否则生成的openapi
规范将无效。
许可证
language1 库由 Sebastian Thiel 生成,并置于 MIT 许可证之下。您可以在存储库的 许可证文件 中阅读全文。
依赖项
~12–23MB
~344K SLoC