Last active
September 11, 2018 10:28
-
-
Save atomicscope/a8486efe1adaf14f63a229eec21a980c to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Azure.WebJobs.Host; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
namespace BitCoinTracker | |
{ | |
public static class BitCoinTracker | |
{ | |
[FunctionName("BitCoinTracker")] | |
public static async Task<HttpResponseMessage> Run( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] | |
HttpRequestMessage req, TraceWriter log) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
client.BaseAddress = new Uri("https://api.coindesk.com/v1/bpi/currentprice.json"); | |
var result = await client.GetAsync(""); | |
var resultContent = await result.Content.ReadAsStringAsync(); | |
log.Info(resultContent); | |
var httpContent = new StringContent(resultContent, Encoding.UTF8, "application/json"); | |
//*****Mandatory Headers******// | |
var subscriptionid = (string)ConfigurationManager.AppSettings["SubscriptionId"]; | |
var resourceGroupName = (string)ConfigurationManager.AppSettings["ResourceGroupName"]; | |
var businessProcessName = (string)ConfigurationManager.AppSettings["BusinessProcessName"]; | |
var businessTransactionName = (string)ConfigurationManager.AppSettings["BusinessTransactionName"]; | |
var currentstageName = (string)ConfigurationManager.AppSettings["CurrentStageName"]; | |
client.DefaultRequestHeaders.Add("x-ms-workflow-subscription-id", subscriptionid); | |
client.DefaultRequestHeaders.Add("x-ms-workflow-resourcegroup-name", resourceGroupName); | |
client.DefaultRequestHeaders.Add("AS-BusinessProcess", businessProcessName); | |
client.DefaultRequestHeaders.Add("AS-BusinessTransaction", businessTransactionName); | |
client.DefaultRequestHeaders.Add("AS-CurrentStage", currentstageName); | |
var startActivityUri = new Uri(req.RequestUri.AbsoluteUri.Replace(req.RequestUri.PathAndQuery, "/api/StartActivity")); | |
log.Info("Another function URL : " + startActivityUri); | |
log.Info("Http Content ( MessageBody) :" + httpContent.ReadAsStringAsync().Result); | |
var activityContent = new ActivityContent() | |
{ | |
MessageBody = httpContent.ReadAsStringAsync().Result, | |
MessageHeader = httpContent.Headers.ToDictionary(s => s.Key, s => s.Value) | |
}; | |
var json = JsonConvert.SerializeObject(activityContent, Formatting.Indented); | |
log.Info("Calling Start Activity"); | |
var startActivityResponse = await client.PostAsync(startActivityUri, new StringContent(json)); | |
log.Info("Response from Start Activity :" + startActivityResponse); | |
var response = startActivityResponse.Content.ReadAsAsync<JObject>().Result; | |
var mainActivityId = response.GetValue("MainActivityId").Value<string>(); | |
var stageActivityId = response.GetValue("StageActivityId").Value<string>(); | |
var output = response.GetValue("Result").Value<string>(); | |
log.Info("MainActivityId: " + mainActivityId + "\n, StageActivityId " + stageActivityId + "\n, " + output); | |
log.Info("Start Activity Completed"); | |
log.Info("Adding Headers for Update Activity"); | |
client.DefaultRequestHeaders.Add("AS-MainActivityId", mainActivityId); | |
client.DefaultRequestHeaders.Add("AS-StageActivityId", stageActivityId); | |
client.DefaultRequestHeaders.Add("AS-Status", "Success"); | |
client.DefaultRequestHeaders.Remove("AS-ArchiveMessage"); | |
log.Info("Successfully Added Headers"); | |
var updateActivityUri = new Uri(req.RequestUri.AbsoluteUri.Replace(req.RequestUri.PathAndQuery, "/api/UpdateActivity")); | |
log.Info("Calling update Activity"); | |
var updateActivityResponse = await client.PostAsync(updateActivityUri, new StringContent(activityContent.MessageBody)); | |
log.Info("Response from Update Activity :" + updateActivityResponse); | |
log.Info("Update Activity Completed"); | |
log.Info("Initiating new Start Activity"); | |
log.Info("Updating Start Activity Headers"); | |
client.DefaultRequestHeaders.Add("AS-PreviousStage", "GetPrice"); | |
client.DefaultRequestHeaders.Remove("AS-CurrentStage"); | |
client.DefaultRequestHeaders.Add("AS-CurrentStage", "Track CoinPrice"); | |
startActivityResponse = await client.PostAsync(startActivityUri, new StringContent(json)); | |
log.Info("Response from Start Activity :" + startActivityResponse); | |
response = startActivityResponse.Content.ReadAsAsync<JObject>().Result; | |
//Reading output for mainactivity & StageactivityId | |
mainActivityId = response.GetValue("MainActivityId").Value<string>(); | |
stageActivityId = response.GetValue("StageActivityId").Value<string>(); | |
output = response.GetValue("Result").Value<string>(); | |
log.Info("MainActivityId: " + mainActivityId + "\n, StageActivityId " + stageActivityId + "\n, " + output); | |
log.Info("Start Activity Completed"); | |
log.Info("Initiating Update Activity"); | |
updateActivityResponse = await client.PostAsync(updateActivityUri, new StringContent(activityContent.MessageBody)); | |
log.Info("Response from Update Activity :" + updateActivityResponse); | |
log.Info("Update Activity Completed"); | |
} | |
return req.CreateResponse(HttpStatusCode.Accepted); | |
} | |
} | |
internal class ActivityContent | |
{ | |
public dynamic MessageBody { get; set; } | |
public dynamic MessageHeader { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment