Create a gist now

Instantly share code, notes, and snippets.

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