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

chore: update to netstandard2.0 #2

Merged
merged 4 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 36 additions & 34 deletions Typeform/Client.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
using JorgeSerrano.Json;
using Refit;

namespace Typeform;

public class TypeformClient
namespace Typeform
{
/// <summary>
/// Note: Doesn't make sense for consumers to override naming policy!
/// </summary>
/// <value></value>
public static RefitSettings DefaultSettings => new RefitSettings

public class TypeformClient
{
ContentSerializer = new SystemTextJsonContentSerializer(new System.Text.Json.JsonSerializerOptions()
/// <summary>
/// Note: Doesn't make sense for consumers to override naming policy!
/// </summary>
/// <value></value>
public static RefitSettings DefaultSettings => new RefitSettings
{
PropertyNamingPolicy = new JsonSnakeCaseNamingPolicy()
})
};
ContentSerializer = new SystemTextJsonContentSerializer(new System.Text.Json.JsonSerializerOptions()
{
PropertyNamingPolicy = new JsonSnakeCaseNamingPolicy()
})
};

public static ITypeformApi CreateApi()
{
return CreateApi(DefaultSettings);
}
public static ITypeformApi CreateApi()
{
return CreateApi(DefaultSettings);
}

public static ITypeformApi CreateApi(RefitSettings settings)
{
return CreateApi("https://api.typeform.com", settings);
}
public static ITypeformApi CreateApi(RefitSettings settings)
{
return CreateApi("https://api.typeform.com", settings);
}

public static ITypeformApi CreateApi(string baseUrl)
{
return CreateApi(baseUrl, DefaultSettings);
}
public static ITypeformApi CreateApi(string baseUrl)
{
return CreateApi(baseUrl, DefaultSettings);
}

public static ITypeformApi CreateApi(string baseUrl, RefitSettings settings)
{
// TODO: Ensure naming policy is set correctly
public static ITypeformApi CreateApi(string baseUrl, RefitSettings settings)
{
// TODO: Ensure naming policy is set correctly

var typeformApi = RestService.For<ITypeformApi>(
baseUrl,
settings
);
var typeformApi = RestService.For<ITypeformApi>(
baseUrl,
settings
);

return typeformApi;
return typeformApi;
}
}
}
}
1 change: 0 additions & 1 deletion Typeform/Globals.cs

This file was deleted.

80 changes: 80 additions & 0 deletions Typeform/JsonSnakeCaseNamingPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Text;
using System.Text.Json;

public class JsonSnakeCaseNamingPolicy : JsonNamingPolicy
{
private readonly string _separator = "_";

public override string ConvertName(string name)
{
if (String.IsNullOrEmpty(name) || String.IsNullOrWhiteSpace(name)) return String.Empty;

ReadOnlySpan<char> spanName = name.Trim().ToCharArray();

var stringBuilder = new StringBuilder();
var addCharacter = true;

var isPreviousSpace = false;
var isPreviousSeparator = false;
var isCurrentSpace = false;
var isNextLower = false;
var isNextUpper = false;
var isNextSpace = false;

for (int position = 0; position < spanName.Length; position++)
{
if (position != 0)
{
isCurrentSpace = spanName[position] == 32;
isPreviousSpace = spanName[position - 1] == 32;
isPreviousSeparator = spanName[position - 1] == 95;

if (position + 1 != spanName.Length)
{
isNextLower = spanName[position + 1] > 96 && spanName[position + 1] < 123;
isNextUpper = spanName[position + 1] > 64 && spanName[position + 1] < 91;
isNextSpace = spanName[position + 1] == 32;
}

if ((isCurrentSpace) &&
((isPreviousSpace) ||
(isPreviousSeparator) ||
(isNextUpper) ||
(isNextSpace)))
addCharacter = false;
else
{
var isCurrentUpper = spanName[position] > 64 && spanName[position] < 91;
var isPreviousLower = spanName[position - 1] > 96 && spanName[position - 1] < 123;
var isPreviousNumber = spanName[position - 1] > 47 && spanName[position - 1] < 58;

if ((isCurrentUpper) &&
((isPreviousLower) ||
(isPreviousNumber) ||
(isNextLower) ||
(isNextSpace) ||
(isNextLower && !isPreviousSpace)))
stringBuilder.Append(_separator);
else
{
if ((isCurrentSpace &&
!isPreviousSpace &&
!isNextSpace))
{
stringBuilder.Append(_separator);
addCharacter = false;
}
}
}
}

if (addCharacter)
stringBuilder.Append(spanName[position]);
else
addCharacter = true;
}

return stringBuilder.ToString().ToLower();
}
}
5 changes: 1 addition & 4 deletions Typeform/Typeform.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JorgeSerrano.Json.JsonSnakeCaseNamingPolicy" Version="0.9.0" />
<PackageReference Include="Refit" Version="6.1.15" />
</ItemGroup>

Expand Down
72 changes: 39 additions & 33 deletions Typeform/TypeformApi.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
namespace Typeform;
using System.Collections.Generic;
using System.Threading.Tasks;
using Refit;

