Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create NetStandard 2.0 library #1510

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions csharp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<LangVersion>10</LangVersion>
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>bin\Debug/net6.0/</OutputPath>
<OutputPath>bin\Debug</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.extensibility.execution" Version="2.6.1" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="ini-parser-netstandard" Version="2.5.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LookerSdk\LookerSdk.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ public async Task GetHtmlUrlTest()
Assert.Contains(_config.HtmlTestContent, actual);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Looker.RTL;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
using Newtonsoft.Json;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -62,7 +61,7 @@ public async Task GetJsonUrlTest()
var content = actual.Body.ToString();
Assert.NotNull(content);
Assert.Contains("looker_release_version", content);
var json = JsonSerializer.Deserialize<Values>(content);
var json = JsonConvert.DeserializeObject<Values>(content);
Assert.Equal(json.Keys, _versionKeys);
}

Expand Down
14 changes: 14 additions & 0 deletions csharp/LookerSdk/LookerSdk.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>true</IsPackable>
<LangVersion>10</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>bin\Debug</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ini-parser-netstandard" Version="2.5.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using IniParser;

Expand Down Expand Up @@ -35,6 +36,7 @@ public class ApiSettings : IApiSettings
public string AgentTag { get; set; }
private string FileName { get; }
private string SectionName { get; }
public IDictionary<string, string> Headers { get; } = new Dictionary<string, string>();

public ApiSettings()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.VisualBasic.CompilerServices;
using Xunit.Sdk;

namespace Looker.RTL
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public struct Constants

public const string DefaultApiVersion = "4.0";
public const string AgentPrefix = "CS-SDK";
public const string LookerVersion = "24.18";
public const string LookerVersion = "24.20";

public const string Bearer = "Bearer";
public const string LookerAppiId = "x-looker-appid";
Expand Down
File renamed without changes.
101 changes: 51 additions & 50 deletions csharp/rtl/Transport.cs → csharp/LookerSdk/rtl/Transport.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Looker.RTL
{
Expand All @@ -19,16 +19,18 @@ namespace Looker.RTL
public interface ITransportSettings
{
/// base URL of API REST web service
string BaseUrl { get; set; }
string BaseUrl { get; }

/// whether to verify ssl certs or not. Defaults to true
bool VerifySsl { get; set; }
bool VerifySsl { get; }

/// request timeout in seconds. Default to 30
int Timeout { get; set; }
int Timeout { get; }

/// agent tag to use for the SDK requests
string AgentTag { get; set; }

IDictionary<string, string> Headers { get; }
}

/// <summary>
Expand Down Expand Up @@ -154,7 +156,7 @@ Task<SdkResponse<TSuccess, TError>> Request<TSuccess, TError>(
/// <summary>
/// HTPP request processor
/// </summary>
public class Transport : ITransport, ITransportSettings
public class Transport : ITransport
{
private readonly HttpClient _client;
private readonly ITransportSettings _settings;
Expand Down Expand Up @@ -188,7 +190,7 @@ public string MakeUrl(string path, Values queryParams = null, Authenticator auth
|| path.StartsWith("https:", StringComparison.InvariantCultureIgnoreCase))
return SdkUtils.AddQueryParams(path, queryParams);
// TODO I don't think authenticator is needed here any more?
return SdkUtils.AddQueryParams($"{BaseUrl}{path}", queryParams);
return SdkUtils.AddQueryParams($"{_settings.BaseUrl}{path}", queryParams);
}

private static RawResponse InitRawResponse(HttpResponseMessage response)
Expand Down Expand Up @@ -225,6 +227,26 @@ public async Task<IRawResponse> RawRequest(
var request = new HttpRequestMessage(method,
url);
request.Headers.Add(Constants.LookerAppiId, _settings.AgentTag);
foreach(var h in _settings.Headers)
{
if (request.Headers.Contains(h.Key))
{
request.Headers.Remove(h.Key);
}
request.Headers.Add(h.Key, h.Value);
}
if (options != null)
{
foreach (var h in options.Headers)
{
if (request.Headers.Contains(h.Key))
{
request.Headers.Remove(h.Key);
}
request.Headers.Add(h.Key, h.Value);
}
}

if (body != null)
{
if (body is string)
Expand All @@ -238,7 +260,7 @@ public async Task<IRawResponse> RawRequest(
{
request.Content =
new StringContent(
JsonSerializer.Serialize(body, new JsonSerializerOptions { IgnoreNullValues = true }),
JsonConvert.SerializeObject(body, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
Encoding.UTF8,
"application/json");
}
Expand All @@ -250,27 +272,30 @@ public async Task<IRawResponse> RawRequest(
try
{
response = await _client.SendAsync(request);
response.EnsureSuccessStatusCode();
result = InitRawResponse(response);
// if (response.IsSuccessStatusCode)
await using var stream = await response.Content.ReadAsStreamAsync();
// Simple content conversion here to make body easily readable in consumers
switch (SdkUtils.ResponseMode(result.ContentType))
using (var stream = await response.Content.ReadAsStreamAsync())
{
case ResponseMode.Binary:
result.Body = SdkUtils.StreamToByteArray(stream);
break;
case ResponseMode.String:
using (var sr = new StreamReader(stream))
{
result.Body = await sr.ReadToEndAsync();
}

break;
case ResponseMode.Unknown:
result.Body = SdkUtils.StreamToByteArray(stream);
break;
default:
throw new ArgumentOutOfRangeException($"Unrecognized Content Type {result.ContentType}");
// Simple content conversion here to make body easily readable in consumers
switch (SdkUtils.ResponseMode(result.ContentType))
{
case ResponseMode.Binary:
result.Body = SdkUtils.StreamToByteArray(stream);
break;
case ResponseMode.String:
using (var sr = new StreamReader(stream))
{
result.Body = await sr.ReadToEndAsync();
}

break;
case ResponseMode.Unknown:
result.Body = SdkUtils.StreamToByteArray(stream);
break;
default:
throw new ArgumentOutOfRangeException($"Unrecognized Content Type {result.ContentType}");
}
}
}
catch (Exception e)
Expand Down Expand Up @@ -328,29 +353,5 @@ public async Task<SdkResponse<TSuccess, TError>> Request<TSuccess, TError>(
var raw = await RawRequest(method, path, queryParams, body, authenticator, options);
return ParseResponse<TSuccess, TError>(raw);
}

public string BaseUrl
{
get => _settings.BaseUrl;
set => _settings.BaseUrl = value;
}

public bool VerifySsl
{
get => _settings.VerifySsl;
set => _settings.VerifySsl = value;
}

public int Timeout
{
get => _settings.Timeout;
set => _settings.Timeout = value;
}

public string AgentTag
{
get => _settings.AgentTag;
set => _settings.AgentTag = value;
}
}
}
File renamed without changes.
Loading