public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "put", Route = "callback/{id}")] HttpRequest req, [CosmosDB(databaseName: "doc", collectionName: "doc", Id = "{id}", PartitionKey = "shard", ConnectionStringSetting = "connection")] Models.StatusObj statusObj, [CosmosDB(databaseName: "doc", collectionName: "doc", ConnectionStringSetting = "connection")] IAsyncCollector <Models.StatusObj> item, ILogger log) { log.LogInformation("DocService function PUT /callback"); string requestBody = ""; try { requestBody = new StreamReader(req.Body).ReadToEnd(); } catch { log.LogWarning("TODO: Better exception handling"); } finally { if (requestBody != "") { dynamic data = JsonConvert.DeserializeObject(requestBody); statusObj.Status = data.status; statusObj.Detail = data.detail; } await item.AddAsync(statusObj); } return(string.IsNullOrEmpty(requestBody) ? new BadRequestObjectResult("error") : (ActionResult) new NoContentResult()); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", Route = "status/{id}")] HttpRequest req, [CosmosDB(databaseName: "doc", collectionName: "doc", Id = "{id}", PartitionKey = "shard", ConnectionStringSetting = "connection")] Models.StatusObj statusObj, ILogger log) { log.LogInformation("DocService function GET /status"); string json = JsonConvert.SerializeObject(statusObj); log.LogInformation(json); return(json == "null" ? new BadRequestObjectResult("error or id not found") : (ActionResult) new OkObjectResult($"{json}")); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = "request")] HttpRequest req, [CosmosDB(databaseName: "doc", collectionName: "doc", ConnectionStringSetting = "connection")] IAsyncCollector <Models.StatusObj> item, ILogger log) { log.LogInformation("DocService function POST /request"); var obj = new Models.StatusObj { }; string requestBody = ""; // string result = ""; try { requestBody = new StreamReader(req.Body).ReadToEnd(); } catch { log.LogWarning("TODO: Better exception handling"); } finally { obj.Id = Guid.NewGuid().ToString(); obj.PartId = "shard"; obj.Body = requestBody; await item.AddAsync(obj); // ****************** // using var c = new HttpClient(); // var post = await c.PostAsJsonAsync("https://webhook.site/b0972ceb-0ef6-4066-8bd6-ab67f26d500c", $"{{\"body\":{requestBody}, \"callback\":\"/callback/{obj.Id}\"}}"); // result = await post.Content.ReadAsStringAsync(); // log.LogInformation(result); // ****************** } return(string.IsNullOrEmpty(requestBody) ? new BadRequestObjectResult("error") : (ActionResult) new OkObjectResult($"{obj.Id}")); }