Skip to content

Commit

Permalink
[csharp][httpclient] Introduce FileParameter as an abstraction (OpenA…
Browse files Browse the repository at this point in the history
…PITools#9426)

* Introduce FileParameter as an abstraction

* Update Samples

* Add new file to samples

* Fix hardcoded package name

* Fix hardcoded package name, import FileParameter in Model

* Remove unneccesary warning
  • Loading branch information
Blackclaws authored and Fornori committed May 26, 2021
1 parent 1a8438b commit 98de404
Show file tree
Hide file tree
Showing 88 changed files with 356 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,11 @@ public void processOpts() {
binRelativePath += "vendor";
additionalProperties.put("binRelativePath", binRelativePath);

if(HTTPCLIENT.equals(getLibrary())) {
supportingFiles.add(new SupportingFile("FileParameter.mustache", clientPackageDir, "FileParameter.cs"));
typeMapping.put("file", "FileParameter");
}

supportingFiles.add(new SupportingFile("IApiAccessor.mustache", clientPackageDir, "IApiAccessor.cs"));
supportingFiles.add(new SupportingFile("Configuration.mustache", clientPackageDir, "Configuration.cs"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", clientPackageDir, "ApiClient.cs"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,10 @@ namespace {{packageName}}.Client
{
foreach (var fileParam in options.FileParameters)
{
var fileStream = fileParam.Value as FileStream;
var fileStreamName = fileStream != null ? System.IO.Path.GetFileName(fileStream.Name) : null;
var content = new StreamContent(fileParam.Value);
var content = new StreamContent(fileParam.Value.Content);
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
multipartContent.Add(content, fileParam.Key,
fileStreamName ?? "no_file_name_provided");
fileParam.Value.Name);
}
}
return multipartContent;
Expand Down Expand Up @@ -370,11 +368,11 @@ namespace {{packageName}}.Client
{
if (options.Data != null)
{
if (options.Data is Stream s)
if (options.Data is FileParameter fp)
{
contentType = contentType ?? "application/octet-stream";
var streamContent = new StreamContent(s);
var streamContent = new StreamContent(fp.Content);
streamContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
request.Content = streamContent;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{{>partial_header}}

using System.IO;

namespace {{packageName}}.Client
{
/// <summary>
/// Represents a File passed to the API as a Parameter, allows using different backends for files
/// </summary>
public class FileParameter
{
/// <summary>
/// The filename
/// </summary>
public string Name { get; set; } = "no_name_provided";

/// <summary>
/// The content of the file
/// </summary>
public Stream Content { get; set; }

/// <summary>
/// Construct a FileParameter just from the contents, will extract the filename from a filestream
/// </summary>
/// <param name="content"> The file content </param>
public FileParameter(Stream content)
{
if (content is FileStream fs)
{
Name = fs.Name;
}
Content = content;
}

/// <summary>
/// Construct a FileParameter from name and content
/// </summary>
/// <param name="filename">The filename</param>
/// <param name="content">The file content</param>
public FileParameter(string filename, Stream content)
{
Name = filename;
Content = content;
}

/// <summary>
/// Implicit conversion of stream to file parameter. Useful for backwards compatibility.
/// </summary>
/// <param name="s">Stream to convert</param>
/// <returns>FileParameter</returns>
public static implicit operator FileParameter(Stream s) => new FileParameter(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{{>partial_header}}

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace {{packageName}}.Client
{
/// <summary>
/// A container for generalized request inputs. This type allows consumers to extend the request functionality
/// by abstracting away from the default (built-in) request framework (e.g. RestSharp).
/// </summary>
public class RequestOptions
{
/// <summary>
/// Parameters to be bound to path parts of the Request's URL
/// </summary>
public Dictionary<String, String> PathParameters { get; set; }
/// <summary>
/// Query parameters to be applied to the request.
/// Keys may have 1 or more values associated.
/// </summary>
public Multimap<String, String> QueryParameters { get; set; }
/// <summary>
/// Header parameters to be applied to to the request.
/// Keys may have 1 or more values associated.
/// </summary>
public Multimap<String, String> HeaderParameters { get; set; }
/// <summary>
/// Form parameters to be sent along with the request.
/// </summary>
public Dictionary<String, String> FormParameters { get; set; }
/// <summary>
/// File parameters to be sent along with the request.
/// </summary>
public Dictionary<String, FileParameter> FileParameters { get; set; }
/// <summary>
/// Cookies to be sent along with the request.
/// </summary>
public List<Cookie> Cookies { get; set; }
/// <summary>
/// Any data associated with a request body.
/// </summary>
public Object Data { get; set; }
/// <summary>
/// Constructs a new instance of <see cref="RequestOptions"/>
/// </summary>
public RequestOptions()
{
PathParameters = new Dictionary<string, string>();
QueryParameters = new Multimap<string, string>();
HeaderParameters = new Multimap<string, string>();
FormParameters = new Dictionary<string, string>();
FileParameters = new Dictionary<String, FileParameter>();
Cookies = new List<Cookie>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{{>partial_header}}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
{{#models}}
{{#model}}
{{#discriminator}}
using JsonSubTypes;
{{/discriminator}}
{{/model}}
{{/models}}
{{#validatable}}
using System.ComponentModel.DataAnnotations;
{{/validatable}}
using FileParameter = {{packageName}}.Client.FileParameter;
using OpenAPIDateConverter = {{packageName}}.Client.OpenAPIDateConverter;
{{#useCompareNetObjects}}
using OpenAPIClientUtils = {{packageName}}.Client.ClientUtils;
{{/useCompareNetObjects}}
{{#models}}
{{#model}}
{{#oneOf}}
{{#-first}}
using System.Reflection;
{{/-first}}
{{/oneOf}}
{{#aneOf}}
{{#-first}}
using System.Reflection;
{{/-first}}
{{/aneOf}}

namespace {{packageName}}.{{modelPackage}}
{
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{#anyOf}}{{#-first}}{{>modelAnyOf}}{{/-first}}{{/anyOf}}{{^oneOf}}{{^anyOf}}{{>modelGeneric}}{{/anyOf}}{{/oneOf}}{{/isEnum}}
{{/model}}
{{/models}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ src/Org.OpenAPITools/Client/ApiResponse.cs
src/Org.OpenAPITools/Client/ClientUtils.cs
src/Org.OpenAPITools/Client/Configuration.cs
src/Org.OpenAPITools/Client/ExceptionFactory.cs
src/Org.OpenAPITools/Client/FileParameter.cs
src/Org.OpenAPITools/Client/GlobalConfiguration.cs
src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
src/Org.OpenAPITools/Client/IApiAccessor.cs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ No authorization required

<a name="testendpointparameters"></a>
# **TestEndpointParameters**
> void TestEndpointParameters (decimal number, double _double, string patternWithoutDelimiter, byte[] _byte, int? integer = null, int? int32 = null, long? int64 = null, float? _float = null, string _string = null, System.IO.Stream binary = null, DateTime? date = null, DateTime? dateTime = null, string password = null, string callback = null)
> void TestEndpointParameters (decimal number, double _double, string patternWithoutDelimiter, byte[] _byte, int? integer = null, int? int32 = null, long? int64 = null, float? _float = null, string _string = null, FileParameter binary = null, DateTime? date = null, DateTime? dateTime = null, string password = null, string callback = null)
Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트

Expand Down Expand Up @@ -718,7 +718,7 @@ namespace Example
var int64 = 789; // long? | None (optional)
var _float = 3.4F; // float? | None (optional)
var _string = _string_example; // string | None (optional)
var binary = BINARY_DATA_HERE; // System.IO.Stream | None (optional)
var binary = BINARY_DATA_HERE; // FileParameter | None (optional)
var date = 2013-10-20; // DateTime? | None (optional)
var dateTime = 2013-10-20T19:20:30+01:00; // DateTime? | None (optional) (default to "2010-02-01T10:20:10.111110+01:00")
var password = password_example; // string | None (optional)
Expand Down Expand Up @@ -753,7 +753,7 @@ Name | Type | Description | Notes
**int64** | **long?**| None | [optional]
**_float** | **float?**| None | [optional]
**_string** | **string**| None | [optional]
**binary** | **System.IO.Stream****System.IO.Stream**| None | [optional]
**binary** | **FileParameter****FileParameter**| None | [optional]
**date** | **DateTime?**| None | [optional]
**dateTime** | **DateTime?**| None | [optional] [default to &quot;2010-02-01T10:20:10.111110+01:00&quot;]
**password** | **string**| None | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Name | Type | Description | Notes
**Decimal** | **decimal** | | [optional]
**String** | **string** | | [optional]
**Byte** | **byte[]** | |
**Binary** | **System.IO.Stream** | | [optional]
**Binary** | [**FileParameter**](FileParameter.md) | | [optional]
**Date** | **DateTime** | |
**DateTime** | **DateTime** | | [optional]
**Uuid** | **Guid** | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ void (empty response body)

<a name="uploadfile"></a>
# **UploadFile**
> ApiResponse UploadFile (long petId, string additionalMetadata = null, System.IO.Stream file = null)
> ApiResponse UploadFile (long petId, string additionalMetadata = null, FileParameter file = null)
uploads an image

Expand Down Expand Up @@ -595,7 +595,7 @@ namespace Example
var apiInstance = new PetApi(httpClient, config, httpClientHandler);
var petId = 789; // long | ID of pet to update
var additionalMetadata = additionalMetadata_example; // string | Additional data to pass to server (optional)
var file = BINARY_DATA_HERE; // System.IO.Stream | file to upload (optional)
var file = BINARY_DATA_HERE; // FileParameter | file to upload (optional)
try
{
Expand All @@ -620,7 +620,7 @@ Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**petId** | **long**| ID of pet to update |
**additionalMetadata** | **string**| Additional data to pass to server | [optional]
**file** | **System.IO.Stream****System.IO.Stream**| file to upload | [optional]
**file** | **FileParameter****FileParameter**| file to upload | [optional]

### Return type

Expand All @@ -645,7 +645,7 @@ Name | Type | Description | Notes

<a name="uploadfilewithrequiredfile"></a>
# **UploadFileWithRequiredFile**
> ApiResponse UploadFileWithRequiredFile (long petId, System.IO.Stream requiredFile, string additionalMetadata = null)
> ApiResponse UploadFileWithRequiredFile (long petId, FileParameter requiredFile, string additionalMetadata = null)
uploads an image (required)

Expand Down Expand Up @@ -674,7 +674,7 @@ namespace Example
HttpClientHandler httpClientHandler = new HttpClientHandler();
var apiInstance = new PetApi(httpClient, config, httpClientHandler);
var petId = 789; // long | ID of pet to update
var requiredFile = BINARY_DATA_HERE; // System.IO.Stream | file to upload
var requiredFile = BINARY_DATA_HERE; // FileParameter | file to upload
var additionalMetadata = additionalMetadata_example; // string | Additional data to pass to server (optional)
try
Expand All @@ -699,7 +699,7 @@ namespace Example
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**petId** | **long**| ID of pet to update |
**requiredFile** | **System.IO.Stream****System.IO.Stream**| file to upload |
**requiredFile** | **FileParameter****FileParameter**| file to upload |
**additionalMetadata** | **string**| Additional data to pass to server | [optional]

### Return type
Expand Down
Loading

0 comments on commit 98de404

Please sign in to comment.