Skip to content

Commit 7408aaa

Browse files
committed
adding changes to support multipart form data content type
1 parent 60361e5 commit 7408aaa

File tree

3 files changed

+86
-13
lines changed

3 files changed

+86
-13
lines changed

Utilities/MultipartHelpers.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using RestSharp;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Text;
6+
7+
namespace CyberSource.Utilities
8+
{
9+
public static class MultipartHelpers
10+
{
11+
public static string[] BuildPostBodyForFiles(Dictionary<string, FileParameter> localVarFileParams)
12+
{
13+
if (localVarFileParams == null || localVarFileParams.Count == 0)
14+
{
15+
return null;
16+
}
17+
18+
Dictionary<string, string> localVarForFileNameAndContent = new Dictionary<string, string>();
19+
foreach(var fileParam in localVarFileParams)
20+
{
21+
string fileName = fileParam.Value.FileName;
22+
string fileContent = null;
23+
using (var reader = new StreamReader(fileParam.Value.GetFile()))
24+
{
25+
fileContent = reader.ReadToEnd();
26+
// fileContent now contains the content of the file as a string
27+
}
28+
localVarForFileNameAndContent.Add(fileName, fileContent);
29+
}
30+
31+
string boundary = Guid.NewGuid().ToString("N");
32+
string delimiter = "-------------" + boundary;
33+
string eol = "\r\n";
34+
StringBuilder data = new StringBuilder();
35+
36+
foreach (var param in localVarForFileNameAndContent)
37+
{
38+
string name = param.Key;
39+
string content = param.Value;
40+
41+
data.Append("--").Append(delimiter).Append(eol);
42+
data.Append("Content-Disposition: form-data; name=\"").Append(name).Append("\"; filename=\"").Append(name).Append("\"").Append(eol);
43+
data.Append("Content-Transfer-Encoding: binary").Append(eol);
44+
data.Append(eol);
45+
data.Append(content).Append(eol);
46+
}
47+
48+
data.Append("--").Append(delimiter).Append("--").Append(eol);
49+
50+
return new string[] { data.ToString(), delimiter };
51+
}
52+
53+
54+
}
55+
}

generator/cybersource-csharp-template/ApiClient.mustache

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,22 @@ namespace {{packageName}}.Client
226226
request.AddParameter(param.Key, param.Value);
227227

