Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bytefish committed Sep 24, 2020
0 parents commit 76aa317
Show file tree
Hide file tree
Showing 32 changed files with 3,475 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.iml
.vs/
.idea/
*.xproj.user
*.csproj.user
project.lock.json
bin/
obj/
packages/
*.DotSettings.user
*.db
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Philipp Wagner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OpenSkyRestClient\OpenSkyRestClient.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\all_state_vectors_response.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions OpenSkyRestClient/OpenSkyRestClient.Test/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using NUnit.Framework;
using OpenSkyRestClient.Parser;
using System;
using System.IO;

namespace OpenSkyRestClient.Test
{
public class Tests
{
public string GetFileContent(string filename)
{
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", filename);

var text = File.ReadAllText(filePath);

return text;
}

[Test]
public void ParseStateVectorResponseTest()
{
var json = GetFileContent("all_state_vectors_response.json");

var res = StateVectorResponseParser.Parse(json);

}
}
}
31 changes: 31 additions & 0 deletions OpenSkyRestClient/OpenSkyRestClient.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenSkyRestClient", "OpenSkyRestClient\OpenSkyRestClient.csproj", "{AFF625DD-04FA-4FB0-87DF-CFB2106C59FD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenSkyRestClient.Test", "OpenSkyRestClient.Test\OpenSkyRestClient.Test.csproj", "{B4E0F040-4C9C-4A16-B722-FC3BDB0BB747}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AFF625DD-04FA-4FB0-87DF-CFB2106C59FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFF625DD-04FA-4FB0-87DF-CFB2106C59FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFF625DD-04FA-4FB0-87DF-CFB2106C59FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFF625DD-04FA-4FB0-87DF-CFB2106C59FD}.Release|Any CPU.Build.0 = Release|Any CPU
{B4E0F040-4C9C-4A16-B722-FC3BDB0BB747}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4E0F040-4C9C-4A16-B722-FC3BDB0BB747}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4E0F040-4C9C-4A16-B722-FC3BDB0BB747}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4E0F040-4C9C-4A16-B722-FC3BDB0BB747}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AF12F58F-F243-4879-A679-138F96F39E01}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Text.Json;

namespace OpenSkyRestClient.Extensions
{
public static class JsonElementExtensions
{
public static string GetNullableString(this JsonElement element)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}

return element.GetString();
}

public static int? GetNullableInt(this JsonElement element)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}

if (!element.TryGetInt32(out int result))
{
return null;
}

return result;
}

public static float? GetNullableFloat(this JsonElement element)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}

if (!element.TryGetSingle(out float result))
{
return null;
}

return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;

namespace OpenSkyRestClient.Http.Builder
{
public class HttpRequestMessageBuilder
{
private class Header
{
public readonly string Name;
public readonly string Value;

public Header(string name, string value)
{
Name = name;
Value = value;
}
}

private string url;
private HttpMethod httpMethod;
private IDictionary<string, string> parameters;
private IList<Header> headers;
private IList<UrlSegment> segments;
private HttpContent content;

public HttpRequestMessageBuilder(string url, HttpMethod httpMethod)
{
if (url == null)
{
throw new ArgumentNullException("url");
}

this.url = url;
this.httpMethod = httpMethod;
this.headers = new List<Header>();
this.segments = new List<UrlSegment>();
this.content = null;
this.parameters = new Dictionary<string, string>();
}

public HttpRequestMessageBuilder HttpMethod(HttpMethod httpMethod)
{
this.httpMethod = httpMethod;

return this;
}

public HttpRequestMessageBuilder AddHeader(string name, string value)
{
this.headers.Add(new Header(name, value));

return this;
}

public HttpRequestMessageBuilder SetHeader(string name, string value)
{
var header = this.headers.FirstOrDefault(x => x.Name == name);

if (header != null)
{
this.headers.Remove(header);
}

AddHeader(name, value);

return this;
}

public HttpRequestMessageBuilder SetStringContent(string content, Encoding encoding, string mediaType)
{
this.content = new StringContent(content, encoding, mediaType);

return this;
}

public HttpRequestMessageBuilder SetHttpContent(HttpContent httpContent)
{
this.content = httpContent;

return this;
}

public HttpRequestMessageBuilder AddUrlSegment(string name, string value)
{
this.segments.Add(new UrlSegment(name, value));

return this;
}

public HttpRequestMessageBuilder AddQueryString(string key, string value)
{
this.parameters.Add(key, value);

return this;
}

public HttpRequestMessage Build()
{
string resourceUrl = HttpRequestUtils.ReplaceSegments(url, segments);
string queryString = HttpRequestUtils.BuildQueryString(resourceUrl, parameters);
string resourceUrlWithQueryString = string.Format("{0}{1}", resourceUrl, queryString);

HttpRequestMessage httpRequestMessage = new HttpRequestMessage(httpMethod, resourceUrlWithQueryString);

foreach (var header in headers)
{
httpRequestMessage.Headers.TryAddWithoutValidation(header.Name, header.Value);
}

if (content != null)
{
httpRequestMessage.Content = content;
}

return httpRequestMessage;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Text;

namespace OpenSkyRestClient.Http.Builder
{
public static class HttpRequestUtils
{
public static string ReplaceSegments(string resource, IList<UrlSegment> segments)
{
string url = new string(resource.ToCharArray());

foreach (var segment in segments)
{
url = url.Replace(segment.name, segment.value);
}

return url;
}

public static string BuildQueryString(string resource, IDictionary<string, string> parameters)
{
var builder = new StringBuilder();

bool first = true;
foreach (var parameter in parameters)
{
builder.Append(first ? "?" : "&");

first = false;

builder.Append(parameter.Key);
builder.Append("=");
builder.Append(parameter.Value);
}

return builder.ToString();
}
}
}
17 changes: 17 additions & 0 deletions OpenSkyRestClient/OpenSkyRestClient/Http/Builder/UrlSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace OpenSkyRestClient.Http.Builder
{
public class UrlSegment
{
public readonly string name;
public readonly string value;

public UrlSegment(string name, string value)
{
this.name = name;
this.value = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace OpenSkyRestClient.Http.Constants
{
public class HttpHeaderNames
{
public const string ContentType = "Content-Type";

public const string Authorization = "Authorization";

public const string RetryAfter = "Retry-After";
}
}
Loading

0 comments on commit 76aa317

Please sign in to comment.