/pbs-api-client

API Client/SDK for Paperbackswap.com

Primary LanguageJavaMIT LicenseMIT

alt text API client/SDK for Paperbackswap.com

Build Status

  • Built as a standalone Java 1.5+ library, usable in Android.
  • Builds with Gradle.
  • Uses fluent syntax wherever possible (perhaps at a slight loss of efficiency as mikaelhg's UrlBuilder is final)

Table of Contents generated with DocToc

Common Usage

OAuth 1.0a

Do the OAuth 1.0a dance

PbsOauth oauth = PbsOauth("key", "secret");
String requestTokenUrl = oauth.getRequestTokenUrl("http://localhost:8080/capture_verifier")
System.out.println(oauth.isAuthorizing()); // "true"

// Go do things in the browser with requestTokenUrl, capture verifier from callback

oauth.retrieveAccessToken(verifier)
System.out.println(oauth.isAuthorizing()); // "false"

String signedUrl = oauth.signRequest(
    PbsUrlInfo.MEMBER_WISH_LIST.toString();
);

Sign the request with saved token/secret

PbsOauth oauth = PbsOauth("key", "secret", "token", "tokensecret");
String signedUrl = oauth.signRequest(
    PbsUrlInfo.MEMBER_WISH_LIST.toString()
);

Build URLs

Build a URL from the know list

PbsUrlInfo.MEMBER_WISH_LIST.toString();
PbsUrlInfo.MEMBER_WISH_LIST.toUri();
PbsUrlInfo.MEMBER_WISH_LIST.toBuilder();

PbsUrlBuilder builder = PbsUrlBuilder
    .fromPath(PbsUrlInfo.MEMBER_WISH_LIST)
    .toString();

Build a URL from a string

In the event that you need to build your own URL

PbsUrlBuilder builder = PbsUrlBuilder
    .fromUrl(url)
    .toString();

Explicitly set a page

PbsUrlInfo.MEMBER_WISH_LIST.toBuilder()
    .withOffsetLimit(10, 10)
    .toString();

Set an isbn in the URL

PbsUrlInfo.ADD_TO_WISH_LIST.toBuilder()
    .withIsbn(0312851820)
    .toString();

Deserialize responses

It should be noted that the BookListBuilder will process single book responses (e.g. RequestType=ISBNList when only one ISBN is provided) or a list of books. May not work properly with Requests at this point. Not every field is guaranteed to be deseralized (feel free to PR more).

Turn response text/json into a list of books

// Go fetch response with your favorite HTTP client stack => response
BookList bookList = BooksFactory.getBookList(response);
for (Book book : bookList) {
    System.out.println(book.getTitle());
}

Get next page of results

This is useful when using HTTP, as PBS API automatically generates HTTPS URLs in paging elements. The getNextPage() method fixes this behavior, and maintains HTTP.

// Go fetch response with your favorite HTTP client stack => response
BookList bookList = BooksFactory.getBookList(response);
// Returns PbsUrlBuilder, so you can manipulate if needed
System.out.println(bookList.getNextPage().toString());

Deserialize one book

// Extract one book's JSON => json

// Not typically used directly, this is used internally by getBookList
// Will not work correctly with PBS response
Book book = BooksFactory.getBook(json)
System.out.println(book.getTitle());

Acknowledgements

Thanks to a few great Java libraries that made this much tidier