228228
// add file parameter, if any
229-
foreach(var param in fileParams)
230-
{
229+
//foreach(var param in fileParams)
230+
//{
231231
{{#netStandard}}
232-
request.AddFile(param.Value);
232+
//request.AddFile(param.Value);
233233
{{/netStandard}}
234234
{{^netStandard}}
235235
{{^supportsUWP}}
236-
request.AddFile(param.Value.Name, param.Value.FileName, param.Value.ContentType);
236+
//request.AddFile(param.Value.Name, param.Value.FileName, param.Value.ContentType);
237237
{{/supportsUWP}}
238238
{{#supportsUWP}}
239-
byte[] paramWriter = null;
240-
param.Value.Writer = delegate (Stream stream) { paramWriter = ToByteArray(stream); };
241-
request.AddFile(param.Value.Name, param.Value.FileName, param.Value.ContentType);
239+
//byte[] paramWriter = null;
240+
//param.Value.Writer = delegate (Stream stream) { paramWriter = ToByteArray(stream); };
241+
//request.AddFile(param.Value.Name, param.Value.FileName, param.Value.ContentType);
242242
{{/supportsUWP}}
243243
{{/netStandard}}
244-
}
244+
//}
245245

246246
if (postBody != null) // http body (model or byte[]) parameter
247247
{
@@ -260,6 +260,11 @@ namespace {{packageName}}.Client
260260
{{/netStandard}}
261261
}
262262
}
263+
else if (contentType.Contains("multipart/form-data"))
264+
{
265+
request.AddBody(postBody, "multipart/form-data");
266+
request.AddHeader("Content-Type", contentType); //required to set in case of file params
267+
}
263268
else
264269
{
265270
{{#netStandard}}
@@ -491,7 +496,7 @@ namespace {{packageName}}.Client
491496
RestClientOptions clientOptions = new RestClientOptions(RestClient.Options.BaseUrl)
492497
{
493498
Timeout = TimeSpan.FromMilliseconds(Configuration.Timeout),
494-
};
499+
};
495500

496501
// RestClient.ClearHandlers();
497502

@@ -563,7 +568,7 @@ namespace {{packageName}}.Client
563568
}
564569

565570
logger.Debug($"HTTP Response Headers :\n{logUtility.MaskSensitiveData(responseHeadersBuilder.ToString())}");
566-
571+
567572
if (!string.IsNullOrEmpty(httpResponseData))
568573
{
569574
logger.Debug($"HTTP Response Body :\n{logUtility.MaskSensitiveData(httpResponseData)}");
@@ -615,7 +620,7 @@ namespace {{packageName}}.Client
615620
};
616621

617622
logger.Debug($"HTTP Request Headers :\n{logUtility.MaskSensitiveData(headerPrintOutput.ToString())}");
618-
623+
619624
clientOptions.UserAgent = Configuration.UserAgent;
620625
RestClient = new RestClient(clientOptions);
621626

@@ -1014,6 +1019,7 @@ namespace {{packageName}}.Client
10141019

10151020
//Set the Configuration
10161021
Configuration.DefaultHeader = authenticationHeaders;
1022+
10171023
if (!string.IsNullOrWhiteSpace(merchantConfig.IntermediateHost))
10181024
{
10191025
//change with intermediate hostname if present

generator/cybersource-csharp-template/api.mustache

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ using NLog;
1616
using AuthenticationSdk.util;
1717
using CyberSource.Utilities.Tracking;
1818
using AuthenticationSdk.core;
19+
using CyberSource.Utilities;
1920

2021
namespace {{packageName}}.{{apiPackage}}
2122
{
@@ -324,7 +325,6 @@ namespace {{packageName}}.{{apiPackage}}
324325
{
325326
localVarPostBody = {{paramName}}; // byte array
326327
}
327-
328328
{{/bodyParam}}
329329
{{^bodyParam}}
330330
if (Method.{{httpMethod}} == Method.POST)
@@ -335,8 +335,14 @@ namespace {{packageName}}.{{apiPackage}}
335335
{
336336
localVarPostBody = null;
337337
}
338+
String[] filePostBodyAndDelimiter = MultipartHelpers.BuildPostBodyForFiles(localVarFileParams);
339+
if(null!= filePostBodyAndDelimiter)
340+
{
341+
localVarPostBody = filePostBodyAndDelimiter[0];
342+
localVarHttpContentType = "multipart/form-data; boundary=" + filePostBodyAndDelimiter[1];
343+
}
338344
{{/bodyParam}}
339-
345+
340346
bool isMLESupportedByCybsForApi = {{#vendorExtensions.x-devcenter-metaData.isMLEsupported}}true;{{/vendorExtensions.x-devcenter-metaData.isMLEsupported}}{{^vendorExtensions.x-devcenter-metaData.isMLEsupported}}false;{{/vendorExtensions.x-devcenter-metaData.isMLEsupported}}
341347
MerchantConfig merchantConfig = new MerchantConfig(Configuration.MerchantConfigDictionaryObj, Configuration.MapToControlMLEonAPI);
342348
if (MLEUtility.CheckIsMLEForAPI(merchantConfig, isMLESupportedByCybsForApi, "{{operationId}},{{operationId}}Async,{{operationId}}WithHttpInfo,{{operationId}}AsyncWithHttpInfo"))
@@ -535,6 +541,12 @@ namespace {{packageName}}.{{apiPackage}}
535541
{
536542
localVarPostBody = null;
537543
}
544+
String[] filePostBodyAndDelimiter = MultipartHelpers.BuildPostBodyForFiles(localVarFileParams);
545+
if(null!= filePostBodyAndDelimiter)
546+
{
547+
localVarPostBody = filePostBodyAndDelimiter[0];
548+
localVarHttpContentType = "multipart/form-data; boundary=" + filePostBodyAndDelimiter[1];
549+
}
538550
{{/bodyParam}}
539551

540552
bool isMLESupportedByCybsForApi = {{#vendorExtensions.x-devcenter-metaData.isMLEsupported}}true;{{/vendorExtensions.x-devcenter-metaData.isMLEsupported}}{{^vendorExtensions.x-devcenter-metaData.isMLEsupported}}false;{{/vendorExtensions.x-devcenter-metaData.isMLEsupported}}

0 commit comments

Comments
 (0)