|
1 |
| -using PodioAPI.Exceptions; |
| 1 | +using Newtonsoft.Json; |
| 2 | +using PodioAPI.Exceptions; |
2 | 3 | using PodioAPI.Models;
|
3 | 4 | using PodioAPI.Services;
|
4 | 5 | using PodioAPI.Utils;
|
@@ -75,8 +76,11 @@ internal async Task<T> Get<T>(string url, Dictionary<string, string> requestData
|
75 | 76 | }
|
76 | 77 | else
|
77 | 78 | {
|
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 | + } |
80 | 84 | }
|
81 | 85 |
|
82 | 86 | return await Request<T>(request);
|
@@ -159,31 +163,44 @@ internal async Task<T> Get<T>(string url, Dictionary<string, string> requestData
|
159 | 163 | else
|
160 | 164 | {
|
161 | 165 | var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
162 |
| - var podioError = JSONSerializer.Deserialize<PodioError>(responseBody); |
163 |
| - |
164 |
| - if (response.StatusCode == HttpStatusCode.Unauthorized) |
| 166 | + try |
165 | 167 | {
|
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) |
168 | 171 | {
|
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 |
171 | 183 | {
|
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); |
174 | 186 | }
|
175 | 187 | }
|
176 | 188 | else
|
177 | 189 | {
|
178 |
| - OAuth = null; |
179 |
| - throw new PodioAuthorizationException((int)response.StatusCode, podioError); |
180 |
| - } |
| 190 | + ProcessErrorResponse(response.StatusCode, podioError); |
| 191 | + } |
| 192 | + |
181 | 193 | }
|
182 |
| - else |
| 194 | + catch (JsonException ex) |
183 | 195 | {
|
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 | + }); |
185 | 202 | }
|
186 |
| - |
| 203 | + |
187 | 204 | return default(T);
|
188 | 205 | }
|
189 | 206 | }
|
@@ -215,6 +232,7 @@ private HttpRequestMessage CreateHttpRequest(string url, HttpMethod httpMethod,
|
215 | 232 | private void ProcessErrorResponse(HttpStatusCode statusCode, PodioError podioError)
|
216 | 233 | {
|
217 | 234 | var status = (int)statusCode;
|
| 235 | + |
218 | 236 | switch (status)
|
219 | 237 | {
|
220 | 238 | case 400:
|
|
0 commit comments