29 May 2011

Examples on how to use cl-odesk

First of all you need to get keys to use oDesk API.

Let's load cl-odesk. There are 2 ways: to use quicklisp (as of now, 29 May 2011, quicklisp version of cl-odesk has no all the features) or to do it by hand.
Quicklisp way:
(ql:quickload 'odesk)

By hand: fetch latest version from github. Than run at your repl:
(load "odesk.asd")
(require :odesk)


Now let's do some queries to odesk. First we need to define connection. You will need PUBLIC_KEY, PRIVATE_KEY and TOKEN. The last one you can get only by authenticating yourself in your favourite web browser. You can check the logic of this at restas-odesk. Pleas see routes.lisp file.

Let's imagine you already have that token.
We can create connection by instantiating API class:

(defparameter *test-connection*
(make-instance 'odesk:api-json
:public-key "PUBLIC"
:secret-key "SECRET"
:api-token "TOKEN"))


Here is macro that can ease your life a lot:

(with-odesk (:connection con
:public-key "PUBLIC"
:secret-key "SECRET"
:api-token "TOKEN")
(print con))


And another macro that can help too:

(connect-odesk (:public-key "PUBLIC"
:secret-key "SECRET"
:api-token "TOKEN"))


Now let's do request to get info about user.
1) If we go first way and created variable *test-connection*

CL-USER> (odesk:hr/get-user :connection *test-connection*)
("{\"server_time\":\"1306690187\",\"auth_user\":{\"first_name\":\"Dmitriy\",
\"last_name\":\"Budashny\",\"uid\":\"dbudashny\",\"mail\":\"dbudashny@odesk.com\",
\"messenger_id\":\"\",\"messenger_type\":\"\",\"timezone\":\"EET\",
\"timezone_offset\":\"10800\"},\"user\":{\"timezone\":\"UTC+02:00 Eastern Europe\",
\"status\":\"active\",\"timezone_offset\":\"10800\",
\"public_url\":\"https:\\/\\/www.odesk.com\\/users\\/~~d6114868dcf921c5\",
\"last_name\":\"Budashny\",\"email\":\"dbudashny@odesk.com\",\"reference\":\"1001150\",
\"id\":\"dbudashny\",\"is_provider\":\"1\",\"first_name\":\"Dmitriy\"}}")

2) If we go the second way and will use with-odesk macro.
a) subvariant with defining connection explicitly:

CL-USER> (odesk:with-odesk
(:connection my-con
:public-key "PUBLIC"
:secret-key "SECRET"
:api-token "TOKEN")
(odesk:hr/get-user :connection my-con))
("{\"server_time\":\"1306690187\",\"auth_user\":{\"first_name\":\"Dmitriy\",
\"last_name\":\"Budashny\",\"uid\":\"dbudashny\",\"mail\":\"dbudashny@odesk.com\",
\"messenger_id\":\"\",\"messenger_type\":\"\",\"timezone\":\"EET\",
\"timezone_offset\":\"10800\"},\"user\":{\"timezone\":\"UTC+02:00 Eastern Europe\",
\"status\":\"active\",\"timezone_offset\":\"10800\",
\"public_url\":\"https:\\/\\/www.odesk.com\\/users\\/~~d6114868dcf921c5\",
\"last_name\":\"Budashny\",\"email\":\"dbudashny@odesk.com\",\"reference\":\"1001150\",
\"id\":\"dbudashny\",\"is_provider\":\"1\",\"first_name\":\"Dmitriy\"}}")

b) subvariant with default connection key:

CL-USER> (odesk:with-odesk
(:public-key "PUBLIC"
:secret-key "SECRET"
:api-token "TOKEN")
(odesk:hr/get-user))
("{\"server_time\":\"1306690187\",\"auth_user\":{\"first_name\":\"Dmitriy\",
\"last_name\":\"Budashny\",\"uid\":\"dbudashny\",\"mail\":\"dbudashny@odesk.com\",
\"messenger_id\":\"\",\"messenger_type\":\"\",\"timezone\":\"EET\",
\"timezone_offset\":\"10800\"},\"user\":{\"timezone\":\"UTC+02:00 Eastern Europe\",
\"status\":\"active\",\"timezone_offset\":\"10800\",
\"public_url\":\"https:\\/\\/www.odesk.com\\/users\\/~~d6114868dcf921c5\",
\"last_name\":\"Budashny\",\"email\":\"dbudashny@odesk.com\",\"reference\":\"1001150\",
\"id\":\"dbudashny\",\"is_provider\":\"1\",\"first_name\":\"Dmitriy\"}}")


3) Third way is for those who want to create connection globally and then use it in the code with-out the need to use with-odesk macro:

CL-USER> (odesk:connect-odesk (:public-key "PUBLIC"
:secret-key "SECRET"
:api-token "TOKEN"))
#<ODESK:API-JSON {CB4EF11}>
CL-USER> (odesk:hr/get-user)
("{\"server_time\":\"1306690187\",\"auth_user\":{\"first_name\":\"Dmitriy\",
\"last_name\":\"Budashny\",\"uid\":\"dbudashny\",\"mail\":\"dbudashny@odesk.com\",
\"messenger_id\":\"\",\"messenger_type\":\"\",\"timezone\":\"EET\",
\"timezone_offset\":\"10800\"},\"user\":{\"timezone\":\"UTC+02:00 Eastern Europe\",
\"status\":\"active\",\"timezone_offset\":\"10800\",
\"public_url\":\"https:\\/\\/www.odesk.com\\/users\\/~~d6114868dcf921c5\",
\"last_name\":\"Budashny\",\"email\":\"dbudashny@odesk.com\",\"reference\":\"1001150\",
\"id\":\"dbudashny\",\"is_provider\":\"1\",\"first_name\":\"Dmitriy\"}}")

No comments:

Post a Comment

Total Pageviews

Followers