tags (get, update, rename, remove) endpoint

This commit is contained in:
2014-11-19 18:16:09 +01:00
parent 5c6dae63f7
commit 1fbec8b809
5 changed files with 106 additions and 3 deletions
+59
View File
@@ -0,0 +1,59 @@
using FeedlySharp.Models;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
namespace FeedlySharp
{
public partial class FeedlyClient
{
public async Task<List<FeedlyTag>> GetTags(CancellationToken cancellationToken = default(CancellationToken))
{
return await Client.AuthRequest<List<FeedlyTag>>(HttpMethod.Get, "v3/tags", null, cancellationToken);
}
public async Task<bool> UpdateTags(string entryId, string[] tags, CancellationToken cancellationToken = default(CancellationToken))
{
string tagsString = String.Join(",", tags.Select(x => ValueToResource("tag", x)));
await Client.AuthRequest<object>(HttpMethod.Put, String.Format("v3/tags/{0}", tagsString), new { entryId = entryId }, cancellationToken);
return true;
}
public async Task<bool> UpdateTags(string[] entryIds, string[] tags, CancellationToken cancellationToken = default(CancellationToken))
{
string tagsString = String.Join(",", tags.Select(x => ValueToResource("tag", x)));
await Client.AuthRequest<object>(HttpMethod.Put, String.Format("v3/tags/{0}", tagsString), new { entryIds = entryIds }, cancellationToken);
return true;
}
public async Task<bool> RenameTag(string oldTag, string newTag, CancellationToken cancellationToken = default(CancellationToken))
{
await Client.AuthRequest<object>(HttpMethod.Post, String.Format("v3/tags/{0}", ValueToResource("tag", oldTag)), new { label = newTag }, cancellationToken);
return true;
}
public async Task<bool> RemoveTags(string[] entryIds, string[] tags, CancellationToken cancellationToken = default(CancellationToken))
{
string tagsString = String.Join(",", tags.Select(x => ValueToResource("tag", x)));
string entryIdsString = String.Join(",", entryIds.Select(x => WebUtility.UrlEncode(x)));
await Client.AuthRequest<object>(HttpMethod.Delete, String.Format("v3/tags/{0}/{1}", tagsString, entryIdsString), null, cancellationToken);
return true;
}
public async Task<bool> RemoveTags(string[] tags, CancellationToken cancellationToken = default(CancellationToken))
{
string tagsString = String.Join(",", tags.Select(x => ValueToResource("tag", x)));
await Client.AuthRequest<object>(HttpMethod.Delete, String.Format("v3/tags/{0}", tagsString), null, cancellationToken);
return true;
}
}
}
+10 -1
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
@@ -20,6 +21,8 @@ namespace FeedlySharp
private string AccessToken { get; set; }
private string UserId { get; set; }
private FeedlyHttpClient Client { get; set; }
@@ -38,9 +41,10 @@ namespace FeedlySharp
}
public void Activate(string accessToken)
public void Activate(string accessToken, string userId)
{
AccessToken = accessToken;
UserId = userId;
Client.AccessToken = accessToken;
}
@@ -50,6 +54,11 @@ namespace FeedlySharp
return String.Format("https://{0}.feedly.com", environment == CloudEnvironment.Production ? "cloud" : "sandbox");
}
private string ValueToResource(string key, string value)
{
return WebUtility.UrlEncode(String.Format("user/{0}/{1}/{2}", UserId, key, value));
}
public void Dispose()
{
+21 -2
View File
@@ -32,14 +32,28 @@ namespace FeedlySharp
throw new FeedlySharpException("This request requires an access token.");
}
return await Request<T>(method, requestUri, parameters, cancellationToken, new Dictionary<string,string>()
return await Request<T>(method, requestUri, parameters, null, cancellationToken, new Dictionary<string,string>()
{
{ "Authorization", String.Format("OAuth {0}", AccessToken) }
});
}
public async Task<T> Request<T>(HttpMethod method, string requestUri, Dictionary<string, string> parameters = null, CancellationToken cancellationToken = default(CancellationToken), Dictionary<string, string> headers = null) where T : class, new()
public async Task<T> AuthRequest<T>(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<T>(method, requestUri, null, body, cancellationToken, new Dictionary<string, string>()
{
{ "Authorization", String.Format("OAuth {0}", AccessToken) }
});
}
public async Task<T> Request<T>(HttpMethod method, string requestUri, Dictionary<string, string> parameters = null, dynamic body = null, CancellationToken cancellationToken = default(CancellationToken), Dictionary<string, string> headers = null) where T : class, new()
{
HttpRequestMessage request = new HttpRequestMessage(method, requestUri);
HttpResponseMessage response = null;
@@ -58,6 +72,11 @@ namespace FeedlySharp
request.Headers.Add(header.Key, header.Value);
}
}
// body
if (body != null)
{
request.Content = new StringContent(JsonConvert.SerializeObject(body));
}
// make async request
try
+2
View File
@@ -36,6 +36,7 @@
<ItemGroup>
<Compile Include="Extensions\JsonExtensions.cs" />
<Compile Include="Extensions\UriExtensions.cs" />
<Compile Include="FeedlyClient.Tags.cs" />
<Compile Include="FeedlyClient.Auth.cs" />
<Compile Include="FeedlyClient.Categories.cs" />
<Compile Include="FeedlyClient.cs" />
@@ -43,6 +44,7 @@
<Compile Include="FeedlyHttpClient.cs" />
<Compile Include="FeedlySharpException.cs" />
<Compile Include="Models\FeedlyCategory.cs" />
<Compile Include="Models\FeedlyTag.cs" />
<Compile Include="Models\FeedlyUser.cs" />
<Compile Include="Models\Responses.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+14
View File
@@ -0,0 +1,14 @@
using Newtonsoft.Json;
using System;
namespace FeedlySharp.Models
{
public class FeedlyTag
{
public string Id { get; set; }
public string Label { get; set; }
public bool IsGlobal { get; set; }
}
}