/
Program.cs
149 lines (135 loc) · 6.12 KB
/
Program.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
using FiftyOne.GeoLocation;
using FiftyOne.GeoLocation.Core.Data;
using Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// @example Cloud/GettingStarted/Program.cs
///
/// Getting started example of using 51Degrees reverse geocoding.
///
/// This example is available in full on [GitHub](https://github.com/51Degrees/location-dotnet/blob/master/FiftyOne.GeoLocation/Examples/Cloud/GettingStarted/Program.cs).
///
/// To run this example, you will need to create a **resource key**.
/// The resource key is used as short-hand to store the particular set of
/// properties you are interested in as well as any associated license keys
/// that entitle you to increased request limits and/or paid-for properties.
///
/// You can create a resource key using the 51Degrees [Configurator](https://configure.51degrees.com).
///
/// Required NuGet Dependencies:
/// - FiftyOne.GeoLocation
///
/// The example shows how to:
///
/// 1. Build a new Pipeline with a cloud-based geolocation engine, with lazy
/// loading configured to allow up to a second for a response from the cloud
/// service.
/// 2. Create a new FlowData instance ready to be populated with evidence for
/// the Pipeline.
/// 3. Process a longitude and latitude to retrieve the values associated with
/// with the location for the selected properties.
/// 4. Extract the value of a property as a string from the results.
/// </summary>
namespace GettingStarted
{
public class Program
{
public class Example
{
private static ILoggerFactory _loggerFactory = new LoggerFactory();
private static string latitude = "51.458048";
private static string longitude = "-0.9822207999999999";
public void Run(string resourceKey, string cloudEndPoint = "")
{
// Create a new pipeline builder.
var pipelineBuilder = new GeoLocationPipelineBuilder(_loggerFactory)
// Obtain a resource key from https://configure.51degrees.com
// and select your Geo Location provider.
.UseCloud(resourceKey, FiftyOne.GeoLocation.Core.GeoLocationProvider.FiftyOneDegrees)
.UseLazyLoading(TimeSpan.FromSeconds(10));
// If a cloud endpoint has been provided then set the
// cloud pipeline endpoint.
if (string.IsNullOrWhiteSpace(cloudEndPoint) == false)
{
pipelineBuilder.SetEndPoint(cloudEndPoint);
}
using (var pipeline = pipelineBuilder.Build())
{
using (var data = pipeline.CreateFlowData())
{
data.AddEvidence("query.51D_Pos_latitude", latitude);
data.AddEvidence("query.51D_Pos_longitude", longitude);
data.Process();
var country = data.Get<IGeoData>().Country;
Console.Write($"Awaiting response");
CancellationTokenSource cancellationSource = new CancellationTokenSource();
Task.Run(() => { OutputUntilCancelled(".", 1000, cancellationSource.Token); });
cancellationSource.Cancel();
Console.WriteLine();
if (country.HasValue)
{
Console.WriteLine($"Country: {country.Value}");
}
else
{
Console.WriteLine($"Country: {country.NoValueMessage}");
}
}
}
}
private void OutputUntilCancelled(string text, int intervalMs, CancellationToken token)
{
while (token.IsCancellationRequested == false)
{
Console.Write(text);
Task.Delay(intervalMs).Wait();
}
}
}
static void Main(string[] args)
{
// Obtain a resource key for free at https://configure.51degrees.com
// Make sure to include the 'Country' property as it is used by this example.
string resourceKey = args.Length > 0 ? args[0] : "!!YOUR_RESOURCE_KEY!!";
if (resourceKey.StartsWith("!!"))
{
Console.WriteLine("You need to create a resource key at " +
"https://configure.51degrees.com and paste it into this example.");
Console.WriteLine("Make sure to include the 'Country' " +
"property as it is used by this example.");
}
else
{
new Example().Run(resourceKey);
}
#if (DEBUG)
Console.WriteLine("Complete. Press key to exit.");
Console.ReadKey();
#endif
}
}
}