-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSearchCharacterActivity.cs
64 lines (55 loc) · 2.87 KB
/
SearchCharacterActivity.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
60
61
62
63
64
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using DurableFunctions.Demo.DotNetCore.Chaining.Activities.Models;
using DurableTask.Core.Exceptions;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.DurableTask;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
// ReSharper disable once CheckNamespace
namespace DurableFunctions.Demo.DotNetCore.Chaining.Activities
{
public class SearchCharacterActivity
{
private readonly HttpClient _httpClient;
public SearchCharacterActivity(IHttpClientFactory httpClientFactory)
{
_httpClient = httpClientFactory.CreateClient();
}
[FunctionName(nameof(SearchCharacterActivity))]
public async Task<Character> Run(
[ActivityTrigger] string name,
ILogger logger)
{
bool.TryParse(Environment.GetEnvironmentVariable("SkipRemoteSwapi"), out bool skipRemoteSwapi);
string characterResult;
if (skipRemoteSwapi)
{
characterResult = GetLocalPersonSearchResult();
}
else
{
characterResult = await GetRemotePersonSearchResult(name);
}
var characters = JToken.Parse(characterResult).SelectToken("results").ToObject<Character[]>();
return characters.FirstOrDefault();
}
private async Task<string> GetRemotePersonSearchResult(string name)
{
var uri = $"{Environment.GetEnvironmentVariable("SwapiBaseUrl")}people?search={name}";
var result = await _httpClient.GetAsync(uri);
if (!result.IsSuccessStatusCode)
{
throw new TaskFailedException($"SwApi returned status code {result.StatusCode}");
}
var personContent = await result.Content.ReadAsStringAsync();
return personContent;
}
private static string GetLocalPersonSearchResult()
{
return @"{""count"":1,""next"":null,""previous"":null,""results"":[{""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/""}]}";
}
}
}