Skip to content

Commit

Permalink
[TT-9627] Use Refit v6.0.24 (#12)
Browse files Browse the repository at this point in the history
* chore: using refit 6.0.24

* chore: using .net 5.0

* chore: added console app example
  • Loading branch information
chaddgrimm authored Sep 23, 2024
1 parent a6fce68 commit d2188e9
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
dotnet: [3.1.x]
dotnet: [5.0.408]
timeout-minutes: 10
steps:
- name: Checkout repo
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
dotnet: [3.1.x]
dotnet: [5.0.408]
name: Run tests using dotnet ${{ matrix.dotnet }}
steps:
- name: Checkout repo
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dotnet 3.1.426
dotnet 5.0.408
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ To run the tests, you need to provide the following environment variables:
```bash
dotnet test what3words.dotnet.wrapper.utests/what3words.dotnet.wrapper.utests.csproj

Test run for /w3w-dotnet-wrapper/what3words.dotnet.wrapper.utests/bin/Debug/netcoreapp3.1/what3words.dotnet.wrapper.utests.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.7.1
Test run for /w3w-dotnet-wrapper/what3words.dotnet.wrapper.utests/bin/Debug/net5.0/what3words.dotnet.wrapper.utests.dll(.NETCoreApp,Version=v5.0)
Microsoft (R) Test Execution Command Line Tool Version 16.11.0
Copyright (c) Microsoft Corporation. All rights reserved.

Starting test execution, please wait...
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.1.426"
"version": "5.0.408"
}
}
46 changes: 46 additions & 0 deletions sample/sample.Console/Arguments.cs
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;
}
}
120 changes: 120 additions & 0 deletions sample/sample.Console/Program.cs
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);
}
}
}
}
12 changes: 12 additions & 0 deletions sample/sample.Console/README.md
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>

```
12 changes: 12 additions & 0 deletions sample/sample.Console/sample.Console.csproj
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>
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand All @@ -11,7 +12,7 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1" />
<PackageReference Include="ReportGenerator" Version="4.4.7" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
<PackageReference Include="coverlet.msbuild" Version="2.8.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion what3words.dotnet.wrapper/what3words.dotnet.wrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Refit" Version="5.2.4" />
<PackageReference Include="Refit" Version="6.0.24" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit d2188e9

Please sign in to comment.