-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
120 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Formats.Cbor; | ||
using EdjCase.ICP.Agent.Models; | ||
using EdjCase.ICP.Candid.Models; | ||
|
||
namespace EdjCase.ICP.Agent.Responses | ||
{ | ||
/// <summary> | ||
/// Model for a reponse to a read state request | ||
/// </summary> | ||
public class V3CallResponse | ||
{ | ||
/// <summary> | ||
/// The status of the call ('replied', 'rejected', 'done) | ||
/// </summary> | ||
public string Status { get; } | ||
/// <summary> | ||
/// The certificate data of the current canister state | ||
/// </summary> | ||
public Certificate Certificate { get; } | ||
|
||
/// <param name="status">The status of the call ('replied', 'rejected', 'done)</param> | ||
/// <param name="certificate">The certificate data of the current canister state</param> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public V3CallResponse(string status, Certificate certificate) | ||
{ | ||
this.Status = status ?? throw new ArgumentNullException(nameof(status)); | ||
this.Certificate = certificate ?? throw new ArgumentNullException(nameof(certificate)); | ||
} | ||
|
||
|
||
internal static V3CallResponse ReadCbor(CborReader reader) | ||
{ | ||
if (reader.ReadTag() != CborTag.SelfDescribeCbor) | ||
{ | ||
throw new CborContentException("Expected self describe tag"); | ||
} | ||
Certificate? certificate = null; | ||
string? status = null; | ||
reader.ReadStartMap(); | ||
while (reader.PeekState() != CborReaderState.EndMap) | ||
{ | ||
string field = reader.ReadTextString(); | ||
switch (field) | ||
{ | ||
case "certificate": | ||
var certReader = new CborReader(reader.ReadByteString()); | ||
certificate = Certificate.FromCbor(certReader); | ||
break; | ||
case "status": | ||
status = reader.ReadTextString(); | ||
break; | ||
default: | ||
Debug.WriteLine($"Unknown field '{field}' in v3 call response"); | ||
reader.SkipValue(); | ||
break; | ||
} | ||
} | ||
reader.ReadEndMap(); | ||
|
||
if (status == null) | ||
{ | ||
throw new CborContentException("Missing field: status"); | ||
} | ||
|
||
if (certificate == null) | ||
{ | ||
throw new CborContentException("Missing field: certificate"); | ||
} | ||
|
||
return new V3CallResponse(status, certificate); | ||
} | ||
} | ||
} |