Scala Guide for Java Developers

Source: Internet
Author: User
Tags file size

At the time of writing, the summer is coming to an end, the new school year is about to begin, and Twitter's servers are constantly popping up with updates released by the world's web bugs and non-web bugs. For many of us in North America, from beach parties to football, from outdoor entertainment to indoor projects, all sorts of ideas are pouring in. To keep up with the situation, it is time to revisit Scitter, the Scala client library that is used to access Twitter.

If you've been following Scitter's development so far, you'll know that the library is now able to use a variety of Twitter APIs to view users ' friends, followers, timelines, and more. However, this library does not yet have the ability to publish status updates. In this last article on Scitter, we will enrich the functionality of this library, adding some interesting content (termination and evaluation) features and important methods update (), show () and Destroy (). In this process, you will learn more about the Twitter API, how it interacts with Scala, and how you can overcome the inevitable programming challenges between the two.

Note that when you see this article, the Scitter Library will be in a common source code control library. Of course, I'll also include the source code in this article, but you know that the source code base can change. In other words, the code in the project library may be slightly different from the code you see here, or it might be a little different.

POST to Twitter

So far, our Scitter development has focused on some of the operations based on HTTP get, mainly because these calls are very easy, and I want to easily plunge into the Twitter API. Adding POST and DELETE operations to the library is an important step towards visibility. So far, you can run unit tests on a personal Twitter account, and others don't know what you're doing. However, once you start sending an update message, the world will know that you want to run the Scitter unit test.

If you continue to test Scitter, you will need to create your own "test" account on Twitter. (Perhaps the biggest disadvantage of programming with Twitter API is that there is no suitable test or simulation tool.) )

The current progress

Before embarking on the new UPDATE function for this library, let's review what we've created so far.

Roughly speaking, the Scitter library is divided into 4 parts:

The request and response types (User, Status, etc.) that are sent back and forth are included in the API; they are modeled as case classes.

Optionalparam types, also in certain parts of the API, are modeled as case classes that inherit the basic Optionalparam type.

Scitter object for Communication basics and anonymous (no authentication) access to Twitter.

Scitter class, which holds a username and password to authenticate when accessing a given Twitter account.

Note that in this last article, in order to keep the file size in a relatively reasonable range, I put the request/response type separately in a different file.

Termination and evaluation

Well, now we know the target. We will achieve our goal by implementing two "read-only" Twitter APIs: the End_session API (end user session) and the Rate_limit_status API (which describes how much of the available post is left in a user account for a specific period of time).

The End_session API, similar to its fellow verify_credentials, is also a very simple API: just call it with a validated request, and it will "end" the currently running session. Implementing it on the Scitter class is easy, as shown in Listing 1:

Listing 1. Implement End_session on the Scitter

package  com.tedneward.scitter

{

  import org.apache.commons.httpclient._, auth._, methods._,  params._

  import scala.xml._

  // ...

  class Scitter

  {

   /**

    *

    */ 

   def endSession : Boolean =

   {

    val (statusCode, statusBody) =

     Scitter.execute ("http://twitter.com/account/end_session.xml",

      username, password)

    statusCode == 200

   }

  }

}

Well, I made a slip of the tongue. It's not that easy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.