-
Notifications
You must be signed in to change notification settings - Fork 13
/
GetCharacterActivity.cs
59 lines (51 loc) · 2.5 KB
/
GetCharacterActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
// ReSharper disable once CheckNamespace
namespace DurableFunctions.Demo.DotNetCore.FanOutFanIn.Activities
{
public class GetCharacterActivity
{
public GetCharacterActivity(IHttpClientFactory httpClientFactory)
{
_httpClient = httpClientFactory.CreateClient();
}
[FunctionName(nameof(GetCharacterActivity))]
public async Task<string> Run(
[ActivityTrigger] string characterUri,
ILogger logger)
{
dynamic characterResult;
bool.TryParse(Environment.GetEnvironmentVariable("SkipRemoteSwapi"), out bool skipRemoteSwapi);
if (skipRemoteSwapi)
{
characterResult = GetLocalCharacterResult();
}
else
{
characterResult = await GetRemoteCharacterResult(characterUri);
}
return characterResult.name;
}
private async Task<object> GetRemoteCharacterResult(string characterUri)
{
var result = await _httpClient.GetAsync(characterUri);
if (!result.IsSuccessStatusCode)
{
return null;
}
var characterContent = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject(characterContent);
}
private static JToken GetLocalCharacterResult()
{
return JToken.Parse(@"{""name"":""!Local! Luke Skywalker"",""height"":""172"",""mass"":""77"",""hair_color"":""blond"",""skin_color"":""fair"",""eye_color"":""blue"",""birth_year"":""19BBY"",""gender"":""male"",""homeworld"":""https://swapi.co/api/planets/1/"",""films"":[""https://swapi.co/api/films/2/"",""https://swapi.co/api/films/6/"",""https://swapi.co/api/films/3/"",""https://swapi.co/api/films/1/"",""https://swapi.co/api/films/7/""],""species"":[""https://swapi.co/api/species/1/""],""vehicles"":[""https://swapi.co/api/vehicles/14/"",""https://swapi.co/api/vehicles/30/""],""starships"":[""https://swapi.co/api/starships/12/"",""https://swapi.co/api/starships/22/""],""created"":""2014-12-09T13:50:51.644000Z"",""edited"":""2014-12-20T21:17:56.891000Z"",""url"":""https://swapi.co/api/people/1/""}");
}
private readonly HttpClient _httpClient;
}
}