-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: using refit 6.0.24 * chore: using .net 5.0 * chore: added console app example
- Loading branch information
1 parent
a6fce68
commit d2188e9
Showing
11 changed files
with
200 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
dotnet 3.1.426 | ||
dotnet 5.0.408 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"sdk": { | ||
"version": "3.1.426" | ||
"version": "5.0.408" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* A simple utility class to parse command line arguments. | ||
*/ | ||
namespace sample.Console.Utils | ||
{ | ||
using System; | ||
public class Arguments | ||
{ | ||
private string[] _args; | ||
private string _command; | ||
public Arguments(string[] args) | ||
{ | ||
_command = this.ResolveCommand(args); | ||
_args = args; | ||
} | ||
|
||
private string ResolveCommand(string[] args) | ||
{ | ||
if (string.IsNullOrEmpty(args[0]) || args[0].StartsWith("--")) | ||
{ | ||
if (args[0].StartsWith("--") && args[0].Equals("--help")) | ||
{ | ||
return "help"; | ||
} | ||
throw new InvalidOperationException("Invalid command provided."); | ||
} | ||
return args[0]; | ||
} | ||
|
||
public string GetArgument(string name) | ||
{ | ||
if (_args == null || _args.Length == 0) | ||
{ | ||
throw new InvalidOperationException("No arguments provided."); | ||
} | ||
int index = Array.IndexOf(_args, name); | ||
if (index >= 0 && index + 1 < _args.Length && !string.IsNullOrEmpty(_args[index + 1]) && !_args[index + 1].StartsWith("--")) | ||
{ | ||
return _args[index + 1]; | ||
} | ||
throw new InvalidOperationException($"{name} is missing or not provided."); | ||
} | ||
|
||
public string GetCommand() => _command; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Simple console application using the what3words-dotnet-wrapper. | ||
*/ | ||
|
||
using what3words.dotnet.wrapper; | ||
using what3words.dotnet.wrapper.models; | ||
using what3words.dotnet.wrapper.response; | ||
using System.Linq; | ||
using System.Globalization; | ||
|
||
namespace sample.Console | ||
{ | ||
using System; | ||
using Utils; | ||
|
||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
try | ||
{ | ||
var arguments = new Arguments(args); | ||
if (arguments.GetCommand().Equals("help")) | ||
{ | ||
PrintUsage(); | ||
return; | ||
} | ||
string apiKey = arguments.GetArgument("--api-key"); | ||
if (apiKey != null) | ||
{ | ||
What3WordsV3 api = new What3WordsV3(apiKey); | ||
switch (arguments.GetCommand()) | ||
{ | ||
case "convert-to-coordinates": | ||
ConvertToCoordinates(api, arguments); | ||
break; | ||
case "convert-to-3wa": | ||
ConvertTo3WA(api, arguments); | ||
break; | ||
case "autosuggest": | ||
AutoSuggest(api, arguments); | ||
break; | ||
default: | ||
Console.WriteLine("Command is not supported."); | ||
PrintUsage(); | ||
break; | ||
} | ||
} | ||
} | ||
catch (Exception error) | ||
{ | ||
Console.WriteLine("Something went wrong, " + error.Message); | ||
PrintUsage(); | ||
} | ||
} | ||
|
||
static void PrintUsage() | ||
{ | ||
Console.WriteLine("Usage: <command> [options]"); | ||
Console.WriteLine("Required parameters:"); | ||
Console.WriteLine(" --api-key <key>"); | ||
Console.WriteLine("Commands:"); | ||
Console.WriteLine(" convert-to-coordinates --3wa <3 word address>"); | ||
Console.WriteLine(" convert-to-3wa --lat <latitude> --lng <longitude>"); | ||
Console.WriteLine(" autosuggest --input <input>"); | ||
} | ||
|
||
static void ConvertToCoordinates(What3WordsV3 api, Arguments arg) | ||
{ | ||
var threeWords = arg.GetArgument("--3wa"); | ||
var result = api.ConvertToCoordinates(threeWords).RequestAsync().Result; | ||
if (result.IsSuccessful) | ||
{ | ||
Console.WriteLine("Coordinates: " + result.Data.Coordinates.Lat + ", " + result.Data.Coordinates.Lng); | ||
} | ||
else | ||
{ | ||
Console.WriteLine(result.Error.Code + " - " + result.Error.Message); | ||
} | ||
} | ||
|
||
static void ConvertTo3WA(What3WordsV3 api, Arguments arg) | ||
{ | ||
double latitude, longitude; | ||
var lat = double.TryParse(arg.GetArgument("--lat"), NumberStyles.Any, CultureInfo.InvariantCulture, out latitude) ? latitude : 0.0; | ||
var lng = double.TryParse(arg.GetArgument("--lng"), NumberStyles.Any, CultureInfo.InvariantCulture, out longitude) ? longitude : 0.0; | ||
var coordinates = new Coordinates(lat, lng); | ||
var result = api.ConvertTo3WA(coordinates).RequestAsync().Result; | ||
if (result.IsSuccessful) | ||
{ | ||
Console.WriteLine($"3 word address: https://w3w.co/{result.Data.Words}"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine(result.Error.Code + " - " + result.Error.Message); | ||
} | ||
} | ||
|
||
static void AutoSuggest(What3WordsV3 api, Arguments arg) | ||
{ | ||
var input = arg.GetArgument("--input"); | ||
var result = api.Autosuggest(input).RequestAsync().Result; | ||
if (result.IsSuccessful) | ||
{ | ||
if (result.Data.Suggestions.Count > 0) | ||
{ | ||
Console.WriteLine("Suggestions: " + string.Join(", ", result.Data.Suggestions.Select(x => $"https://w3w.co/{x.Words}"))); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("No suggestions found."); | ||
} | ||
} | ||
else | ||
{ | ||
Console.WriteLine(result.Error.Code + " - " + result.Error.Message); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
```sh | ||
$ dotnet run -- --help | ||
|
||
Usage: <command> [options] | ||
Required parameters: | ||
--api-key <key> | ||
Commands: | ||
convert-to-coordinates --3wa <3 word address> | ||
convert-to-3wa --lat <latitude> --lng <longitude> | ||
autosuggest --input <input> | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>.NET5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\what3words.dotnet.wrapper\what3words.dotnet.wrapper.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters