Skip to content

Commit

Permalink
Merge pull request #52 from shefin/master
Browse files Browse the repository at this point in the history
Bug fixes and updates
  • Loading branch information
PavloBasiuk authored Jul 9, 2018
2 parents 50d4b84 + 54a16b6 commit 6187c77
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 20 deletions.
13 changes: 12 additions & 1 deletion Source/Podio .NET/Exceptions/PodioException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,18 @@ public PodioUnavailableException(SerializationInfo info, StreamingContext contex
}
#endif
}

public class PodioInvalidJsonException : PodioException
{
public PodioInvalidJsonException(int status, PodioError error)
: base(status, error)
{
}
#if !NETSTANDARD1_3
public PodioInvalidJsonException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
#endif
}
/// <summary>
/// Represent the error response from API
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Source/Podio .NET/Models/Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace PodioAPI.Models
public class Task
{
[JsonProperty("task_id")]
public string TaskId { get; set; }
public int TaskId { get; set; }

[JsonProperty("status")]
public string Status { get; set; }
Expand Down
54 changes: 36 additions & 18 deletions Source/Podio .NET/Podio.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PodioAPI.Exceptions;
using Newtonsoft.Json;
using PodioAPI.Exceptions;
using PodioAPI.Models;
using PodioAPI.Services;
using PodioAPI.Utils;
Expand Down Expand Up @@ -75,8 +76,11 @@ internal async Task<T> Get<T>(string url, Dictionary<string, string> requestData
}
else
{
var jsonString = JSONSerializer.Serilaize(requestData);
request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
if (requestData != null)
{
var jsonString = JSONSerializer.Serilaize(requestData);
request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
}
}

return await Request<T>(request);
Expand Down Expand Up @@ -159,31 +163,44 @@ internal async Task<T> Get<T>(string url, Dictionary<string, string> requestData
else
{
var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var podioError = JSONSerializer.Deserialize<PodioError>(responseBody);

if (response.StatusCode == HttpStatusCode.Unauthorized)
try
{
// If RefreshToken exists, refresh the access token and try the request again
if (OAuth is PodioOAuth podioOAuth && !string.IsNullOrEmpty(podioOAuth.RefreshToken) && podioError.ErrorDescription == "expired_token" || podioError.Error == "invalid_token")
var podioError = JSONSerializer.Deserialize<PodioError>(responseBody);

if (response.StatusCode == HttpStatusCode.Unauthorized)
{
var authInfo = await RefreshAccessToken().ConfigureAwait(false);
if (authInfo != null && !string.IsNullOrEmpty(authInfo.AccessToken))
// If RefreshToken exists, refresh the access token and try the request again
if (OAuth is PodioOAuth podioOAuth && !string.IsNullOrEmpty(podioOAuth.RefreshToken) && podioError.ErrorDescription == "expired_token" || podioError.Error == "invalid_token")
{
var authInfo = await RefreshAccessToken().ConfigureAwait(false);
if (authInfo != null && !string.IsNullOrEmpty(authInfo.AccessToken))
{
requestCopy.Headers.Authorization = new AuthenticationHeaderValue("OAuth2", authInfo.AccessToken);
return await Request<T>(requestCopy, isFileDownload, returnAsString);
}
}
else
{
requestCopy.Headers.Authorization = new AuthenticationHeaderValue("OAuth2", authInfo.AccessToken);
return await Request<T>(requestCopy, isFileDownload, returnAsString);
OAuth = null;
throw new PodioAuthorizationException((int)response.StatusCode, podioError);
}
}
else
{
OAuth = null;
throw new PodioAuthorizationException((int)response.StatusCode, podioError);
}
ProcessErrorResponse(response.StatusCode, podioError);
}

}
else
catch (JsonException ex)
{
ProcessErrorResponse(response.StatusCode, podioError);
throw new PodioInvalidJsonException((int)response.StatusCode, new PodioError
{
Error = "Error response is not a valid Json string.",
ErrorDescription = ex.ToString(),
ErrorDetail = responseBody
});
}

return default(T);
}
}
Expand Down Expand Up @@ -215,6 +232,7 @@ private HttpRequestMessage CreateHttpRequest(string url, HttpMethod httpMethod,
private void ProcessErrorResponse(HttpStatusCode statusCode, PodioError podioError)
{
var status = (int)statusCode;

switch (status)
{
case 400:
Expand Down
16 changes: 16 additions & 0 deletions Source/Podio .NET/Services/FileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,21 @@ public async Task<FileResponse> DownloadFile(FileAttachment fileAttachment)
};
return await _podio.Get<FileResponse>(fileLink, new Dictionary<string, string>(), true);
}
/// <summary>
/// Add linked account file (like sharefile, google drive)
/// </summary>
/// <param name="linkedAccountId"></param>
/// <param name="externalFileId"></param>
/// <param name="preservePermissions"></param>
/// <returns></returns>
public async Task<FileAttachment> UploadLinkedAccountFile(int linkedAccountId, string externalFileId, bool preservePermissions = true)
{
var url = $"/file/linked_account/{linkedAccountId}/";
var request = new
{
external_file_id = externalFileId
};
return await _podio.Post<FileAttachment>(url, request);
}
}
}
38 changes: 38 additions & 0 deletions Source/Podio .NET/Utils/ItemFields/LocationItemField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,44 @@ public double? Longitude
this.Values.First()["lng"] = value;
}
}
public string StreetAddress
{
get
{
if (this.Values.Any())
{
return (string)this.Values.First["street_address"];
}

return null;
}
}

public string City
{
get
{
if (this.Values.Any())
{
return (string)this.Values.First["city"];
}

return null;
}
}

public string PostalCode
{
get
{
if (this.Values.Any())
{
return (string)this.Values.First["postal_code"];
}

return null;
}
}

public string Country
{
Expand Down

0 comments on commit 6187c77

Please sign in to comment.