Bookmark and Share

Twitter Samples

This sample contains the implementation of a some Twitter Rest APIs: Status, Direct Message and User.

NB: The following are partial mappings of the available services and their main purpose is to give a quick example of a real world use case scenario of CRest usage, thus the mappings may not reflect all the services offered by the API.

Status

This sample maps some of the Twitter's Statuses Rest API.

Below the mapped interface. Note: the mapping below only shows some mapped method, to have a look at all of them, refer to the sample source code.

@EndPoint("http://api.twitter.com")
@Path("/1/statuses")
@HeaderParam(value = "Accept-Encoding", defaultValue = "gzip")
@Consumes("application/json")
public interface StatusService {

    @POST
    @Path("/update.json")
    Status updateStatus(@FormParam("status") String status);

    @POST
    @Path("/update.json")
    Status updateStatus(
            @FormParam("status") String status,
            @QueryParam("lat") float lat,
            @QueryParam("long") float longitude);

    (...)

    @Path("/show/{id}.json")
    Status getStatus(@PathParam("id") long id);

    @Path("/retweets/{id}.json")
    Status[] getRetweets(@PathParam("id") long id);

    (...)

    @Path("/friends.json")
    User[] getFriends(
            @QueryParam("user_id") long userId,
            @QueryParam("screen_name") String screenName);

    @Path("/friends.json")
    User[] getFriends(@QueryParam("user_id") long userId);

    @Path("/friends.json")
    User[] getFriends(@QueryParam("screen_name") String screenName);

    @Path("/friends.json")
    Cursor.User getFriends(
            @QueryParam("user_id") long userId,
            @QueryParam("screen_name") String screenName,
            @QueryParam("cursor") long cursor);

    @Path("/friends.json")
    Cursor.User getFriends(
            @QueryParam("user_id") long userId,
            @QueryParam("cursor") long cursor);

    @Path("/friends.json")
    Cursor.User getFriends(
            @QueryParam("screen_name") String screenName,
            @QueryParam("cursor") long cursor);

    (...)

}

And here's how to get an instance of the StatusService:

String consumerKey = ...;
String consumerSecret = ...;
String accessToken = ...;
String accessTokenSecre = ...;

// Get a CRest instance. Should be done once.
CRest crest = CRest.getOAuthInstance(consumerKey, consumerSecret, accessToken, accessTokenSecret);

// get a service instance
StatusService service = crest.build(StatusService.class);

// use it !
Status status = service.updateStatus("Status updated!");
(...)

Direct Message

This sample maps some of the Twitter's Direct Message Rest API.

Below the mapped interface.

@EndPoint("http://api.twitter.com")
@Path("/1")
@HeaderParam(value = "Accept-Encoding", defaultValue = "gzip")
@Consumes("application/json")
public interface DirectMessageService {

    @Path("direct_messages.json")
    Message[] getReceived(
            @QueryParam("count") long count,
            @QueryParam("page") long page,
            @QueryParam("since_id") long sinceId,
            @QueryParam("max_id") long maxId
            );

    @Path("direct_messages.json")
    Message[] getReceived(
            @QueryParam("count") long count,
            @QueryParam("page") long page);

    @Path("direct_messages/sent.json")
    Message[] getSent(
            @QueryParam("count") long count,
            @QueryParam("page") long page,
            @QueryParam("since_id") long sinceId,
            @QueryParam("max_id") long maxId);

    @Path("direct_messages/sent.json")
    Message[] getSent(
            @QueryParam("count") long count,
            @QueryParam("page") long page);

    @POST
    @Path("direct_messages/new.json")
    Message send(
            @QueryParam("user_id") long userId,
            @QueryParam("text") String msg);

    @POST
    @Path("direct_messages/new.json")
    Message send(
            @QueryParam("user_id") long userId,
            @QueryParam("text") String msg,
            @QueryParam("screen_name") String screenName);

    @DELETE
    @Path("direct_messages/destroy/{msgid}.json")
    Message destroy(@PathParam("msgid") long msgId);

}

And here's how to get an instance of the DirectMessageService:

String consumerKey = ...;
String consumerSecret = ...;
String accessToken = ...;
String accessTokenSecre = ...;

// Get a CRest instance. Should be done once.
CRest crest = CRest.getOAuthInstance(consumerKey, consumerSecret, accessToken, accessTokenSecret);

// get a service instance
DirectMessageService service = crest.build(DirectMessageService.class);

// use it !
Message msg = service.send(userId, "Hi mate, what's up!");
(...)

User

This sample maps some of the Twitter's User Rest API.

Below the mapped interface.

@EndPoint("http://api.twitter.com")
@Path("/1/users")
@HeaderParam(value = "Accept-Encoding", defaultValue = "gzip")
@Consumes("application/json")
public interface UserService {

    @Path("/search.json")
    User[] search(
            @QueryParam("q") String search);

    @Path("/search.json")
    User[] search(
            @QueryParam("q") String search,
            @QueryParam("per_page") long count,
            @QueryParam("page") long page);

    @Path("/show.json")
    User get(@QueryParam("user_id") long id);

    @Path("/show.json")
    User get(@QueryParam("screen_name") String screenName);

    @Path("/lookup.json")
    User[] lookup(
            @QueryParam("user_id") long id,
            @QueryParam("screen_name") String... screenName);

    @Path("/lookup.json")
    User[] lookup(@QueryParam("screen_name") String[] screenName);
}

And here's how to get an instance of the DirectMessageService:

String consumerKey = ...;
String consumerSecret = ...;
String accessToken = ...;
String accessTokenSecre = ...;

// Get a CRest instance. Should be done once.
CRest crest = CRest.getOAuthInstance(consumerKey, consumerSecret, accessToken, accessTokenSecret);

// get a service instance
UserService service = crest.build(UserService.class);

// use it !
User user = service.get(userId);
(...)

Model & Sources

The data model used by these samples hasn't been documented in this page as it only limits itself to plain java bean objects. The model have been deduced from the API response format and documentation and isn't guaranteed to reflect the full available model the API offers.

Do get a look at it, please refer to the sample source code HTML-based version.

This code is part of a project containing more sample, go to the project home page to get more information.