Skip to content

Commit 6187c77

Browse files
authored
Merge pull request #52 from shefin/master
Bug fixes and updates
2 parents 50d4b84 + 54a16b6 commit 6187c77

File tree

5 files changed

+103
-20
lines changed

5 files changed

+103
-20
lines changed

Source/Podio .NET/Exceptions/PodioException.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,18 @@ public PodioUnavailableException(SerializationInfo info, StreamingContext contex
207207
}
208208
#endif
209209
}
210-
210+
public class PodioInvalidJsonException : PodioException
211+
{
212+
public PodioInvalidJsonException(int status, PodioError error)
213+
: base(status, error)
214+
{
215+
}
216+
#if !NETSTANDARD1_3
217+
public PodioInvalidJsonException(SerializationInfo info, StreamingContext context) : base(info, context)
218+
{
219+
}
220+
#endif
221+
}
211222
/// <summary>
212223
/// Represent the error response from API
213224
/// </summary>

Source/Podio .NET/Models/Task.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace PodioAPI.Models
77
public class Task
88
{
99
[JsonProperty("task_id")]
10-
public string TaskId { get; set; }
10+
public int TaskId { get; set; }
1111

1212
[JsonProperty("status")]
1313
public string Status { get; set; }

Source/Podio .NET/Podio.cs

+36-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using PodioAPI.Exceptions;
1+
using Newtonsoft.Json;
2+
using PodioAPI.Exceptions;
23
using PodioAPI.Models;
34
using PodioAPI.Services;
45
using PodioAPI.Utils;
@@ -75,8 +76,11 @@ internal async Task<T> Get<T>(string url, Dictionary<string, string> requestData
7576
}
7677
else
7778
{
78-
var jsonString = JSONSerializer.Serilaize(requestData);
79-
request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
79+
if (requestData != null)
80+
{
81+
var jsonString = JSONSerializer.Serilaize(requestData);
82+
request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
83+
}
8084
}
8185

8286
return await Request<T>(request);
@@ -159,31 +163,44 @@ internal async Task<T> Get<T>(string url, Dictionary<string, string> requestData
159163
else
160164
{
161165
var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
162-
var podioError = JSONSerializer.Deserialize<PodioError>(responseBody);
163-
164-
if (response.StatusCode == HttpStatusCode.Unauthorized)
166+
try
165167
{
166-
// If RefreshToken exists, refresh the access token and try the request again
167-
if (OAuth is PodioOAuth podioOAuth && !string.IsNullOrEmpty(podioOAuth.RefreshToken) && podioError.ErrorDescription == "expired_token" || podioError.Error == "invalid_token")
168+
var podioError = JSONSerializer.Deserialize<PodioError>(responseBody);
169+
170+
if (response.StatusCode == HttpStatusCode.Unauthorized)
168171
{
169-
var authInfo = await RefreshAccessToken().ConfigureAwait(false);
170-
if (authInfo != null && !string.IsNullOrEmpty(authInfo.AccessToken))
172+
// If RefreshToken exists, refresh the access token and try the request again
173+
if (OAuth is PodioOAuth podioOAuth && !string.IsNullOrEmpty(podioOAuth.RefreshToken) && podioError.ErrorDescription == "expired_token" || podioError.Error == "invalid_token")
174+
{
175+
var authInfo = await RefreshAccessToken().ConfigureAwait(false);
176+
if (authInfo != null && !string.IsNullOrEmpty(authInfo.AccessToken))
177+
{
178+
requestCopy.Headers.Authorization = new AuthenticationHeaderValue("OAuth2", authInfo.AccessToken);
179+
return await Request<T>(requestCopy, isFileDownload, returnAsString);
180+
}
181+
}
182+
else
171183
{
172-
requestCopy.Headers.Authorization = new AuthenticationHeaderValue("OAuth2", authInfo.AccessToken);
173-
return await Request<T>(requestCopy, isFileDownload, returnAsString);
184+
OAuth = null;
185+
throw new PodioAuthorizationException((int)response.StatusCode, podioError);
174186
}
175187
}
176188
else
177189
{
178-
OAuth = null;
179-
throw new PodioAuthorizationException((int)response.StatusCode, podioError);
180-
}
190+
ProcessErrorResponse(response.StatusCode, podioError);
191+
}
192+
181193
}
182-
else
194+
catch (JsonException ex)
183195
{
184-
ProcessErrorResponse(response.StatusCode, podioError);
196+
throw new PodioInvalidJsonException((int)response.StatusCode, new PodioError
197+
{
198+
Error = "Error response is not a valid Json string.",
199+
ErrorDescription = ex.ToString(),
200+
ErrorDetail = responseBody
201+
});
185202
}
186-
203+
187204
return default(T);
188205
}
189206
}
@@ -215,6 +232,7 @@ private HttpRequestMessage CreateHttpRequest(string url, HttpMethod httpMethod,
215232
private void ProcessErrorResponse(HttpStatusCode statusCode, PodioError podioError)
216233
{
217234
var status = (int)statusCode;
235+
218236
switch (status)
219237
{
220238
case 400:

Source/Podio .NET/Services/FileService.cs

+16
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,21 @@ public async Task<FileResponse> DownloadFile(FileAttachment fileAttachment)
318318
};
319319
return await _podio.Get<FileResponse>(fileLink, new Dictionary<string, string>(), true);
320320
}
321+
/// <summary>
322+
/// Add linked account file (like sharefile, google drive)
323+
/// </summary>
324+
/// <param name="linkedAccountId"></param>
325+
/// <param name="externalFileId"></param>
326+
/// <param name="preservePermissions"></param>
327+
/// <returns></returns>
328+
public async Task<FileAttachment> UploadLinkedAccountFile(int linkedAccountId, string externalFileId, bool preservePermissions = true)
329+
{
330+
var url = $"/file/linked_account/{linkedAccountId}/";
331+
var request = new
332+
{
333+
external_file_id = externalFileId
334+
};
335+
return await _podio.Post<FileAttachment>(url, request);
336+
}
321337
}
322338
}

Source/Podio .NET/Utils/ItemFields/LocationItemField.cs

+38
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,44 @@ public double? Longitude
7777
this.Values.First()["lng"] = value;
7878
}
7979
}
80+
public string StreetAddress
81+
{
82+
get
83+
{
84+
if (this.Values.Any())
85+
{
86+
return (string)this.Values.First["street_address"];
87+
}
88+
89+
return null;
90+
}
91+
}
92+
93+
public string City
94+
{
95+
get
96+
{
97+
if (this.Values.Any())
98+
{
99+
return (string)this.Values.First["city"];
100+
}
101+
102+
return null;
103+
}
104+
}
105+
106+
public string PostalCode
107+
{
108+
get
109+
{
110+
if (this.Values.Any())
111+
{
112+
return (string)this.Values.First["postal_code"];
113+
}
114+
115+
return null;
116+
}
117+
}
80118

81119
public string Country
82120
{

0 commit comments

Comments
 (0)