Skip to content

Commit

Permalink
Fix exception when DownloadHandler is used
Browse files Browse the repository at this point in the history
  • Loading branch information
jdnichollsc committed Aug 26, 2018
1 parent 9a49ec6 commit 71a763d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 42 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ RestClient.Request(new RequestHelper {
BodyString = "Use it instead of 'Body' if you want to use other tool to serialize the JSON",
SimpleForm = new Dictionary<string, string> {}, //Content-Type: application/x-www-form-urlencoded
FormSections = new List<IMultipartFormSection>() {}, //Content-Type: multipart/form-data
DownloadHandler = new DownloadHandlerFile(destPah), //Download large files
ChunkedTransfer = true,
IgnoreHttpException = true //Prevent to catch http exceptions
}).Then(response => {
Expand Down
2 changes: 1 addition & 1 deletion src/Proyecto26.RestClient/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion ("2.1.0")]
[assembly: AssemblyVersion ("2.1.1")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
Expand Down
2 changes: 1 addition & 1 deletion src/Proyecto26.RestClient/Proyecto26.RestClient.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>Proyecto26.RestClient</id>
<version>2.1.0</version>
<version>2.1.1</version>
<title>RestClient for Unity</title>
<authors>Juan David Nicholls Cardona</authors>
<owners>jdnichollsc</owners>
Expand Down
11 changes: 2 additions & 9 deletions src/Proyecto26.RestClient/Utils/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static IEnumerator SendWebRequest(this UnityWebRequest request, RequestHe
{
request.chunkedTransfer = options.ChunkedTransfer.Value;
}
options.request = request;
options.Request = request;
yield return request.SendWebRequest();
}

Expand All @@ -76,14 +76,7 @@ public static IEnumerator SendWebRequest(this UnityWebRequest request, RequestHe
/// <param name="request">An UnityWebRequest object.</param>
public static ResponseHelper CreateWebResponse(this UnityWebRequest request)
{
return new ResponseHelper
{
StatusCode = request.responseCode,
Data = request.downloadHandler.data,
Text = request.downloadHandler.text,
Headers = request.GetResponseHeaders(),
Error = request.error
};
return new ResponseHelper(request);
}

public static bool IsValidRequest(this UnityWebRequest request, RequestHelper options)
Expand Down
25 changes: 16 additions & 9 deletions src/Proyecto26.RestClient/Utils/RequestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public float UploadProgress
get
{
float progress = 0;
if(this.request != null)
if(this.Request != null)
{
progress = this.request.uploadProgress;
progress = this.Request.uploadProgress;
}
return progress;
}
Expand All @@ -109,9 +109,9 @@ public float DownloadProgress
get
{
float progress = 0;
if (this.request != null)
if (this.Request != null)
{
progress = this.request.downloadProgress;
progress = this.Request.downloadProgress;
}
return progress;
}
Expand All @@ -120,7 +120,7 @@ public float DownloadProgress
/// <summary>
/// Internal use
/// </summary>
public UnityWebRequest request { private get; set; }
public UnityWebRequest Request { private get; set; }

/// <summary>
/// Get the value of a header
Expand All @@ -130,9 +130,9 @@ public float DownloadProgress
public string GetHeader(string name)
{
string headerValue;
if (request != null)
if (this.Request != null)
{
headerValue = request.GetRequestHeader(name);
headerValue = this.Request.GetRequestHeader(name);
}
else
{
Expand All @@ -145,9 +145,16 @@ public string GetHeader(string name)
/// Abort the request manually
/// </summary>
public void Abort() {
if (this.request != null)
if (this.Request != null)
{
this.request.Abort();
try
{
this.Request.Abort();
}
finally
{
this.Request = null;
}
}
}
}
Expand Down
58 changes: 36 additions & 22 deletions src/Proyecto26.RestClient/Utils/ResponseHelper.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,66 @@
using System;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Networking;

namespace Proyecto26
{
[Serializable]
public class ResponseHelper
{
private long _statusCode;
private UnityWebRequest request { get; set; }

public ResponseHelper(UnityWebRequest unityWebRequest)
{
request = unityWebRequest;
}

public long StatusCode
{
get { return _statusCode; }
set { _statusCode = value; }
get { return request.responseCode; }
}

private byte[] _data;
public byte[] Data
{
get { return _data; }
set { _data = value; }
get {
byte[] _data;
try
{
_data = request.downloadHandler.data;
}
catch (Exception)
{
_data = null;
}
return _data;
}
}

private string _text;
public string Text
{
get { return _text; }
set { _text = value; }
get
{
string _text;
try
{
_text = request.downloadHandler.text;
}
catch (Exception)
{
_text = string.Empty;
}
return _text;
}
}

private string _error;
public string Error
{
get { return _error; }
set { _error = value; }
get { return request.error; }
}

private Dictionary<string, string> _headers;
public Dictionary<string, string> Headers
{
get
{
if (_headers == null)
{
_headers = new Dictionary<string, string>();
}
return _headers;
}
set { _headers = value; }
get { return request.GetResponseHeaders(); }
}

public override string ToString()
Expand Down

0 comments on commit 71a763d

Please sign in to comment.