-
Notifications
You must be signed in to change notification settings - Fork 13
/
SearchPlanetActivity.cs
63 lines (54 loc) · 2.87 KB
/
SearchPlanetActivity.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
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using DurableFunctions.Demo.DotNetCore.FanOutFanIn.Orchestrators.Models;
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.FanOutFanIn.Activities
{
public class SearchPlanetActivity
{
public SearchPlanetActivity(IHttpClientFactory httpClientFactory)
{
_httpClient = httpClientFactory.CreateClient();
}
[FunctionName(nameof(SearchPlanetActivity))]
public async Task<Planet> Run(
[ActivityTrigger] string name,
ILogger logger)
{
string planetResult;
bool.TryParse(Environment.GetEnvironmentVariable("SkipRemoteSwapi"), out bool skipRemoteSwapi);
if (skipRemoteSwapi)
{
planetResult = GetLocalPlanetSearchResult();
}
else
{
planetResult = await GetRemotePlanetSearchResult(name);
}
var planets = JToken.Parse(planetResult).SelectToken("results").ToObject<Planet[]>();
return planets.FirstOrDefault();
}
private async Task<string> GetRemotePlanetSearchResult(string name)
{
var uri = $"{Environment.GetEnvironmentVariable("SwapiBaseUrl")}planets?search={name}";
var result = await _httpClient.GetAsync(uri);
if (!result.IsSuccessStatusCode)
{
return null;
}
var planetContent = await result.Content.ReadAsStringAsync();
return planetContent;
}
private static string GetLocalPlanetSearchResult()
{
return @"{""count"":1,""next"":null,""previous"":null,""results"":[{""name"":""!Local! Tatooine"",""rotation_period"":""23"",""orbital_period"":""304"",""diameter"":""10465"",""climate"":""arid"",""gravity"":""1 standard"",""terrain"":""desert"",""surface_water"":""1"",""population"":""200000"",""residents"":[""https://swapi.co/api/people/1/"",""https://swapi.co/api/people/2/"",""https://swapi.co/api/people/4/"",""https://swapi.co/api/people/6/"",""https://swapi.co/api/people/7/"",""https://swapi.co/api/people/8/"",""https://swapi.co/api/people/9/"",""https://swapi.co/api/people/11/"",""https://swapi.co/api/people/43/"",""https://swapi.co/api/people/62/""],""films"":[""https://swapi.co/api/films/5/"",""https://swapi.co/api/films/4/"",""https://swapi.co/api/films/6/"",""https://swapi.co/api/films/3/"",""https://swapi.co/api/films/1/""],""created"":""2014-12-09T13:50:49.641000Z"",""edited"":""2014-12-21T20:48:04.175778Z"",""url"":""https://swapi.co/api/planets/1/""}]}";
}
private readonly HttpClient _httpClient;
}
}