Skip to content

Commit

Permalink
Introduced scalastyle
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-murzinov committed May 18, 2016
1 parent 0db6d98 commit fefa97b
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ scala:
- 2.11.8

script:
- sbt ++$TRAVIS_SCALA_VERSION test tut
- sbt ++$TRAVIS_SCALA_VERSION test tut scalastyle

cache:
directories:
Expand Down
15 changes: 8 additions & 7 deletions featherbed-circe/src/main/scala/featherbed/circe/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@ package featherbed

import java.nio.charset.Charset

import cats.data.Validated.Valid
import cats.data.ValidatedNel
import com.twitter.io.Buf
import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._
import io.circe._
import io.circe.generic.auto._
import io.circe.parser._
import io.circe.syntax._
import shapeless.Witness

package object circe {

private val printer = Printer.noSpaces.copy(dropNullKeys = true)

implicit def circeEncoder[A : Encoder] : content.Encoder[A, Witness.`"application/json"`.T] =
implicit def circeEncoder[A: Encoder]: content.Encoder[A, Witness.`"application/json"`.T] =
content.Encoder.of("application/json") {
(value: A, charset: Charset) => content.Encoder.encodeString(printer.pretty(value.asJson), charset)
}

implicit def circeDecoder[A : Decoder] : content.Decoder.Aux[Witness.`"application/json"`.T, A] =
implicit def circeDecoder[A: Decoder]: content.Decoder.Aux[Witness.`"application/json"`.T, A] =
content.Decoder.of("application/json") {
response =>
content.Decoder.decodeString(response).andThen {
str => (parse(str).toValidated.toValidatedNel : ValidatedNel[Throwable, Json]).andThen {
json : Json => json.as[A].toValidated.toValidatedNel : ValidatedNel[Throwable, A]
str => (parse(str).toValidated.toValidatedNel: ValidatedNel[Throwable, Json]).andThen {
json: Json => json.as[A].toValidated.toValidatedNel: ValidatedNel[Throwable, A]
}
}
}
Expand Down
71 changes: 37 additions & 34 deletions featherbed-core/src/main/scala/featherbed/Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,98 +9,101 @@ import shapeless.Coproduct
/**
* A REST client with a given base URL.
*/
class Client private[featherbed] (private[featherbed] val backend: ClientBackend) {

private[featherbed] class Client (private[featherbed] val backend: ClientBackend) {

This comment has been minimized.

Copy link
@bhudgeons

bhudgeons May 26, 2016

This was a bad change ...

[error] /Users/brandon/Documents/workspace/schoox-api-scalawrapper/src/main/scala/com/schoox/api/Api.scala:36: class Client in package featherbed cannot be accessed in package featherbed
[error]   private val client = new featherbed.Client(baseUrl)
/**
* Construct a [[Client]] with the given base URL. The URL will be used as the base for resolving resources, so
* it usually needs to include the trailing slash "/":
*
* When "foo/bar" is resolved against "http://example.com/api/v1", the result is "http://example.com/api/foo/bar", because
* "v1" is the final path segment; this usually isn't desired. This can't be solved by resolving "/foo/bar", because
* that is a host-relative URI, and will result in "http://example.com/foo/bar" (removing the entire path of the
* base URL).
* When "foo/bar" is resolved against "http://example.com/api/v1", the result is "http://example.com/api/foo/bar",
* because "v1" is the final path segment; this usually isn't desired. This can't be solved by resolving "/foo/bar",
* because that is a host-relative URI, and will result in "http://example.com/foo/bar" (removing the entire path of
* the base URL).
*
* When "foo/bar" is resolved against "http://example.com/api/v1/", on the other hand, the result is the desired
* location of "http://example.com/api/v1/foo/bar".
*
* @param baseUrl The base URL that this client will resolve resources against
*/
def this(baseUrl : URL) = this(ClientBackend(
def this(baseUrl: URL) = this(ClientBackend(
Client.forUrl(baseUrl), baseUrl))

/**
* Specify a GET request to be performed against the given resource
* @param relativePath The path to the resource, relative to the baseUrl
* @return A [[GetRequest]] object, which can further specify and send the request
*/
def get(relativePath : String) = GetRequest[Coproduct.`"*/*"`.T](
this,
backend.baseUrl,
relativePath,
Map.empty,
RequestBuilder())
def get(relativePath: String): GetRequest[Coproduct.`"*/*"`.T] =
GetRequest[Coproduct.`"*/*"`.T](
this,
backend.baseUrl,
relativePath,
Map.empty,
RequestBuilder()
)

/**
* Specify a POST request to be performed against the given resource
* @param relativePath The path to the resource, relative to the baseUrl
* @return A [[PostRequest]] object, which can further specify and send the request
*/
def post(relativePath : String) = PostRequest[Nothing, Nothing, None.type, Coproduct.`"*/*"`.T](
this,
backend.baseUrl,
relativePath,
Map.empty,
RequestBuilder(),
None)
def post(relativePath: String): PostRequest[Nothing, Nothing, None.type, Coproduct.`"*/*"`.T] =
PostRequest[Nothing, Nothing, None.type, Coproduct.`"*/*"`.T](
this,
backend.baseUrl,
relativePath,
Map.empty,
RequestBuilder(),
None
)

/**
* Specify a PUT request to be performed against the given resource
* @param relativePath The path to the resource, relative to the baseUrl
* @return A [[PutRequest]] object, which can further specify and send the request
*/
def put(relativePath : String) = PutRequest[Nothing, Nothing, None.type, Coproduct.`"*/*"`.T](
this,
backend.baseUrl,
relativePath,
Map.empty,
RequestBuilder(),
multipart = false,
None)
def put(relativePath: String): PutRequest[Nothing, Nothing, None.type, Coproduct.`"*/*"`.T] =
PutRequest[Nothing, Nothing, None.type, Coproduct.`"*/*"`.T](
this,
backend.baseUrl,
relativePath,
Map.empty,
RequestBuilder(),
multipart = false,
None
)

/**
* Specify a HEAD request to be performed against the given resource
* @param relativePath The path to the resource, relative to the baseUrl
* @return A [[HeadRequest]] object, which can further specify and send the request
*/
def head(relativePath : String) =
def head(relativePath: String): HeadRequest =
HeadRequest(this, backend.baseUrl, relativePath, Map.empty, RequestBuilder())

/**
* Specify a DELETE request to be performed against the given resource
* @param relativePath The path to the resource, relative to the baseUrl
* @return A [[DeleteRequest]] object, which can further specify and send the request
*/
def delete(relativePath : String) =
def delete(relativePath: String): DeleteRequest[Coproduct.`"*/*"`.T] =
DeleteRequest[Coproduct.`"*/*"`.T](this, backend.baseUrl, relativePath, Map.empty, RequestBuilder())

protected def clientTransform(client: Http.Client): Http.Client = client

val httpClient = clientTransform(backend.client).newClient(Client.hostAndPort(backend.baseUrl))

}

object Client {
private[featherbed] def forUrl(url : URL) = {
private[featherbed] def forUrl(url: URL) = {
val client =
Http.Client()
if(url.getProtocol == "https") client.withTls(url.getHost) else client
}

private[featherbed] def hostAndPort(url : URL) = url.getPort match {
private[featherbed] def hostAndPort(url: URL) = url.getPort match {
case -1 => s"${url.getHost}:${url.getDefaultPort}"
case port => s"${url.getHost}:$port"
}

}

private[featherbed] case class ClientBackend(
Expand Down
Loading

0 comments on commit fefa97b

Please sign in to comment.