Skip to content

Commit

Permalink
fix sample live execution (#21654)
Browse files Browse the repository at this point in the history
* fix sample live execution
  • Loading branch information
christothes authored Jun 7, 2021
1 parent b29a0b9 commit 45177ce
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 30 deletions.
141 changes: 127 additions & 14 deletions sdk/confidentialledger/Azure.Security.ConfidentialLedger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ string status = JsonDocument.Parse(statusResponse.Content)
.GetProperty("state")
.GetString();

Console.WriteLine($"Transaction status: {status}");

// Wait for the entry to be committed
while (status == "Pending")
{
statusResponse = ledgerClient.GetTransactionStatus(transactionId);
status = JsonDocument.Parse(statusResponse.Content)
.RootElement
.GetProperty("state")
.GetString();
}

Console.WriteLine($"Transaction status: {status}");
```

Expand Down Expand Up @@ -157,18 +169,45 @@ Response postResponse = ledgerClient.PostLedgerEntry(
RequestContent.Create(
new { contents = "Hello world!" }));
postResponse.Headers.TryGetValue(ConfidentialLedgerConstants.Headers.TransactionId, out string transactionId);
string subLedgerId = JsonDocument.Parse(statusResponse.Content)
string subLedgerId = JsonDocument.Parse(postResponse.Content)
.RootElement
.GetProperty("subLedgerId")
.GetString();

// Wait for the entry to be available.
status = "Pending";
while (status == "Pending")
{
statusResponse = ledgerClient.GetTransactionStatus(transactionId);
status = JsonDocument.Parse(statusResponse.Content)
.RootElement
.GetProperty("state")
.GetString();
}

Console.WriteLine($"Transaction status: {status}");

// Provide both the transactionId and subLedgerId.
Response getBySubledgerResponse = ledgerClient.GetLedgerEntry(transactionId, subLedgerId);

string contents = JsonDocument.Parse(getBySubledgerResponse.Content)
.RootElement
.GetProperty("contents")
.GetString();
// Try until the entry is available.
bool loaded = false;
JsonElement element = default;
string contents = null;
while (!loaded)
{
loaded = JsonDocument.Parse(getBySubledgerResponse.Content)
.RootElement
.TryGetProperty("entry", out element);
if (loaded)
{
contents = element.GetProperty("contents").GetString();
}
else
{
getBySubledgerResponse = ledgerClient.GetLedgerEntry(transactionId, subLedgerId);
}
}

Console.WriteLine(contents); // "Hello world!"
Expand All @@ -177,6 +216,7 @@ getBySubledgerResponse = ledgerClient.GetLedgerEntry(transactionId);

string subLedgerId2 = JsonDocument.Parse(getBySubledgerResponse.Content)
.RootElement
.GetProperty("entry")
.GetProperty("subLedgerId")
.GetString();

Expand All @@ -199,31 +239,104 @@ ledgerClient.PostLedgerEntry(

firstPostResponse.Headers.TryGetValue(ConfidentialLedgerConstants.Headers.TransactionId, out string transactionId);

// Wait for the entry to be committed
status = "Pending";
while (status == "Pending")
{
statusResponse = ledgerClient.GetTransactionStatus(transactionId);
status = JsonDocument.Parse(statusResponse.Content)
.RootElement
.GetProperty("state")
.GetString();
}

// The ledger entry written at the transactionId in firstResponse is retrieved from the default sub-ledger.
Response getResponse = ledgerClient.GetLedgerEntry(transactionId);

// Try until the entry is available.
loaded = false;
element = default;
contents = null;
while (!loaded)
{
loaded = JsonDocument.Parse(getResponse.Content)
.RootElement
.TryGetProperty("entry", out element);
if (loaded)
{
contents = element.GetProperty("contents").GetString();
}
else
{
getResponse = ledgerClient.GetLedgerEntry(transactionId, subLedgerId);
}
}

string firstEntryContents = JsonDocument.Parse(getResponse.Content)
.RootElement
.GetProperty("entry")
.GetProperty("contents")
.GetString();

Console.WriteLine(firstEntryContents); // "Hello world 0"
// This will return the latest entry available in the default sub-ledger.
getResponse = ledgerClient.GetCurrentLedgerEntry();
string latestDefaultSubLedger = JsonDocument.Parse(getResponse.Content)
.RootElement
.GetProperty("contents")
.GetString();

// Try until the entry is available.
loaded = false;
element = default;
string latestDefaultSubLedger = null;
while (!loaded)
{
loaded = JsonDocument.Parse(getResponse.Content)
.RootElement
.TryGetProperty("contents", out element);
if (loaded)
{
latestDefaultSubLedger = element.GetString();
}
else
{
getResponse = ledgerClient.GetCurrentLedgerEntry();
}
}

Console.WriteLine($"The latest ledger entry from the default sub-ledger is {latestDefaultSubLedger}"); //"Hello world 1"
// The ledger entry written at subLedgerTransactionId is retrieved from the sub-ledger 'sub-ledger'.
subLedgerPostResponse.Headers.TryGetValue(ConfidentialLedgerConstants.TransactionIdHeaderName, out string subLedgerTransactionId);

// Wait for the entry to be committed
status = "Pending";
while (status == "Pending")
{
statusResponse = ledgerClient.GetTransactionStatus(subLedgerTransactionId);
status = JsonDocument.Parse(statusResponse.Content)
.RootElement
.GetProperty("state")
.GetString();
}

getResponse = ledgerClient.GetLedgerEntry(subLedgerTransactionId, "my sub-ledger");
string subLedgerEntry = JsonDocument.Parse(getResponse.Content)
.RootElement
.GetProperty("contents")
.GetString();
// Try until the entry is available.
loaded = false;
element = default;
string subLedgerEntry = null;
while (!loaded)
{
loaded = JsonDocument.Parse(getResponse.Content)
.RootElement
.TryGetProperty("entry", out element);
if (loaded)
{
subLedgerEntry = element.GetProperty("contents").GetString();
}
else
{
getResponse = ledgerClient.GetLedgerEntry(subLedgerTransactionId, "my sub-ledger");
}
}

Console.WriteLine(subLedgerEntry); // "Hello world sub-ledger 0"
Expand All @@ -242,7 +355,7 @@ Console.WriteLine($"The latest ledger entry from the sub-ledger is {latestSubLed
Ledger entries in a sub-ledger may be retrieved over a range of transaction ids.

```C# Snippet:RangedQuery
ledgerClient.GetLedgerEntries(fromTransactionId: "2.1", toTransactionId: "4.5");
ledgerClient.GetLedgerEntries(fromTransactionId: "2.1", toTransactionId: subLedgerTransactionId);
```

### User management
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public void Setup()

public async Task GetUser(string objId)
{
// string objId = TestEnvironment.ConfidentialLedgerAdminOid;
var result = await Client.GetUserAsync(objId);
var stringResult = new StreamReader(result.ContentStream).ReadToEnd();

Expand Down
Loading

0 comments on commit 45177ce

Please sign in to comment.