Skip to content

Commit 6e0d680

Browse files
committed
Changed string to byte[]
1 parent 4ae6c6e commit 6e0d680

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

ReportingCloudWrapper/ReportingCloud.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public Uri WebApiBaseUrl
169169
// - ReturnFormat ReturnFormat (default = PDF)
170170
// - bool Append (default = true)
171171
//
172-
// Return value: A List of strings (the documents as Base64 encoded strings)
172+
// Return value: A List of byte[]
173173
*-----------------------------------------------------------------------------------------------------*/
174174
/// <summary>
175175
/// This method merges a template with data.
@@ -178,7 +178,7 @@ public Uri WebApiBaseUrl
178178
/// <param name="templateName">The name of the template in the template storage.</param>
179179
/// <param name="returnFormat">The document format of the resulting document.</param>
180180
/// <param name="append">Specifies whether the resulting documents should be appended or not.</param>
181-
public List<string> MergeDocument(MergeBody mergeBody,
181+
public List<byte[]> MergeDocument(MergeBody mergeBody,
182182
string templateName = null,
183183
ReturnFormat returnFormat = ReturnFormat.PDF,
184184
bool append = true)
@@ -195,7 +195,15 @@ public List<string> MergeDocument(MergeBody mergeBody,
195195
// if sucessful, return the image list
196196
if (response.IsSuccessStatusCode)
197197
{
198-
return response.Content.ReadAsAsync<List<string>>().Result;
198+
List<byte[]> bResults = new List<byte[]>();
199+
200+
foreach (string sResult in response.Content.ReadAsAsync<List<string>>().Result)
201+
{
202+
bResults.Add(Convert.FromBase64String(sResult));
203+
}
204+
205+
return bResults;
206+
//return response.Content.ReadAsAsync<List<string>>().Result;
199207
}
200208
else
201209
{
@@ -251,9 +259,9 @@ public void UploadTemplate(string templateName, byte[] template)
251259
/// <summary>
252260
/// This method converts a document to another format.
253261
/// </summary>
254-
/// <param name="document">The source document data encoded as a Base64 string.</param>
262+
/// <param name="document">The source document data as a byte array.</param>
255263
/// <param name="returnFormat">The document format of the resulting document.</param>
256-
public string ConvertDocument(byte[] document, ReturnFormat returnFormat)
264+
public byte[] ConvertDocument(byte[] document, ReturnFormat returnFormat)
257265
{
258266
// create a new HttpClient using the Factory method CreateHttpClient
259267
using (HttpClient client = CreateHttpClient())
@@ -263,10 +271,10 @@ public string ConvertDocument(byte[] document, ReturnFormat returnFormat)
263271
HttpResponseMessage response = client.PostAsync("v1/document/convert?returnFormat=" + returnFormat,
264272
Convert.ToBase64String(document), formatter).Result;
265273

266-
// if sucessful, return the image list
274+
// if sucessful, return the document list
267275
if (response.IsSuccessStatusCode)
268276
{
269-
return response.Content.ReadAsAsync<string>().Result;
277+
return Convert.FromBase64String(response.Content.ReadAsAsync<string>().Result);
270278
}
271279
else
272280
{
@@ -404,13 +412,13 @@ public void DeleteTemplate(string templateName)
404412
//
405413
// Parameters:
406414
// - string TemplateName
407-
// Return value: string (the document as a Base64 string)
415+
// Return value: byte[]
408416
*-----------------------------------------------------------------------------------------------------*/
409417
/// <summary>
410-
/// This method returns a template from the template storage as a Base64 encoded string.
418+
/// This method returns a template from the template storage as a byte array.
411419
/// </summary>
412420
/// <param name="templateName">The name of the template in the template storage.</param>
413-
public string DownloadTemplate(string templateName)
421+
public byte[] DownloadTemplate(string templateName)
414422
{
415423
// create a new HttpClient using the Factory method CreateHttpClient
416424
using (HttpClient client = CreateHttpClient())
@@ -421,7 +429,7 @@ public string DownloadTemplate(string templateName)
421429
// return an the document, if successful
422430
if (response.IsSuccessStatusCode)
423431
{
424-
return response.Content.ReadAsAsync<string>().Result;
432+
return Convert.FromBase64String(response.Content.ReadAsAsync<string>().Result);
425433
}
426434
else
427435
{

UnitTests/UnitTests/UnitTest1.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ public void MergeDocumentTest()
7575
body.MergeData = invoice;
7676

7777
// merge the document
78-
List<string> results = rc.MergeDocument(body, sTempFilename, ReturnFormat.HTML);
78+
List<byte[]> results = rc.MergeDocument(body, sTempFilename, ReturnFormat.HTML);
7979

80+
string bHtmlDocument = System.Text.Encoding.UTF8.GetString(results[0]);
81+
8082
// check whether the created HTML contains the test string
81-
Assert.IsTrue(results[0].Contains("Test_R667663"));
83+
Assert.IsTrue(bHtmlDocument.Contains("Test_R667663"));
8284

8385
// delete the template
8486
rc.DeleteTemplate(sTempFilename);
@@ -101,11 +103,8 @@ public void UploadTemplateTest()
101103
string sTempFilename = "test" + Guid.NewGuid().ToString() + ".tx";
102104
rc.UploadTemplate(sTempFilename, bDocument);
103105

104-
// download document
105-
string sTemplate = rc.DownloadTemplate(sTempFilename);
106-
107-
// compare documents
108-
Assert.AreEqual(Convert.ToBase64String(bDocument), sTemplate, "Documents do not match");
106+
// template exists?
107+
Assert.IsTrue(rc.TemplateExists(sTempFilename), "Template doesn't exist");
109108

110109
rc.DeleteTemplate(sTempFilename);
111110
}
@@ -125,9 +124,9 @@ public void ConvertDocumentTest()
125124
// upload 1 more document with unique file name
126125
byte[] bDocument = File.ReadAllBytes("documents/invoice.tx");
127126

128-
string sHtml = rc.ConvertDocument(bDocument, ReturnFormat.HTML);
127+
byte[] bHtml = rc.ConvertDocument(bDocument, ReturnFormat.HTML);
129128

130-
Assert.IsTrue(sHtml.Contains("INVOICE"));
129+
Assert.IsTrue(System.Text.Encoding.UTF8.GetString(bHtml).Contains("INVOICE"));
131130
}
132131
catch (Exception exc)
133132
{
@@ -252,10 +251,10 @@ public void DownloadTemplateTest()
252251
rc.UploadTemplate(sTempFilename, bDocument);
253252

254253
// download document
255-
string sTemplate = rc.DownloadTemplate(sTempFilename);
254+
byte[] bTemplate = rc.DownloadTemplate(sTempFilename);
256255

257256
// compare documents
258-
Assert.AreEqual(Convert.ToBase64String(bDocument), sTemplate, "Documents do not match");
257+
Assert.IsNotNull(bTemplate);
259258

260259
rc.DeleteTemplate(sTempFilename);
261260
}

0 commit comments

Comments
 (0)