Skip to content

Commit 1936f57

Browse files
authored
Update azure_core README samples (#2182)
Made a few other edits for consistency with other READMEs.
1 parent 68f7fc8 commit 1936f57

File tree

5 files changed

+45
-45
lines changed

5 files changed

+45
-45
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/core/azure_core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ once_cell.workspace = true
3535
rustc_version.workspace = true
3636

3737
[dev-dependencies]
38+
azure_identity.path = "../../identity/azure_identity"
39+
azure_security_keyvault_secrets.path = "../../keyvault/azure_security_keyvault_secrets"
3840
time.workspace = true
3941
tracing-subscriber.workspace = true
4042
tokio.workspace = true

sdk/core/azure_core/README.md

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Azure Core shared client library for Rust
22

33
`azure_core` provides shared primitives, abstractions, and helpers for modern Rust Azure SDK client libraries.
4-
These libraries follow the [Azure SDK Design Guidelines for Rust](https://azure.github.io/azure-sdk/rust_introduction.html)
5-
and can be easily identified by package and namespaces names starting with `azure_`, e.g. `azure_identity`.
4+
These libraries follow the [Azure SDK Design Guidelines for Rust][guidelines]
5+
and can typically be identified by package and namespaces names starting with `azure_`, e.g. `azure_identity`.
66

7-
`azure_core` allows client libraries to expose common functionality in a consistent fashion,
7+
`azure_core` allows client libraries to expose common functionality in a consistent fashion
88
so that once you learn how to use these APIs in one client library, you will know how to use them in other client libraries.
99

1010
[Source code] | [Package (crates.io)] | [API Reference Documentation]
@@ -13,12 +13,12 @@ so that once you learn how to use these APIs in one client library, you will kno
1313

1414
Typically, you will not need to install `azure_core`;
1515
it will be installed for you when you install one of the client libraries using it.
16-
In case you want to install it explicitly (to implement your own client library, for example),
16+
In case you want to install it explicitly - to implement your own client library, for example -
1717
you can find the crates.io package [here][Package (crates.io)].
1818

1919
## Key concepts
2020

21-
The main shared concepts of `azure_core` (and so Azure SDK libraries using `azure_core`) include:
21+
The main shared concepts of `azure_core` - and Azure SDK libraries using `azure_core` - include:
2222

2323
- Configuring service clients, e.g. configuring retries, logging (`ClientOptions`).
2424
- Accessing HTTP response details (`Response<T>`).
@@ -29,22 +29,22 @@ The main shared concepts of `azure_core` (and so Azure SDK libraries using `azur
2929

3030
### Thread safety
3131

32-
We guarantee that all client instance methods are thread-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/rust_introduction.html)). This ensures that the recommendation of reusing client instances is always safe, even across threads.
32+
We guarantee that all client instance methods are thread-safe and independent of each other ([guidelines]). This ensures that the recommendation of reusing client instances is always safe, even across threads.
3333

3434
### Additional concepts
35+
3536
<!-- CLIENT COMMON BAR -->
3637
[Client options](#configuring-service-clients-using-clientoptions) |
3738
[Accessing the response](#accessing-http-response-details-using-responset) |
3839
[Handling Errors Results](#handling-errors-results) |
3940
[Consuming Service Methods Returning `Pager<T>`](#consuming-service-methods-returning-pagert)
40-
4141
<!-- CLIENT COMMON BAR -->
4242

4343
## Examples
4444

45-
**NOTE:** Samples in this file apply only to packages that follow [Azure SDK Design Guidelines](https://azure.github.io/azure-sdk/rust_introduction.html). Names of such packages usually start with `azure_`.
45+
**NOTE:** Samples in this file apply only to packages that follow [Azure SDK Design Guidelines][guidelines]. Names of such packages typically start with `azure_`.
4646

47-
### Configuring Service Clients Using `ClientOptions`
47+
### Configuring service clients using `ClientOptions`
4848

4949
Azure SDK client libraries typically expose one or more _service client_ types that
5050
are the main starting points for calling corresponding Azure services.
@@ -63,12 +63,13 @@ use azure_security_keyvault_secrets::{SecretClient, SecretClientOptions};
6363

6464
#[tokio::main]
6565
async fn main() -> Result<(), Box<dyn std::error::Error>> {
66+
let credential = DefaultAzureCredential::new()?;
67+
6668
let options = SecretClientOptions {
6769
api_version: "7.5".to_string(),
68-
client_options: ClientOptions::default(),
70+
..Default::default()
6971
};
7072

71-
let credential = DefaultAzureCredential::new()?;
7273
let client = SecretClient::new(
7374
"https://your-key-vault-name.vault.azure.net/",
7475
credential.clone(),
@@ -79,10 +80,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
7980
}
8081
```
8182

82-
### Accessing HTTP Response Details Using `Response<T>`
83+
### Accessing HTTP response details using `Response<T>`
8384

8485
_Service clients_ have methods that can be used to call Azure services. We refer to these client methods as _service methods_.
85-
_Service methods_ return a shared `azure_core` type `Response<T>` (in rare cases its non-generic sibling, a raw `Response`).
86+
_Service methods_ return a shared `azure_core` type `Response<T>` where `T` is either a `Model` type or a `ResponseBody` representing a raw stream of bytes.
8687
This type provides access to both the deserialized result of the service call, and to the details of the HTTP response returned from the server.
8788

8889
```rust no_run
@@ -101,16 +102,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
101102
)?;
102103

103104
// call a service method, which returns Response<T>
104-
let response: Response<SecretBundle> = client.get_secret("SecretName", "", None).await?;
105+
let response = client.get_secret("secret-name", "", None).await?;
105106

106-
// Response<T> has two main accessors.
107-
// The into_body() function for accessing the deserialized result of the call
107+
// Response<T> has two main accessors:
108+
// 1. The `into_body()` function consumes self to deserialize into a model type
108109
let secret = response.into_body().await?;
109110

110111
// get response again because it was moved in above statement
111-
let response: Response<SecretBundle> = client.get_secret("SecretName", "", None).await?;
112+
let response: Response<SecretBundle> = client.get_secret("secret-name", "", None).await?;
112113

113-
// .. and the deconstruct() method for accessing all the details of the HTTP response
114+
// 2. The deconstruct() method for accessing all the details of the HTTP response
114115
let (status, headers, body) = response.deconstruct();
115116

116117
// for example, you can access HTTP status
@@ -125,12 +126,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
125126
}
126127
```
127128

128-
### Handling Errors Results
129+
### Handling errors results
129130

130131
When a service call fails, the returned `Result` will contain an `Error`. The `Error` type provides a status property with an HTTP status code and an error_code property with a service-specific error code.
131132

132133
```rust no_run
133-
use azure_core::{error::HttpError, Response, StatusCode};
134+
use azure_core::{error::{ErrorKind, HttpError}, Response, StatusCode};
134135
use azure_identity::DefaultAzureCredential;
135136
use azure_security_keyvault_secrets::SecretClient;
136137

@@ -144,28 +145,28 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
144145
None,
145146
)?;
146147

147-
match client.get_secret("secret_name", "", None).await {
148+
match client.get_secret("secret-name", "", None).await {
148149
Ok(secret) => println!("Secret: {:?}", secret.into_body().await?.value),
149-
Err(e) => match e {
150-
azure_core::Error::HttpResponse { status, error_code, .. } if status == StatusCode::NotFound => {
150+
Err(e) => match e.kind() {
151+
ErrorKind::HttpResponse { status, error_code, .. } if *status == StatusCode::NotFound => {
151152
// handle not found error
152153
if let Some(code) = error_code {
153154
println!("ErrorCode: {}", code);
154155
} else {
155156
println!("Secret not found, but no error code provided.");
156157
}
157158
},
158-
_ => println!("An error occurred: {:?}", e),
159+
_ => println!("An error occurred: {e:?}"),
159160
},
160161
}
161-
162+
162163
Ok(())
163164
}
164165
```
165166

166-
### Consuming Service Methods Returning `Pager<T>`
167+
### Consuming service methods returning `Pager<T>`
167168

168-
If a service call returns multiple values in pages, it would return `Result<Pager<T>>` as a result. You can iterate over `AsyncPageable` directly or in pages.
169+
If a service call returns multiple values in pages, it would return `Result<Pager<T>>` as a result. You can iterator over each page's vector of results.
169170

170171
```rust no_run
171172
use azure_identity::DefaultAzureCredential;
@@ -174,29 +175,28 @@ use futures::TryStreamExt;
174175

175176
#[tokio::main]
176177
async fn main() -> Result<(), Box<dyn std::error::Error>> {
177-
// Create a new secret client
178+
// create a client
178179
let credential = DefaultAzureCredential::new()?;
179180
let client = SecretClient::new(
180181
"https://your-key-vault-name.vault.azure.net/",
181182
credential.clone(),
182183
None,
183184
)?;
184185

185-
// Get pager stream
186+
// get a stream
186187
let mut pager = client.get_secrets(None)?.into_stream();
187188

188-
// Poll pager until there are no more SecretListResults
189+
// poll the pager until there are no more SecretListResults
189190
while let Some(secrets) = pager.try_next().await? {
190-
// If Secrets List is None try next page
191191
let Some(secrets) = secrets.into_body().await?.value else {
192192
continue;
193193
};
194194

195-
// Loop through secrets in Secrets List
195+
// loop through secrets in SecretsListResults
196196
for secret in secrets {
197-
// Get the secret name from the ID.
197+
// get the secret name from the ID
198198
let name = secret.resource_id()?.name;
199-
println!("Found Secret with Name: {}", name);
199+
println!("Found secret with name: {}", name);
200200
}
201201
}
202202

@@ -221,3 +221,4 @@ This project has adopted the [Microsoft Open Source Code of Conduct]. For more i
221221
[API Reference Documentation]: https://docs.rs/azure_core
222222
[CONTRIBUTING.md]: https://github.com/Azure/azure-sdk-for-rust/blob/main/CONTRIBUTING.md
223223
[Code of Conduct FAQ]: https://opensource.microsoft.com/codeofconduct/faq/
224+
[guidelines]: https://azure.github.io/azure-sdk/rust_introduction.html

sdk/core/azure_core/src/lib.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
//! Core types and traits for the Rust Azure SDK.
5-
//!
6-
//! This crate is part of the unofficial Azure SDK effort in Rust. For more
7-
//! information on the project and an overview of other crates, please refer to
8-
//! [our GitHub repository](https://github.com/azure/azure-sdk-for-rust).
9-
//!
10-
//! It is a library that provides cross-cutting services to other client
11-
//! libraries. Please see the [general
12-
//! guidelines](https://azure.github.io/azure-sdk/general_azurecore.html).
13-
144
#![forbid(unsafe_code)]
155
#![deny(missing_debug_implementations, nonstandard_style)]
166
// #![warn(missing_docs, future_incompatible, unreachable_pub)]
7+
#![doc = include_str!("../README.md")]
178

189
#[macro_use]
1910
mod macros;

sdk/core/azure_core_amqp/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
// Copyright (c) Microsoft Corporation. All Rights reserved
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#![doc = include_str!("../README.md")]
5+
26
#[cfg(all(feature = "fe2o3-amqp", not(target_arch = "wasm32")))]
37
mod fe2o3;
48

0 commit comments

Comments
 (0)