diff --git a/FeedlySharp/FeedlyClient.Auth.cs b/FeedlySharp/Endpoints/Auth.cs similarity index 95% rename from FeedlySharp/FeedlyClient.Auth.cs rename to FeedlySharp/Endpoints/Auth.cs index 4567091..4337a40 100644 --- a/FeedlySharp/FeedlyClient.Auth.cs +++ b/FeedlySharp/Endpoints/Auth.cs @@ -55,7 +55,7 @@ namespace FeedlySharp { "client_secret", ClientSecret }, { "redirect_uri", RedirectUri }, { "grant_type", "authorization_code" } - }, cancellationToken); + }, false, false, cancellationToken); } @@ -67,7 +67,7 @@ namespace FeedlySharp { "client_id", ClientId }, { "client_secret", ClientSecret }, { "grant_type", "refresh_token" } - }, cancellationToken); + }, false, false, cancellationToken); } @@ -79,7 +79,7 @@ namespace FeedlySharp { "client_id", ClientId }, { "client_secret", ClientSecret }, { "grant_type", "revoke_token" } - }, cancellationToken); + }, false, false, cancellationToken); } } } diff --git a/FeedlySharp/FeedlyClient.Categories.cs b/FeedlySharp/Endpoints/Categories.cs similarity index 57% rename from FeedlySharp/FeedlyClient.Categories.cs rename to FeedlySharp/Endpoints/Categories.cs index 749b459..f213898 100644 --- a/FeedlySharp/FeedlyClient.Categories.cs +++ b/FeedlySharp/Endpoints/Categories.cs @@ -12,19 +12,19 @@ namespace FeedlySharp { public async Task> GetCategories(CancellationToken cancellationToken = default(CancellationToken)) { - return await Client.AuthRequest>(HttpMethod.Get, "v3/categories", null, cancellationToken); + return await Client.Request>(HttpMethod.Get, "v3/categories", null, false, true, cancellationToken); } public async Task RenameCategory(string id, string label, CancellationToken cancellationToken = default(CancellationToken)) { - await Client.AuthRequest(HttpMethod.Post, String.Format("v3/categories/{0}", ValueToResource("category", id)), new { label = label }, cancellationToken); + await Client.Request(HttpMethod.Post, String.Format("v3/categories/{0}", ValueToResource("category", id)), new { label = label }, true, true, cancellationToken); } public async Task DeleteCategory(string id, CancellationToken cancellationToken = default(CancellationToken)) { - await Client.AuthRequest(HttpMethod.Delete, String.Format("v3/categories/{0}", ValueToResource("category", id)), null, cancellationToken); + await Client.Request(HttpMethod.Delete, String.Format("v3/categories/{0}", ValueToResource("category", id)), null, false, true, cancellationToken); } } } diff --git a/FeedlySharp/Endpoints/OPML.cs b/FeedlySharp/Endpoints/OPML.cs new file mode 100644 index 0000000..3dbd443 --- /dev/null +++ b/FeedlySharp/Endpoints/OPML.cs @@ -0,0 +1,24 @@ +using FeedlySharp.Models; +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace FeedlySharp +{ + public partial class FeedlyClient + { + public async Task GetOPML(CancellationToken cancellationToken = default(CancellationToken)) + { + return await Client.Request(HttpMethod.Get, "v3/opml", null, false, true, cancellationToken); + } + + + public async Task ImportOPML(string opml, CancellationToken cancellationToken = default(CancellationToken)) + { + throw new NotImplementedException(); + } + } +} diff --git a/FeedlySharp/FeedlyClient.Preferences.cs b/FeedlySharp/Endpoints/Preferences.cs similarity index 64% rename from FeedlySharp/FeedlyClient.Preferences.cs rename to FeedlySharp/Endpoints/Preferences.cs index 20a729e..0ae75a3 100644 --- a/FeedlySharp/FeedlyClient.Preferences.cs +++ b/FeedlySharp/Endpoints/Preferences.cs @@ -12,13 +12,13 @@ namespace FeedlySharp { public async Task> GetPreferences(CancellationToken cancellationToken = default(CancellationToken)) { - return (await Client.AuthRequest>(HttpMethod.Get, "v3/preferences", null, cancellationToken)) ?? new Dictionary(); + return (await Client.Request>(HttpMethod.Get, "v3/preferences", null, false, true, cancellationToken)) ?? new Dictionary(); } public async Task> UpdatePreferences(Dictionary preferences, CancellationToken cancellationToken = default(CancellationToken)) { - return await Client.AuthRequest>(HttpMethod.Post, "v3/preferences", preferences, true, cancellationToken); + return await Client.Request>(HttpMethod.Post, "v3/preferences", preferences, true, true, cancellationToken); } } } diff --git a/FeedlySharp/FeedlyClient.Tags.cs b/FeedlySharp/Endpoints/Tags.cs similarity index 65% rename from FeedlySharp/FeedlyClient.Tags.cs rename to FeedlySharp/Endpoints/Tags.cs index ce7b6a3..6f97aef 100644 --- a/FeedlySharp/FeedlyClient.Tags.cs +++ b/FeedlySharp/Endpoints/Tags.cs @@ -13,14 +13,14 @@ namespace FeedlySharp { public async Task> GetTags(CancellationToken cancellationToken = default(CancellationToken)) { - return await Client.AuthRequest>(HttpMethod.Get, "v3/tags", null, cancellationToken); + return await Client.Request>(HttpMethod.Get, "v3/tags", null, false, true, cancellationToken); } public async Task UpdateTags(string entryId, string[] tags, CancellationToken cancellationToken = default(CancellationToken)) { string tagsString = String.Join(",", tags.Select(x => ValueToResource("tag", x))); - await Client.AuthRequest(HttpMethod.Put, String.Format("v3/tags/{0}", tagsString), new { entryId = entryId }, cancellationToken); + await Client.Request(HttpMethod.Put, String.Format("v3/tags/{0}", tagsString), new { entryId = entryId }, true, true, cancellationToken); return true; } @@ -28,14 +28,14 @@ namespace FeedlySharp public async Task UpdateTags(string[] entryIds, string[] tags, CancellationToken cancellationToken = default(CancellationToken)) { string tagsString = String.Join(",", tags.Select(x => ValueToResource("tag", x))); - await Client.AuthRequest(HttpMethod.Put, String.Format("v3/tags/{0}", tagsString), new { entryIds = entryIds }, cancellationToken); + await Client.Request(HttpMethod.Put, String.Format("v3/tags/{0}", tagsString), new { entryIds = entryIds }, true, true, cancellationToken); return true; } public async Task RenameTag(string oldTag, string newTag, CancellationToken cancellationToken = default(CancellationToken)) { - await Client.AuthRequest(HttpMethod.Post, String.Format("v3/tags/{0}", ValueToResource("tag", oldTag)), new { label = newTag }, cancellationToken); + await Client.Request(HttpMethod.Post, String.Format("v3/tags/{0}", ValueToResource("tag", oldTag)), new { label = newTag }, true, true, cancellationToken); return true; } @@ -44,7 +44,7 @@ namespace FeedlySharp { string tagsString = String.Join(",", tags.Select(x => ValueToResource("tag", x))); string entryIdsString = String.Join(",", entryIds.Select(x => WebUtility.UrlEncode(x))); - await Client.AuthRequest(HttpMethod.Delete, String.Format("v3/tags/{0}/{1}", tagsString, entryIdsString), null, cancellationToken); + await Client.Request(HttpMethod.Delete, String.Format("v3/tags/{0}/{1}", tagsString, entryIdsString), null, false, true, cancellationToken); return true; } @@ -52,7 +52,7 @@ namespace FeedlySharp public async Task RemoveTags(string[] tags, CancellationToken cancellationToken = default(CancellationToken)) { string tagsString = String.Join(",", tags.Select(x => ValueToResource("tag", x))); - await Client.AuthRequest(HttpMethod.Delete, String.Format("v3/tags/{0}", tagsString), null, cancellationToken); + await Client.Request(HttpMethod.Delete, String.Format("v3/tags/{0}", tagsString), null, false, true, cancellationToken); return true; } } diff --git a/FeedlySharp/Endpoints/User.cs b/FeedlySharp/Endpoints/User.cs new file mode 100644 index 0000000..8bdf371 --- /dev/null +++ b/FeedlySharp/Endpoints/User.cs @@ -0,0 +1,24 @@ +using FeedlySharp.Models; +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace FeedlySharp +{ + public partial class FeedlyClient + { + public async Task GetUser(CancellationToken cancellationToken = default(CancellationToken)) + { + return await Client.Request(HttpMethod.Get, "v3/profile", null, false, true, cancellationToken); + } + + + public async Task UpdateUser(Dictionary parameters, CancellationToken cancellationToken = default(CancellationToken)) + { + return await Client.Request(HttpMethod.Post, "v3/profile", parameters, true, true, cancellationToken); + } + } +} diff --git a/FeedlySharp/FeedlyClient.User.cs b/FeedlySharp/FeedlyClient.User.cs deleted file mode 100644 index 61acc19..0000000 --- a/FeedlySharp/FeedlyClient.User.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FeedlySharp.Models; -using System; -using System.Collections.Generic; -using System.Net; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; - -namespace FeedlySharp -{ - public partial class FeedlyClient - { - public async Task GetUser(CancellationToken cancellationToken = default(CancellationToken)) - { - return await Client.AuthRequest(HttpMethod.Get, "v3/profile", null, cancellationToken); - } - - - public async Task UpdateUser(dynamic parameters, CancellationToken cancellationToken = default(CancellationToken)) - { - return await Client.AuthRequest(HttpMethod.Post, "v3/profile", parameters, cancellationToken); - } - } -} diff --git a/FeedlySharp/FeedlyHttpClient.cs b/FeedlySharp/FeedlyHttpClient.cs index 208f8ed..22ef2b7 100644 --- a/FeedlySharp/FeedlyHttpClient.cs +++ b/FeedlySharp/FeedlyHttpClient.cs @@ -25,85 +25,59 @@ namespace FeedlySharp } - public async Task AuthRequest(HttpMethod method, string requestUri, Dictionary parameters = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class, new() - { - if (String.IsNullOrEmpty(AccessToken)) - { - throw new FeedlySharpException("This request requires an access token."); - } - - return await Request(method, requestUri, parameters, null, false, cancellationToken, new Dictionary() - { - { "Authorization", String.Format("OAuth {0}", AccessToken) } - }); - } - - - public async Task AuthRequest(HttpMethod method, string requestUri, Dictionary parameters = null, bool bodyAsJson = false, CancellationToken cancellationToken = default(CancellationToken)) where T : class, new() - { - if (String.IsNullOrEmpty(AccessToken)) - { - throw new FeedlySharpException("This request requires an access token."); - } - - return await Request(method, requestUri, parameters, null, bodyAsJson, cancellationToken, new Dictionary() - { - { "Authorization", String.Format("OAuth {0}", AccessToken) } - }); - } - - - public async Task AuthRequest(HttpMethod method, string requestUri, dynamic body = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class, new() - { - if (String.IsNullOrEmpty(AccessToken)) - { - throw new FeedlySharpException("This request requires an access token."); - } - - return await Request(method, requestUri, null, body, true, cancellationToken, new Dictionary() - { - { "Authorization", String.Format("OAuth {0}", AccessToken) } - }); - } - - public async Task Request( HttpMethod method, string requestUri, - Dictionary parameters = null, - dynamic body = null, + object body = null, bool bodyAsJson = false, - CancellationToken cancellationToken = default(CancellationToken), - Dictionary headers = null + bool isOauth = true, + CancellationToken cancellationToken = default(CancellationToken) ) where T : class, new() + { + string responseString = await Request(method, requestUri, body, bodyAsJson, isOauth, cancellationToken); + + if (responseString == "[]") + { + return new T(); + } + if ((new string[] { "", "{}" }).Contains(responseString)) + { + return null; + } + + return DeserializeJson(responseString); + } + + + public async Task Request( + HttpMethod method, + string requestUri, + object body = null, + bool bodyAsJson = false, + bool isOauth = true, + CancellationToken cancellationToken = default(CancellationToken) + ) { HttpRequestMessage request = new HttpRequestMessage(method, requestUri); HttpResponseMessage response = null; string responseString = null; // content of the request - if (parameters != null && !bodyAsJson) + if (body != null && !bodyAsJson) { - request.Content = new FormUrlEncodedContent(parameters); + request.Content = new FormUrlEncodedContent(body as Dictionary); } - else if (parameters != null) - { - request.Content = new StringContent(JsonConvert.SerializeObject(parameters)); - } - // additional headers - if (headers != null) - { - foreach (KeyValuePair header in headers) - { - request.Headers.Add(header.Key, header.Value); - } - } - // body - if (body != null) + else if (body != null) { request.Content = new StringContent(JsonConvert.SerializeObject(body)); } + // OAuth header + if (isOauth) + { + request.Headers.Add("Authorization", String.Format("OAuth {0}", AccessToken)); + } + // make async request try { @@ -133,16 +107,7 @@ namespace FeedlySharp } } - if (responseString == "[]") - { - return new T(); - } - if ((new string[] { "", "{}" }).Contains(responseString)) - { - return null; - } - - return DeserializeJson(responseString); + return responseString; } diff --git a/FeedlySharp/FeedlySharp.csproj b/FeedlySharp/FeedlySharp.csproj index 110cc7a..cfc95a2 100644 --- a/FeedlySharp/FeedlySharp.csproj +++ b/FeedlySharp/FeedlySharp.csproj @@ -34,14 +34,15 @@ 4 + - - - - + + + + - +