Skip to content

Commit

Permalink
feat(csharp) .Net 5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierapivideo committed Jun 12, 2024
1 parent 425728e commit b94f4ff
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 171 deletions.
4 changes: 3 additions & 1 deletion config/csharp.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
changelog:
- 1.3.1 (2024-02-19):
- 1.4.0 (2024-06-11):
- .net 5.0 support
- 1.3.1 (2024-02-19):
- Update VideoStatusIngest enum
- 1.3.0 (2023-06-28):
- Introducing new live streams restream feature
Expand Down
51 changes: 37 additions & 14 deletions templates/csharp/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ namespace {{packageName}}.Client
private const int DEFAULT_CHUNK_SIZE = {{ defaultChunkSize }};
private const int MIN_CHUNK_SIZE = {{ minChunkSize }};
private const int MAX_CHUNK_SIZE = {{ maxChunkSize }};
private int timeout = 60000;
private String basePath = "ws.api.video";
private Dictionary<string, string> originHeaders = new Dictionary<string, string>();


private readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
Expand All @@ -47,7 +51,7 @@ namespace {{packageName}}.Client
/// Allows for extending request processing for <see cref="ApiClient"/> generated code.
/// </summary>
/// <param name="request">The RestSharp request object</param>
private RestRequest InterceptRequest(IRestRequest request){
private RestRequest InterceptRequest(RestRequest request){
if(AuthManager != null)
return AuthManager.Intercept(request) as RestRequest;
return request as RestRequest;
Expand All @@ -58,7 +62,15 @@ namespace {{packageName}}.Client
/// Constructor for ApiClient with custom basePath
/// </summary>
/// <param name="basePath">the api base path.</param>
public ApiClient(string basePath): this(new RestClient(basePath)) {
public ApiClient(string basePath) {
var options = new RestClientOptions()
{
ThrowOnAnyError = true,
BaseUrl = new Uri(basePath)
};
RestClient client = new RestClient(options);

Initialize(client);
}

/// <summary>
Expand All @@ -75,7 +87,14 @@ namespace {{packageName}}.Client
/// Constructor for ApiClient with custom http client.
/// </summary>
/// <param name="client">a RestClient instance used to make API call</param>
public ApiClient(RestClient client) {
public ApiClient(IRestClient client) {
Initialize(client);
}


private void Initialize(IRestClient client)
{
this.RestClient = client;
setName("AV-Origin-Client", "csharp", "{{ artifactVersion }}");
}
Expand Down Expand Up @@ -121,7 +140,7 @@ namespace {{packageName}}.Client
throw new Exception("Invalid version value. The version should match the xxx[.yyy][.zzz] pattern.");
}

this.RestClient.AddDefaultHeader(key, name + ":" + version);
this.originHeaders.Add(key, name + ":" + version);
}

private bool isValidName(string name)
Expand All @@ -141,7 +160,7 @@ namespace {{packageName}}.Client
/// </summary>
/// <returns> Timeout in milliseconds</returns>
public int GetTimeout() {
return this.RestClient.Timeout;
return ((int)this.timeout);
}

/// <summary>
Expand All @@ -153,21 +172,21 @@ namespace {{packageName}}.Client
public ApiClient SetTimeout(int connectionTimeout) {
// set timeout
{{#netStandard}}RestClient.Timeout = TimeSpan.FromMilliseconds(connectionTimeout);{{/netStandard}}
{{^netStandard}}RestClient.Timeout = connectionTimeout;{{/netStandard}}
{{^netStandard}}this.timeout = connectionTimeout;{{/netStandard}}
return this;
}

/// <summary>
/// Gets or sets the RestClient.
/// </summary>
/// <value>An instance of the RestClient</value>
public RestClient RestClient { get; set; }
public IRestClient RestClient { get; set; }

/// <summary>
/// Sets the api base path.
/// </summary>
public void SetBasePath(string basePath) {
this.RestClient.BaseUrl = new Uri(basePath);
this.basePath = basePath;
}


Expand Down Expand Up @@ -196,6 +215,7 @@ namespace {{packageName}}.Client
string contentType)
{
var request = new RestRequest(path, method);
request.Timeout = new TimeSpan(0, 0, this.timeout);
{{#netStandard}}
// disable ResetSharp.Portable built-in serialization
request.Serializer = null;
Expand All @@ -209,6 +229,9 @@ namespace {{packageName}}.Client
foreach(var param in headerParams)
request.AddHeader(param.Key, param.Value);

foreach(var param in this.originHeaders)
request.AddHeader(param.Key, param.Value);

// add query parameter, if any
foreach(var param in queryParams)
request.AddQueryParameter(param.Key, param.Value);
Expand All @@ -225,7 +248,7 @@ namespace {{packageName}}.Client
{{/netStandard}}
{{^netStandard}}
{{^supportsUWP}}
request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
request.AddFile(param.Value.Name, param.Value.GetFile, param.Value.FileName, param.Value.ContentType);
{{/supportsUWP}}
{{#supportsUWP}}
byte[] paramWriter = null;
Expand Down Expand Up @@ -281,7 +304,7 @@ namespace {{packageName}}.Client
{{/supportsUWP}}
{{#supportsUWP}}
// Using async method to perform sync call (uwp-only)
var response = RestClient.ExecuteTaskAsync(request).Result;
var response = RestClient.ExecuteAsync(request).Result;
{{/supportsUWP}}
{{/netStandard}}
this.CheckResponse(response);
Expand All @@ -293,7 +316,7 @@ namespace {{packageName}}.Client
/// Check if the api call response was successful
/// </summary>
/// <param name="response">The response to check</param>
private void CheckResponse(IRestResponse response)
private void CheckResponse(RestResponse response)
{
if (response.ResponseStatus != ResponseStatus.Completed)
{
Expand Down Expand Up @@ -339,7 +362,7 @@ namespace {{packageName}}.Client
path, method, queryParams, postBody, headerParams, formParams, fileParams,
pathParams, contentType);
request = InterceptRequest(request);
var response = await RestClient.Execute{{^netStandard}}TaskAsync{{/netStandard}}(request, cancellationToken);
var response = await RestClient.Execute{{^netStandard}}Async{{/netStandard}}(request, cancellationToken);
return (Object)response;
}{{/supportsAsync}}

Expand Down Expand Up @@ -404,9 +427,9 @@ namespace {{packageName}}.Client
/// <param name="response">The HTTP response.</param>
/// <param name="type">Object type.</param>
/// <returns>Object representation of the JSON string.</returns>
public object Deserialize(IRestResponse response, Type type)
public object Deserialize(RestResponse response, Type type)
{
{{^netStandard}}IList<Parameter>{{/netStandard}}{{#netStandard}}IHttpHeaders{{/netStandard}} headers = response.Headers;
IReadOnlyCollection<Parameter> headers = response.Headers;
if (type == typeof(byte[])) // return byte array
{
return response.RawBytes;
Expand Down
6 changes: 3 additions & 3 deletions templates/csharp/ApiVideoClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace {{packageName}}.Client
/// Build an instance that targets the production environment using a custom OkHttp client
/// </summary>
/// <param name="client">the RestClient instance to use</param>
public ApiVideoClient(RestClient client) : this(new ApiClient(client))
public ApiVideoClient(IRestClient client) : this(new ApiClient(client))
{
}

Expand Down Expand Up @@ -107,7 +107,7 @@ namespace {{packageName}}.Client
/// <returns>The rest client timeout</returns>
public int GetTimeout()
{
return this.apiClient.RestClient.Timeout;
return this.apiClient.GetTimeout();
}

/// <summary>
Expand All @@ -116,7 +116,7 @@ namespace {{packageName}}.Client
/// <param name="newTimeOut">the new timeout</param>
public void SetTimeout(int newTimeOut)
{
this.apiClient.RestClient.Timeout = newTimeOut;
this.apiClient.SetTimeout(newTimeOut);
}

/// <summary>
Expand Down
22 changes: 0 additions & 22 deletions templates/csharp/AssemblyInfo.mustache
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("{{packageTitle}}")]
[assembly: AssemblyDescription("{{packageDescription}}")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("{{packageCompany}}")]
[assembly: AssemblyProduct("{{packageProductName}}")]
[assembly: AssemblyCopyright("{{packageCopyright}}")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("{{packageVersion}}")]
[assembly: AssemblyFileVersion("{{packageVersion}}")]
2 changes: 1 addition & 1 deletion templates/csharp/AuthenticationManager.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace {{packageName}}.Client
/// </summary>
/// <param name="request">The request to update</param>
/// <returns>The request updated</returns>
public IRestRequest Intercept(IRestRequest request){
public RestRequest Intercept(RestRequest request){
if(request.Resource.Equals("/auth/api-key")) {
return request;
Expand Down
110 changes: 17 additions & 93 deletions templates/csharp/Project.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
{{#appName}}
{{{appName}}}
Expand All @@ -11,32 +10,11 @@
{{#version}}The version of the OpenAPI document: {{version}}{{/version}}
{{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
-->
<Project ToolsVersion="{{^netStandard}}12.0{{/netStandard}}{{#netStandard}}14.0{{/netStandard}}" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
{{#netStandard}}<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>{{/netStandard}}
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{{packageGuid}}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>{{packageName}}</RootNamespace>
<AssemblyName>{{packageName}}</AssemblyName>
{{#netStandard}}
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkVersion>{{targetFramework}}</TargetFrameworkVersion>
{{/netStandard}}
{{^netStandard}}
{{^supportsUWP}}
<TargetFrameworkVersion>{{targetFramework}}</TargetFrameworkVersion>
{{/supportsUWP}}
{{#supportsUWP}}
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion>10.0.10240.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.10240.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
{{/supportsUWP}}
{{/netStandard}}
<FileAlignment>512</FileAlignment>
<PackOnBuild>true</PackOnBuild>
<PackageId>{{packageName}}</PackageId>
<PackageVersion>{{artifactVersion}}</PackageVersion>
Expand All @@ -45,89 +23,35 @@
<DevelopmentDependency>true</DevelopmentDependency>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>{{artifactUrl}}</PackageProjectUrl>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AssemblyTitle>api.video API client</AssemblyTitle>
<Company>api.video</Company>
<Product>ApiVideoApiCLient</Product>
<Copyright>No Copyright</Copyright>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\{{packageName}}.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\{{packageName}}.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<EnableDefaultItems>true</EnableDefaultItems>
</PropertyGroup>
<ItemGroup>
{{^netStandard}}
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml" />
<Reference Include="Newtonsoft.Json">
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\Newtonsoft.Json.{{newtonsoft-json.version}}\lib\{{newtonsoft-json.targetFramework}}\Newtonsoft.Json.dll</HintPath>
<HintPath Condition="Exists('..\packages')">..\packages\Newtonsoft.Json.{{newtonsoft-json.version}}\lib\{{newtonsoft-json.targetFramework}}\Newtonsoft.Json.dll</HintPath>
<HintPath Condition="Exists('..\..\packages')">..\..\packages\Newtonsoft.Json.{{newtonsoft-json.version}}\lib\{{newtonsoft-json.targetFramework}}\Newtonsoft.Json.dll</HintPath>
<HintPath Condition="Exists('..\..\vendor')">..\..\vendor\Newtonsoft.Json.{{newtonsoft-json.version}}\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="JsonSubTypes">
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\JsonSubTypes.{{json-subtypes.version}}\lib\{{json-subtypes.targetFramework}}\JsonSubTypes.dll</HintPath>
<HintPath Condition="Exists('..\packages')">..\packages\JsonSubTypes.{{json-subtypes.version}}\lib\{{json-subtypes.targetFramework}}\JsonSubTypes.dll</HintPath>
<HintPath Condition="Exists('..\..\packages')">..\..\packages\JsonSubTypes.{{json-subtypes.version}}\lib\{{json-subtypes.targetFramework}}\JsonSubTypes.dll</HintPath>
<HintPath Condition="Exists('..\..\vendor')">..\..\vendor\JsonSubTypes.{{json-subtypes.version}}\lib\{{json-subtypes.targetFramework}}\JsonSubTypes.dll</HintPath>
</Reference>
<Reference Include="RestSharp">
<HintPath Condition="Exists('$(SolutionDir)\packages')">$(SolutionDir)\packages\RestSharp.{{restsharp.version}}\lib\{{restsharp.targetFramework}}\RestSharp.dll</HintPath>
<HintPath Condition="Exists('..\packages')">..\packages\RestSharp.{{restsharp.version}}\lib\{{restsharp.targetFramework}}\RestSharp.dll</HintPath>
<HintPath Condition="Exists('..\..\packages')">..\..\packages\RestSharp.{{restsharp.version}}\lib\{{restsharp.targetFramework}}\RestSharp.dll</HintPath>
<HintPath Condition="Exists('..\..\vendor')">..\..\vendor\RestSharp.{{restsharp.version}}\lib\{{restsharp.targetFramework}}\RestSharp.dll</HintPath>
</Reference>
{{#generatePropertyChanged}}
<Reference Include="PropertyChanged">
<HintPath>..\..\packages\PropertyChanged.Fody.{{propertychanged-fody.version}}\Lib\portable-net4+sl4+wp8+win8+wpa81+MonoAndroid16+MonoTouch40\PropertyChanged.dll</HintPath>
</Reference>
{{/generatePropertyChanged}}
{{/netStandard}}
{{#netStandard}}
<!-- A reference to the entire .NET Framework is automatically included -->
<None Include="project.json" />
{{/netStandard}}
</ItemGroup>
<ItemGroup>
<Compile Include="**\*.cs"
Exclude="obj\**" />
</ItemGroup>
{{^netStandard}}
<ItemGroup>
<PackageReference Include="RestSharp" Version="105.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="RestSharp" Version="111.2.0" />
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
<PackageReference Include="NuGet.Build.Tasks.Pack">
<Version>5.9.1</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<Import Project="$(MsBuildToolsPath)\Microsoft.CSharp.targets" />
{{#generatePropertyChanged}}
<Import Project="..\..\packages\Fody.{{fody.version}}\build\portable-net+sl+win+wpa+wp\Fody.targets" Condition="Exists('..\..\packages\Fody.{{fody.version}}\build\portable-net+sl+win+wpa+wp\Fody.targets')" />
{{/generatePropertyChanged}}
{{/netStandard}}
{{#netStandard}}
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
{{/netStandard}}
</Project>

Loading

0 comments on commit b94f4ff

Please sign in to comment.