Skip to content

Commit 79cde32

Browse files
authored
Add usage to sdk (#467)
* Adding usage to SDK README's to fix #379 * Exit sdk folder * Grep markdown changes in sdk subfolder * Fail build if markdown changes exist
1 parent 2d48f9a commit 79cde32

File tree

26 files changed

+333
-5
lines changed

26 files changed

+333
-5
lines changed

.github/workflows/build.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ jobs:
6262
- name: sdk tests
6363
run: cargo test --all --features mock_transport_framework
6464

65+
- name: update readme of sdks
66+
run: |
67+
cargo install cargo-readme
68+
./eng/scripts/cargo_readme.sh
69+
if git status sdk | grep -q *.md; then
70+
echo "Run ./eng/scripts/cargo_readme.sh to update readmes" && exit 1
71+
fi
72+
6573
test-services:
6674
name: Services Tests
6775
runs-on: ubuntu-20.04

eng/scripts/cargo_readme.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
cd sdk
4+
for crate in *
5+
do
6+
if [ -d "$crate" ]; then
7+
cd "$crate"
8+
echo Updating README.md for "$crate"
9+
cargo readme > README.md
10+
cd ..
11+
fi
12+
done
13+
cd ..

sdk/core/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
# Azure SDK for Rust - Core crate
22

33
Core crate for the unofficial Microsoft Azure SDK for Rust. This crate is part of a collection of crates: for more information please refer to [https://github.com/azure/azure-sdk-for-rust](https://github.com/azure/azure-sdk-for-rust).
4+
5+
## Usage
6+
7+
To set this crate as a dependency, add this to your Cargo.toml
8+
9+
```toml
10+
[dependencies]
11+
azure_core = { version = "0.1.0", git = "https://github.com/Azure/azure-sdk-for-rust" }
12+
```

sdk/core/README.tpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Azure SDK for Rust - Core crate
2+
3+
{{readme}}
4+
5+
## Usage
6+
7+
To set this crate as a dependency, add this to your Cargo.toml
8+
9+
```toml
10+
[dependencies]
11+
{{crate}} = { version = "{{version}}", git = "https://github.com/Azure/azure-sdk-for-rust" }
12+
```

sdk/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Core crate for the unofficial Microsoft Azure SDK for Rust. This crate is part of a collection of crates: for more information please refer to [https://github.com/azure/azure-sdk-for-rust](https://github.com/azure/azure-sdk-for-rust).
12
#![recursion_limit = "256"]
23
#![warn(rust_2018_idioms)]
34

sdk/cosmos/README.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,101 @@
11
# Azure SDK for Rust - Azure Cosmos DB crate
22

3-
Azure Cosmos DB crate for the unofficial Microsoft Azure SDK for Rust. This crate is part of a collection of crates: for more information please refer to [https://github.com/azure/azure-sdk-for-rust](https://github.com/azure/azure-sdk-for-rust).
3+
## The Cosmos DB crate.
4+
5+
`azure-cosmos` offers functionality needed to interact with Cosmos DB from Rust. As an abstraction over the [Cosmos DB
6+
Rest API](https://docs.microsoft.com/rest/api/cosmos-db/), anything that is possible through that Rest API
7+
should also be possible with this crate.
8+
9+
### Examples
10+
11+
```rust
12+
// Using the prelude module of the Cosmos crate makes easier to use the Rust Azure SDK for Cosmos DB.
13+
use azure_cosmos::prelude::*;
14+
use azure_core::Context;
15+
use serde::{Deserialize, Serialize};
16+
use std::error::Error;
17+
18+
// This is the stuct we want to use in our sample.
19+
// Make sure to have a collection with partition key "a_number" for this example to
20+
// work (you can create with this SDK too, check the examples folder for that task).
21+
#[derive(Serialize, Deserialize, Debug)]
22+
struct MySampleStruct {
23+
id: String,
24+
a_string: String,
25+
a_number: u64,
26+
a_timestamp: i64,
27+
}
28+
29+
impl<'a> azure_cosmos::CosmosEntity<'a> for MySampleStruct {
30+
type Entity = u64;
31+
32+
fn partition_key(&'a self) -> Self::Entity {
33+
self.a_number
34+
}
35+
}
36+
37+
// This code will perform these tasks:
38+
// 1. Create 10 documents in the collection.
39+
#[tokio::main]
40+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
41+
// Let's get Cosmos account and master key from env variables.
42+
let master_key =
43+
std::env::var("COSMOS_MASTER_KEY").expect("Set env variable COSMOS_MASTER_KEY first!");
44+
let account = std::env::var("COSMOS_ACCOUNT").expect("Set env variable COSMOS_ACCOUNT first!");
45+
46+
let database_name = std::env::args()
47+
.nth(1)
48+
.expect("please specify the database name as first command line parameter");
49+
let collection_name = std::env::args()
50+
.nth(2)
51+
.expect("please specify the collection name as first command line parameter");
52+
53+
// First, we create an authorization token. There are two types of tokens, master and resource
54+
// constrained. This SDK supports both.
55+
// Please check the Azure documentation for details or the examples folder
56+
// on how to create and use token-based permissions.
57+
let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;
58+
59+
// Next we will create a Cosmos client.
60+
let client = CosmosClient::new(account.clone(), authorization_token, CosmosOptions::default());
61+
62+
// We know the database so we can obtain a database client.
63+
let database_client = client.into_database_client(database_name);
64+
// We know the collection so we can obtain a collection client.
65+
let collection_client = database_client.into_collection_client(collection_name);
66+
67+
// Insert 10 documents
68+
println!("Inserting 10 documents...");
69+
for i in 0..10 {
70+
// define the document.
71+
let document_to_insert = MySampleStruct {
72+
id: format!("unique_id{}", i),
73+
a_string: "Something here".to_owned(),
74+
a_number: i * 100, // this is the partition key
75+
a_timestamp: chrono::Utc::now().timestamp(),
76+
};
77+
78+
// insert it
79+
collection_client
80+
.create_document(
81+
Context::new(),
82+
&document_to_insert,
83+
CreateDocumentOptions::new().is_upsert(true),
84+
)
85+
.await?;
86+
}
87+
// wow that was easy and fast, wasn't it? :)
88+
println!("Done!");
89+
90+
Ok(())
91+
}
92+
```
93+
94+
## Usage
95+
96+
To set this crate as a dependency, add this to your Cargo.toml
97+
98+
```toml
99+
[dependencies]
100+
azure_cosmos = { version = "0.1.0", git = "https://github.com/Azure/azure-sdk-for-rust" }
101+
```

sdk/cosmos/README.tpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Azure SDK for Rust - Azure Cosmos DB crate
2+
3+
{{readme}}
4+
5+
## Usage
6+
7+
To set this crate as a dependency, add this to your Cargo.toml
8+
9+
```toml
10+
[dependencies]
11+
{{crate}} = { version = "{{version}}", git = "https://github.com/Azure/azure-sdk-for-rust" }
12+
```

sdk/cosmos/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
8989
Ok(())
9090
}
9191
```
92-
!*/
92+
*/
9393

9494
#![warn(unused_extern_crates)]
9595
#![deny(missing_docs)]

sdk/identity/README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
11
# Azure SDK for Rust - Azure OAuth2 Crate
22

3-
Azure OAuth2 helper crate for the unofficial Microsoft Azure SDK for Rust. This crate is part of a collection of crates: for more information please refer to [https://github.com/azure/azure-sdk-for-rust](https://github.com/azure/azure-sdk-for-rust).
3+
Azure OAuth2 helper crate for the unofficial Microsoft Azure SDK for Rust. This crate is part of a collection of crates: for more information please refer to [https://github.com/azure/azure-sdk-for-rust](https://github.com/azure/azure-sdk-for-rust).
4+
This crate provides mechanisms for several ways to authenticate against Azure
5+
6+
For example, to authenticate using the client credential flow, you can do the following:
7+
8+
```rust
9+
use azure_identity::client_credentials_flow;
10+
use oauth2::{ClientId, ClientSecret};
11+
use url::Url;
12+
13+
use std::env;
14+
use std::error::Error;
15+
16+
#[tokio::main]
17+
async fn main() -> Result<(), Box<dyn Error>> {
18+
let client_id =
19+
ClientId::new(env::var("CLIENT_ID").expect("Missing CLIENT_ID environment variable."));
20+
let client_secret = ClientSecret::new(
21+
env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable."),
22+
);
23+
let tenant_id = env::var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
24+
let subscription_id =
25+
env::var("SUBSCRIPTION_ID").expect("Missing SUBSCRIPTION_ID environment variable.");
26+
27+
let client = reqwest::Client::new();
28+
// This will give you the final token to use in authorization.
29+
let token = client_credentials_flow::perform(
30+
client,
31+
&client_id,
32+
&client_secret,
33+
"https://management.azure.com/",
34+
&tenant_id,
35+
)
36+
.await?;
37+
Ok(())
38+
}
39+
```
40+
41+
The supported authentication flows are:
42+
* [Authorization code flow](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-auth-code-flow).
43+
* [Client credentials flow](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow).
44+
* [Device code flow](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-device-code).
45+
46+
This crate also includes utilities for handling refresh tokens and accessing token credentials from many different sources.
47+
48+
## Usage
49+
50+
To set this crate as a dependency, add this to your Cargo.toml
51+
52+
```toml
53+
[dependencies]
54+
azure_identity = { version = "0.1.0", git = "https://github.com/Azure/azure-sdk-for-rust" }
55+
```

sdk/identity/README.tpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Azure SDK for Rust - Azure OAuth2 Crate
2+
3+
{{readme}}
4+
5+
## Usage
6+
7+
To set this crate as a dependency, add this to your Cargo.toml
8+
9+
```toml
10+
[dependencies]
11+
{{crate}} = { version = "{{version}}", git = "https://github.com/Azure/azure-sdk-for-rust" }
12+
```

0 commit comments

Comments
 (0)