public interface ITypeformApi
namespace Typeform
{
/// <summary>
/// Typeform Responses API
/// </summary>
/// <param name="formId">Unique ID for the form. Find in your form URL. For example, in the URL "https://mysite.typeform.com/to/u6nXL7" the form_id is u6nXL7.</param>
/// <returns></returns>
[Get("/forms/{form_id}/responses")]
Task<TypeformResponsesContainer> GetFormResponsesAsync(
[Authorize("Bearer")] string accessToken,
[AliasAs("form_id")] string formId);

/// <summary>
/// Typeform Responses API
/// </summary>
/// <param name="formId">Unique ID for the form. Find in your form URL. For example, in the URL "https://mysite.typeform.com/to/u6nXL7" the form_id is u6nXL7.</param>
/// <param name="queryParams">Optional query parameters to pass to Responses endpoint</param>
/// <returns></returns>
[Get("/forms/{form_id}/responses")]
Task<TypeformResponsesContainer> GetFormResponsesAsync(
[Authorize("Bearer")] string accessToken,
[AliasAs("form_id")] string formId,
TypeformResponsesParameters queryParams);
public interface ITypeformApi
{
/// <summary>
/// Typeform Responses API
/// </summary>
/// <param name="formId">Unique ID for the form. Find in your form URL. For example, in the URL "https://mysite.typeform.com/to/u6nXL7" the form_id is u6nXL7.</param>
/// <returns></returns>
[Get("/forms/{form_id}/responses")]
Task<TypeformResponsesContainer> GetFormResponsesAsync(
[Authorize("Bearer")] string accessToken,
[AliasAs("form_id")] string formId);

/// <summary>
/// Typeform Responses API
/// </summary>
/// <param name="formId">Unique ID for the form. Find in your form URL. For example, in the URL "https://mysite.typeform.com/to/u6nXL7" the form_id is u6nXL7.</param>
/// <param name="queryParams">Optional query parameters to pass to Responses endpoint</param>
/// <returns></returns>
[Get("/forms/{form_id}/responses")]
Task<TypeformResponsesContainer> GetFormResponsesAsync(
[Authorize("Bearer")] string accessToken,
[AliasAs("form_id")] string formId,
TypeformResponsesParameters queryParams);
}
}

public class TypeformResponsesContainer
Expand Down Expand Up @@ -96,15 +102,15 @@ public class TypeformResponsesParameters
/// to the second, with T as a delimiter between the date and time (2020-03-20T14:00:59).
/// </summary>
/// <value></value>
public string? Since { get; set; }
public string Since { get; set; }

/// <summary>
/// Limit request to responses submitted until the specified date and time.
/// Could be passed as int (timestamp in seconds) or in ISO 8601 format, UTC time,
/// to the second, with T as a delimiter between the date and time (2020-03-20T14:00:59).
/// </summary>
/// <value></value>
public string? Until { get; set; }
public string Until { get; set; }

/// <summary>
/// Limit request to responses submitted after the specified token. Could
Expand All @@ -113,7 +119,7 @@ public class TypeformResponsesParameters
/// that you can traverse the complete set of responses without repeating entries.
/// </summary>
/// <value></value>
public string? After { get; set; }
public string After { get; set; }

/// <summary>
/// Limit request to responses submitted before the specified token. Could
Expand All @@ -122,28 +128,28 @@ public class TypeformResponsesParameters
/// you can traverse the complete set of responses without repeating entries.
/// </summary>
/// <value></value>
public string? Before { get; set; }
public string Before { get; set; }

/// <summary>
/// Limit request to the specified response_id values.
/// Use a comma-separated list to specify more than one response_id value.
/// </summary>
/// <value></value>
public string[]? IncludedResponseIds { get; set; }
public string[] IncludedResponseIds { get; set; }

/// <summary>
/// Comma-separated list of response_ids to be excluded from the response.
/// </summary>
/// <value></value>
public string[]? ExcludedResponseIds { get; set; }
public string[] ExcludedResponseIds { get; set; }

/// <summary>
/// Limit responses only to those which were submitted. This parameter
/// changes since/until filter, so if completed=true, it will filter by
/// submitted_at, otherwise - landed_at.
/// </summary>
/// <value></value>
public bool? Completed { get; set; }
public bool Completed { get; set; }

/// <summary>
/// Responses order in {fieldID},{asc|desc} format.
Expand All @@ -152,29 +158,29 @@ public class TypeformResponsesParameters
/// Default value is submitted_at,desc.
/// </summary>
/// <value></value>
public string? Sort { get; set; }
public string Sort { get; set; }

/// <summary>
/// Limit request to only responses that include the specified string.
/// The string will be escaped and it will be matched aganist all answers
/// fields, hidden fields and variables values.
/// </summary>
/// <value></value>
public string? Query { get; set; }
public string Query { get; set; }

/// <summary>
/// Show only specified fields in answers section. If response does not
/// have answers for specified fields, there will be null. Use a
/// comma-separated list to specify more than one field value.
/// </summary>
/// <value></value>
public string? Fields { get; set; }
public string Fields { get; set; }

/// <summary>
/// Limit request to only responses that include the specified fields in
/// answers section. Use a comma-separated list to specify more than
/// one field value - response will contain at least one of the specified fields.
/// </summary>
/// <value></value>
public string? AnsweredFields { get; set; }
public string AnsweredFields { get; set; }
}