Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active April 21, 2016 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/39ddc7900c7f9846e9a294ce75a2c737 to your computer and use it in GitHub Desktop.
Save guitarrapc/39ddc7900c7f9846e9a294ce75a2c737 to your computer and use it in GitHub Desktop.
public class AzureFunctionTrigger
{
public HttpStatusCode StatusCode { get; private set; }
public string ResponseMessage { get; private set;}
public static async Task<AzureFunctionTrigger> PostAsync(string functionUrl)
{
var payload = new
{
first = "Azure",
last = "Functions",
name = "Azure"
};
var jsonString = JsonConvert.SerializeObject(payload);
using (var client = new HttpClient())
{
var res = await client.PostAsync(functionUrl, new StringContent(jsonString, Encoding.UTF8, "application/json"));
if (!res.IsSuccessStatusCode)
{
return new AzureFunctionTrigger
{
StatusCode = res.StatusCode,
ResponseMessage = res.ReasonPhrase
};
}
var json = await res.Content.ReadAsStringAsync();
return new AzureFunctionTrigger
{
StatusCode = res.StatusCode,
ResponseMessage = json
};
}
}
public static async Task<AzureFunctionTrigger> PostAsync(string functionUrl, string key)
{
var payload = new
{
first = "Azure",
last = "Functions",
name = "Azure"
};
var jsonString = JsonConvert.SerializeObject(payload);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("x-functions-key", key);
var res = await client.PostAsync(functionUrl, new StringContent(jsonString, Encoding.UTF8, "application/json"));
if (!res.IsSuccessStatusCode)
{
return new AzureFunctionTrigger
{
StatusCode = res.StatusCode,
ResponseMessage = res.ReasonPhrase
};
}
var json = await res.Content.ReadAsStringAsync();
return new AzureFunctionTrigger
{
StatusCode = res.StatusCode,
ResponseMessage = json
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment