Files
PocketSharp/README.md
T

292 lines
8.7 KiB
Markdown
Raw Normal View History

2013-09-06 11:36:41 +02:00
![PocketSharp](https://raw.github.com/ceee/PocketSharp/master/PocketSharp.Website/Assets/Images/github-header.png)
2013-09-05 21:58:13 +02:00
2013-09-15 15:46:13 +02:00
**PocketSharp** is a C#.NET class library, that integrates the [Pocket API v3](http://getpocket.com/developer) and consists of 4 parts: [Authentication](#authentication), [Retrieve](#retrieve), [Modify](#modify) and [Add](#add).
2013-07-29 22:18:41 +02:00
[pocketsharp.frontendplay.com](http://pocketsharp.frontendplay.com/)
2013-07-29 22:18:41 +02:00
## Install using NuGet
2013-07-02 20:58:13 +02:00
2013-07-29 22:22:05 +02:00
```
Install-Package PocketSharp
```
2013-07-02 20:58:13 +02:00
2013-09-15 15:46:13 +02:00
## Supported platforms
PocketSharp is a **Portable Class Library** (since 1.0.0), therefore it's compatible with multiple platforms:
- **.NET** >= 4.0.3 (including WPF)
- **Silverlight** >= 4
- **Windows Phone** >= 7.5
- **Windows Store**
2013-09-17 23:28:28 +02:00
You can find examples for Silverlight 5, WP8 and WPF in the `PocketSharp.Examples` ([@github](https://github.com/ceee/PocketSharp/tree/master/PocketSharp.Examples)) folder.
2013-09-15 15:46:13 +02:00
## Async Support
All public methods, which communicate with the Pocket servers are asynchronous. So they should be called with the preceding `await` keyword, and the encapsulated method marked with `async`.
_In PocketSharp versions < 1.0.0 all methods lacked async support and were blocking._
2013-06-26 14:16:07 +02:00
## Usage Example
2013-06-13 11:07:27 +02:00
2013-06-27 11:43:23 +02:00
Request a [Consumer Key on Pocket.](http://getpocket.com/developer/apps/)
2013-06-26 14:16:07 +02:00
Include the PocketSharp namespace and it's associated models (you will need them later):
2013-06-26 14:21:15 +02:00
```csharp
using PocketSharp;
using PocketSharp.Models;
```
2013-06-26 14:16:07 +02:00
Initialize PocketClient with:
2013-06-26 14:21:15 +02:00
```csharp
PocketClient _client = new PocketClient("[YOUR_CONSUMER_KEY]", "[YOUR_ACCESS_CODE]");
```
2013-06-26 14:16:07 +02:00
Do a simple request - e.g. a search for `CSS`:
2013-06-26 14:21:15 +02:00
```csharp
2013-09-15 15:46:13 +02:00
var items = await _client.Search("css");
items.ForEach(
2013-09-15 21:44:09 +02:00
item => Debug.WriteLine(item.ID + " | " + item.Title)
2013-06-26 14:21:15 +02:00
);
```
2013-06-26 14:16:07 +02:00
Which will output:
330361896 | CSS Front-end Frameworks with comparison : By usabli.ca
345541438 | Editr - HTML, CSS, JavaScript playground
251743431 | CSS Architecture
343693149 | CSS3 Transitions - Thank God We Have A Specification!
...
## Create an instance
2013-07-01 23:17:31 +02:00
Constructor:
2013-06-26 14:21:15 +02:00
```csharp
2013-07-01 23:17:31 +02:00
PocketClient(string consumerKey, string accessCode = null, Uri callbackUri = null)
```
2013-06-26 14:21:15 +02:00
2013-07-01 23:17:31 +02:00
`consumerKey`: The API key
<br>
`accessCode`: Provide an access code if the user is already authenticated
<br>
`callbackUri`: The callback URL is called by Pocket after authentication
2013-06-26 14:21:15 +02:00
2013-07-01 23:17:31 +02:00
Example:
2013-06-26 14:21:15 +02:00
2013-07-01 23:17:31 +02:00
```csharp
PocketClient _client = new PocketClient(
consumerKey: "123498237423498723498723",
callbackUri: new Uri("http://ceecore.com"),
accessCode: "097809-oi987-izi8-jk98-oiuu89"
);
2013-06-26 14:21:15 +02:00
```
2013-06-26 14:16:07 +02:00
You can change the _Access Code_ after initialization:
2013-06-26 14:21:15 +02:00
```csharp
_client.AccessCode = "[YOU_ACCESS_CODE]";
```
2013-06-26 14:16:07 +02:00
2013-07-01 23:17:31 +02:00
**Before authentication** you will need to provide the `callbackUri` for authentication requests.
<br>
**After authentication** you will need to provide the `accessCode`.
2013-06-26 14:16:07 +02:00
## Authentication
In order to communicate with a Pocket User, you will need a consumer key (which is generated by [creating a new application on Pocket](http://getpocket.com/developer/apps/)) and an **Access Code**.
2013-07-02 20:58:13 +02:00
The authentication is a 3-step process:
2013-06-26 14:16:07 +02:00
2013-07-02 21:05:43 +02:00
#### 1) Generate authentication URI
2013-07-02 20:59:06 +02:00
Receive the **request code** and **authentication URI** from the library by calling `string GetRequestCode()`:
2013-06-26 14:16:07 +02:00
2013-06-26 14:21:15 +02:00
```csharp
2013-09-15 15:46:13 +02:00
string requestCode = await _client.GetRequestCode();
2013-07-02 20:58:13 +02:00
// 0f453f2d-1605-8584-28fd-39af8e
2013-07-01 23:17:31 +02:00
Uri authenticationUri = _client.GenerateAuthenticationUri();
2013-07-02 20:58:13 +02:00
// https://getpocket.com/auth/authorize?request_token=0f453f2d-1605-8584-28fd-39af8e&redirect_uri=http%253a%252f%252fceecore.com
2013-06-26 14:21:15 +02:00
```
2013-06-26 14:16:07 +02:00
2013-07-01 23:17:31 +02:00
The _request code_ is stored internally, but you can also provide it as param in `GenerateAuthenticationUri(string requestCode = null)`.
2013-06-26 14:16:07 +02:00
2013-07-02 21:05:43 +02:00
#### 2) Redirect to Pocket
2013-07-02 20:59:06 +02:00
Next you need to redirect the user to the `authenticationUri`, which displays a prompt to grant permissions for the application (see image). After the user granted or denied, he/she is redirected to the `callbackUri`.
2013-06-26 14:16:07 +02:00
2013-07-02 20:59:06 +02:00
![authentication screen](https://raw.github.com/ceee/PocketSharp/master/PocketSharp/authentication-screen.png)
2013-07-02 21:05:43 +02:00
#### 3) Get Access Code
2013-09-15 15:46:13 +02:00
Call `Task<string> GetAccessCode(string requestCode = null)`
2013-06-26 14:16:07 +02:00
2013-06-26 14:21:15 +02:00
```csharp
2013-09-15 15:46:13 +02:00
string accessCode = await _client.GetAccessCode();
2013-07-02 20:58:13 +02:00
// fa8bfc16-69b3-4d22-7db7-84a58d
2013-06-26 14:21:15 +02:00
```
2013-06-26 14:16:07 +02:00
2013-07-01 23:17:31 +02:00
Again, the received _access code_ is stored internally.
Note that `GetAccessCode` can only be called with an existing _request code_. If you need to re-authenticate a user, start again with Step 1).
2013-06-26 14:16:07 +02:00
#### Important
**Be sure to permanently store the _Access Code_ for your user.**
<br>
Without it you would always have to redo the authentication process.
## Retrieve
2013-07-02 21:05:43 +02:00
Get list of all items:
```csharp
2013-09-15 15:46:13 +02:00
List<PocketItem> items = await _client.Retrieve();
2013-09-17 23:28:28 +02:00
// equivalent to: await _client.RetrieveByFilter(RetrieveFilter.All)
```
Get a list with specific parameters (explanation in the [Pocket Docs](http://getpocket.com/developer/docs/v3/retrieve)):
```csharp
List<PocketItem> items = await _client.Retrieve(
State? state = null,
bool? favorite = null,
string tag = null,
ContentType? contentType = null,
Sort? sort = null,
string search = null,
string domain = null,
DateTime? since = null,
int? count = null,
int? offset = null
);
```
It's best to use parameters as _named parameters_, to avoid typing `null` values:
```csharp
List<PocketItem> items = await _client.Retrieve(count: 10, offset: 20, sort: Sort.oldest);
2013-07-02 21:05:43 +02:00
```
2013-06-26 14:16:07 +02:00
Find items by a tag:
2013-06-26 14:21:15 +02:00
```csharp
2013-09-15 15:46:13 +02:00
List<PocketItem> items = await _client.SearchByTag("tutorial");
2013-06-26 14:21:15 +02:00
```
2013-06-26 14:16:07 +02:00
Find items by a search string:
2013-06-26 14:21:15 +02:00
```csharp
2013-09-15 15:46:13 +02:00
List<PocketItem> items = await _client.Search("css");
2013-06-26 14:21:15 +02:00
```
2013-06-26 14:16:07 +02:00
2013-09-17 23:28:28 +02:00
Get a filtered list:
2013-06-26 14:16:07 +02:00
2013-06-26 14:21:15 +02:00
```csharp
2013-09-17 23:28:28 +02:00
List<PocketItem> items = await _client.RetrieveByFilter(RetrieveFilter.Favorite);
// returns favorites only
2013-06-26 14:21:15 +02:00
```
2013-06-26 14:16:07 +02:00
The RetrieveFilter Enum is specified as follows:
2013-06-26 14:21:15 +02:00
```csharp
enum RetrieveFilter { All, Unread, Archive, Favorite, Article, Video, Image }
```
2013-06-26 14:16:07 +02:00
## Add
2013-06-26 14:16:07 +02:00
Adds a new item to your pocket list.
Accepts four parameters, with `uri` being required.
```csharp
2013-09-15 15:46:13 +02:00
Task<PocketItem> Add(Uri uri, string[] tags = null, string title = null, string tweetID = null)
```
Example:
```csharp
2013-09-15 15:46:13 +02:00
PocketItem newItem = await _client.Add(
new Uri("http://www.neowin.net/news/full-build-2013-conference-sessions-listing-revealed"),
new string[] { "microsoft", "neowin", "build" }
);
```
The title can be included for cases where an item does not have a title, which is typical for image or PDF URLs. If Pocket detects a title from the content of the page, this parameter will be ignored.
If you are adding Pocket support to a Twitter client, please send along a reference to the tweet status id (with the tweetID). This allows Pocket to show the original tweet alongside the article.
## Modify
All Modify methods accept either the itemID (as int) or a `PocketItem` as parameter.
Archive the specified item:
2013-09-15 15:46:13 +02:00
bool isSuccess = await _client.Archive(myPocketItem);
Un-archive the specified item:
2013-09-15 15:46:13 +02:00
bool isSuccess = await _client.Unarchive(myPocketItem);
Favorites the specified item:
2013-09-15 15:46:13 +02:00
bool isSuccess = await _client.Favorite(myPocketItem);
Un-favorites the specified item:
2013-09-15 15:46:13 +02:00
bool isSuccess = await _client.Unfavorite(myPocketItem);
Deletes the specified item:
2013-09-15 15:46:13 +02:00
bool isSuccess = await _client.Delete(myPocketItem);
2013-06-27 11:43:23 +02:00
#### Modify tags
Add tags to the specified item:
2013-09-15 15:46:13 +02:00
bool isSuccess = await _client.AddTags(myPocketItem, new string[] { "css", "2013" });
Remove tags from the specified item:
2013-09-15 15:46:13 +02:00
bool isSuccess = await _client.RemoveTags(myPocketItem, new string[] { "css", "2013" });
Remove all tags from the specified item:
2013-09-15 15:46:13 +02:00
bool isSuccess = await _client.RemoveTags(myPocketItem);
Replaces all existing tags with new ones for the specified item:
2013-09-15 15:46:13 +02:00
bool isSuccess = await _client.ReplaceTags(myPocketItem, new string[] { "css", "2013" });
Renames a tag for the specified item:
2013-09-15 15:46:13 +02:00
bool isSuccess = await _client.RenameTag(myPocketItem, "oldTagName", "newTagName");
2013-06-26 14:16:07 +02:00
---
## Release History
2013-09-18 21:19:38 +02:00
- 2013-09-18 v1.2.1 correct parameter conversion for DateTime and Boolean
2013-09-17 23:28:28 +02:00
- 2013-09-17 v1.2.0 simplified retrieve methods
- 2013-09-17 v1.1.0 fix modification requests
2013-09-15 15:46:13 +02:00
- 2013-09-15 v1.0.0 convert to PCL & implement async
2013-08-16 14:02:12 +02:00
- 2013-08-16 v0.3.2 tag modification fixed and full retrieval of items for Retrieve method
2013-07-07 22:33:09 +02:00
- 2013-07-07 v0.3.1 authentication fixes
2013-07-02 21:15:57 +02:00
- 2013-07-02 v0.3.0 update authentication process
2013-06-27 11:38:57 +02:00
- 2013-06-27 v0.2.0 add, modify item & modify tags
2013-06-26 14:16:07 +02:00
- 2013-06-26 v0.1.0 authentication & retrieve functionality
2013-06-13 11:07:27 +02:00
2013-07-09 23:13:06 +02:00
## Dependencies
2013-07-02 21:15:57 +02:00
2013-09-15 15:46:13 +02:00
- [Microsoft.Bcl.Async](https://www.nuget.org/packages/Microsoft.Bcl.Async/)
- [Microsoft.Net.Http](https://www.nuget.org/packages/Microsoft.Net.Http/)
- [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json/)
2013-07-02 21:15:57 +02:00
2013-06-13 11:07:27 +02:00
## Contributors
| [![twitter/artistandsocial](http://gravatar.com/avatar/9c61b1f4307425f12f05d3adb930ba66?s=70)](http://twitter.com/artistandsocial "Follow @artistandsocial on Twitter") |
|---|
2013-07-09 23:13:06 +02:00
| [Tobias Klika @ceee](https://github.com/ceee) |
## License
2013-07-29 22:22:05 +02:00
[MIT License](https://github.com/ceee/PocketSharp/blob/master/LICENSE-MIT)