Created
April 22, 2020 15:03
-
-
Save justinyoo/01426032d1ee6886796d9cb72e048dd9 to your computer and use it in GitHub Desktop.
Building RequestBin with Durable Functions
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
[FunctionName(nameof(CreateBin))] | |
public async Task<IActionResult> CreateBin( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route="bins")] HttpRequest req, | |
[DurableClient] IDurableClient client) | |
{ | |
... | |
} |
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
[FunctionName(nameof(CreateBin))] | |
public async Task<IActionResult> CreateBin( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route="bins")] HttpRequest req, | |
[DurableClient] IDurableClient client) | |
{ | |
var binId = Guid.NewGuid(); | |
var bin = new EntityId("Bin", binId.ToString()); | |
... | |
} |
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
public interface IBin | |
{ | |
void Add(BinItem item); | |
void Reset(); | |
} |
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
[JsonObject(MemberSerialization = MemberSerialization.OptIn)] | |
public class Bin : IBin | |
{ | |
[JsonProperty("history")] | |
public virtual List<BinItem> History { get; set; } = new List<BinItem>(); | |
public void Add(BinItem item) | |
{ | |
if (item == null) | |
{ | |
return; | |
} | |
this.History.Insert(0, item); | |
} | |
public void Reset() | |
{ | |
this.History.Clear(); | |
} | |
[FunctionName(nameof(Bin))] | |
public static Task Run([EntityTrigger] IDurableEntityContext ctx) => ctx.DispatchAsync<Bin>(); | |
} |
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
[FunctionName(nameof(CreateBin))] | |
public async Task<IActionResult> CreateBin( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route="bins")] HttpRequest req, | |
[DurableClient] IDurableClient client) | |
{ | |
... | |
await client.SignalEntityAsync<IBin>(bin, o => o.Add(null)); | |
... | |
} |
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
[FunctionName(nameof(AddHistory))] | |
public async Task<IActionResult> AddHistory( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "patch", "delete", Route="bins/{binId}")] HttpRequest req, | |
[DurableClient] IDurableClient client, | |
string binId) | |
{ | |
var history = new BinItem(); | |
using (var reader = new StreamReader(req.Body)) | |
{ | |
history.Timestamp = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz"); | |
history.Method = req.Method; | |
history.Headers = req.Headers.AsEnumerable().ToDictionary(p => p.Key, p => string.Join(";", p.Value)); | |
history.Queries = req.Query.ToDictionary(p => p.Key, p => string.Join(";", p.Value)); | |
history.Body = await reader.ReadToEndAsync(); | |
} | |
... | |
} |
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
[FunctionName(nameof(AddHistory))] | |
public async Task<IActionResult> AddHistory( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "patch", "delete", Route="bins/{binId}")] HttpRequest req, | |
[DurableClient] IDurableClient client, | |
string binId) | |
{ | |
... | |
var bin = new EntityId("Bin", binId); | |
await client.SignalEntityAsync<IBin>(bin, o => o.Add(history)); | |
... | |
} |
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
[FunctionName(nameof(GetHistory))] | |
public async Task<IActionResult> GetHistory( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route="bins/{binId}/history")] HttpRequest req, | |
[DurableClient] IDurableClient client, | |
string binId) | |
{ | |
var bin = new EntityId("Bin", binId); | |
... | |
} |
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
[FunctionName(nameof(GetHistory))] | |
public async Task<IActionResult> GetHistory( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route="bins/{binId}/history")] HttpRequest req, | |
[DurableClient] IDurableClient client, | |
string binId) | |
{ | |
... | |
var entity = await client.ReadEntityStateAsync<Bin>(bin); | |
var payload = entity.EntityState; | |
var result = new JsonObjectContentResult(HttpStatusCode.OK, payload); | |
return result; | |
} |
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
[FunctionName(nameof(ResetHistory))] | |
public async Task<IActionResult> ResetHistory( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route="bins/{binId}/reset")] HttpRequest req, | |
[DurableClient] IDurableClient client, | |
string binId) | |
{ | |
var bin = new EntityId("Bin", binId); | |
... | |
} |
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
[FunctionName(nameof(ResetHistory))] | |
public async Task<IActionResult> ResetHistory( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route="bins/{binId}/reset")] HttpRequest req, | |
[DurableClient] IDurableClient client, | |
string binId) | |
{ | |
... | |
await client.SignalEntityAsync<IBin>(bin, o => o.Reset()); | |
... | |
} |
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
[FunctionName(nameof(PurgeBin))] | |
public async Task<IActionResult> PurgeBin( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route="bins/{binId}/purge")] HttpRequest req, | |
[DurableClient] IDurableClient client, | |
string binId) | |
{ | |
var bin = new EntityId("Bin", binId); | |
... | |
} |
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
[FunctionName(nameof(PurgeBin))] | |
public async Task<IActionResult> PurgeBin( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route="bins/{binId}/purge")] HttpRequest req, | |
[DurableClient] IDurableClient client, | |
string binId) | |
{ | |
... | |
await client.PurgeInstanceHistoryAsync($"@{bin.EntityName}@{bin.EntityKey}"); | |
var result = new NoContentResult(); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment