EP / me2day-scala

me2day API for scala

Clone this repository (size: 251.3 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/EP/me2day-scala/
commit 38: 9c052701c27c
parent 37: 2dafd3e5ce81
branch: default
User authorization is optional. Extract basic http authentication.
Eung-ju PARK
12 months ago

Changed (Δ233 bytes):

Up to file-list src/main/scala/net/me2day/scala/Me2Day.scala:

@@ -32,34 +32,38 @@ object Me2Day {
32
32
33
33
class Me2Day(val application : Application, val user : User) {
34
34
    val baseUrl = "http://me2day.net/api"
35
35
    
36
    def this(application : Application) = this(application, null)
37
    
36
38
    def execute[T](command : Command[T]) : T = {
37
39
        val invocation = prepareInvocation(command.name, command.method)
38
40
        command.parameters.foreach { x =>
39
41
            invocation.addParameter(x._1, x._2)
40
42
        }
41
43
        val document = invocation.execute()
42
        println(command.name + ":" + document)
43
44
        command.parseResponse(document)
44
45
    }
45
46
46
    def url(name : String) : String = baseUrl + "/" + name + ".xml"
47
    def commandUrl(name : String) : String = baseUrl + "/" + name + ".xml"
47
48
    
48
    def basicAuthorization() : String = {
49
        val base64 = new BASE64Encoder()
50
        "Basic " + base64.encode((user.name + ":" + user.generatePassword).getBytes)
51
    }
52
53
49
    def prepareInvocation(name : String, httpMethod : String) : HttpInvocation = {
54
        val invocation = new HttpInvocation(url(name), httpMethod)
55
        invocation.addHeader("Authorization", basicAuthorization)
50
        val invocation = new HttpInvocation(commandUrl(name), httpMethod)
56
51
        invocation.addHeader("me2_application_key", application.key)
52
        if (user != null) {
53
            val authentication = new BasicHttpAuthentication(user.name, user.generatePassword)
54
            invocation.addHeader("Authorization", authentication.credentials)
55
        }
57
56
        invocation.addHeader("Content-Type", "application/x-www-form-urlencoded")
58
57
        return invocation
59
58
    }
60
59
}
61
60
62
import java.util.Date
61
class BasicHttpAuthentication(userid : String, password : String) {
62
    private val base64 = new BASE64Encoder()
63
63
64
    def credentials = "Basic " + basicCredentials
65
66
    def basicCredentials = base64.encode((userid + ":" + password).getBytes)
67
}
64
68
65
69
case class ErrorResult(code : Int, message : String, description : String)