Skip to content

Commit

Permalink
Removed attachment demo.
Browse files Browse the repository at this point in the history
Added ApiUtils - a library for managing api lifecycles with less boilerplate.

Added default values to http api and improved the api utils.

Fixed spacing and comments.

Removed withName and added a bad request response to handle error cases.

Replaced use of 400 error with a 404 and error message as per HTTP spec.
  • Loading branch information
Clintonio committed Nov 1, 2016
1 parent 572249b commit 7d08c0b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 202 deletions.
3 changes: 3 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ dependencies {

// JPA 2.1 annotations.
compile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final"

// RS API: Response type and codes for ApiUtils.
compile "javax.ws.rs:javax.ws.rs-api:2.0"
}

publishing {
Expand Down
27 changes: 27 additions & 0 deletions core/src/main/kotlin/com/r3corda/core/utilities/ApiUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.r3corda.core.utilities

import com.r3corda.core.crypto.Party
import com.r3corda.core.crypto.parsePublicKeyBase58
import com.r3corda.core.node.ServiceHub
import javax.ws.rs.core.Response

/**
* Utility functions to reduce boilerplate when developing HTTP APIs
*/
class ApiUtils(val services: ServiceHub) {
private val defaultNotFound = { msg: String -> Response.status(Response.Status.NOT_FOUND).entity(msg).build() }

/**
* Get a party and then execute the passed function with the party public key as a parameter.
* Usage: withParty(key) { doSomethingWith(it) }
*/
fun withParty(partyKeyStr: String, notFound: (String) -> Response = defaultNotFound, found: (Party) -> Response): Response {
return try {
val partyKey = parsePublicKeyBase58(partyKeyStr)
val party = services.identityService.partyFromKey(partyKey)
if(party == null) notFound("Unknown party") else found(party)
} catch (e: IllegalArgumentException) {
notFound("Invalid base58 key passed for party key")
}
}
}
200 changes: 0 additions & 200 deletions src/main/kotlin/com/r3corda/demos/attachment/AttachmentDemo.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.google.common.net.HostAndPort
import java.net.URL

class HttpApi(val root: URL) {
fun putJson(path: String, data: Any) = HttpUtils.putJson(URL(root, path), toJson(data))
fun postJson(path: String, data: Any) = HttpUtils.postJson(URL(root, path), toJson(data))
fun putJson(path: String, data: Any = Unit) = HttpUtils.putJson(URL(root, path), toJson(data))
fun postJson(path: String, data: Any = Unit) = HttpUtils.postJson(URL(root, path), toJson(data))

private fun toJson(any: Any) = ObjectMapper().writeValueAsString(any)

Expand Down

0 comments on commit 7d08c0b

Please sign in to comment.