Skip to content

Commit

Permalink
Remove JSON support
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Birchall committed Aug 30, 2016
1 parent 30e5840 commit 4d823bd
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 127 deletions.
3 changes: 0 additions & 3 deletions build.sbt
Expand Up @@ -81,9 +81,6 @@ val CapiModelsVersion = "9.21"

libraryDependencies ++= Seq(
"com.gu" % "content-api-models-scala" % CapiModelsVersion,
"com.gu" % "content-api-models-json" % CapiModelsVersion,
"org.json4s" %% "json4s-native" % "3.3.0",
"org.json4s" %% "json4s-ext" % "3.3.0",
"joda-time" % "joda-time" % "2.3",
"net.databinder.dispatch" %% "dispatch-core" % "0.11.3",
"org.scalatest" %% "scalatest" % "2.2.1" % "test",
Expand Down
44 changes: 13 additions & 31 deletions src/main/scala/com.gu.contentapi.client/GuardianContentClient.scala
Expand Up @@ -5,8 +5,6 @@ import java.nio.charset.StandardCharsets
import com.gu.contentapi.client.model._
import com.gu.contentapi.client.model.v1._

import com.gu.contentapi.json.JsonParser

import com.gu.contentapi.client.utils.QueryStringParams
import com.ning.http.client.AsyncHttpClientConfig.Builder
import com.ning.http.client.AsyncHttpClient
Expand All @@ -16,13 +14,12 @@ import com.gu.contentapi.buildinfo.CapiBuildInfo
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try

import com.gu.contentapi.client.parser.ThriftDeserializer
import com.gu.contentapi.client.thrift.ThriftDeserializer

case class GuardianContentApiError(httpStatus: Int, httpMessage: String, errorResponse: Option[ErrorResponse] = None) extends Exception(httpMessage)

trait ContentApiClientLogic {
val apiKey: String
def useThrift: Boolean

protected val userAgent = "content-api-scala-client/"+CapiBuildInfo.version

Expand Down Expand Up @@ -59,14 +56,11 @@ trait ContentApiClientLogic {
protected[client] def url(location: String, parameters: Map[String, String]): String = {
require(!location.contains('?'), "must not specify parameters in URL")

val format = if (useThrift) "thrift" else "json"
location + QueryStringParams(parameters + ("api-key" -> apiKey) + ("format" -> format))
location + QueryStringParams(parameters + ("api-key" -> apiKey) + ("format" -> "thrift"))
}

protected def fetch(url: String)(implicit context: ExecutionContext): Future[Array[Byte]] = {

val contentType = if (useThrift) Map("Accept" -> "application/x-thrift") else Map("Accept" -> "application/json")
val headers = Map("User-Agent" -> userAgent) ++ contentType
val headers = Map("User-Agent" -> userAgent, "Accept" -> "application/x-thrift")

for (response <- get(url, headers)) yield {
if (List(200, 302) contains response.statusCode) response.body
Expand All @@ -75,15 +69,11 @@ trait ContentApiClientLogic {
}

private def contentApiError(response: HttpResponse): GuardianContentApiError = {
if (useThrift) {
val errorResponse = Try(ThriftDeserializer.deserialize(response.body, ErrorResponse)).toOption
GuardianContentApiError(response.statusCode, response.statusMessage, errorResponse)
}
else GuardianContentApiError(response.statusCode, response.statusMessage, JsonParser.parseError(new String(response.body, "UTF-8")))
val errorResponse = Try(ThriftDeserializer.deserialize(response.body, ErrorResponse)).toOption
GuardianContentApiError(response.statusCode, response.statusMessage, errorResponse)
}

protected def get(url: String, headers: Map[String, String])(implicit context: ExecutionContext): Future[HttpResponse] = {

val req = headers.foldLeft(dispatch.url(url)) {
case (r, (name, value)) => r.setHeader(name, value)
}
Expand All @@ -102,44 +92,37 @@ trait ContentApiClientLogic {

def getResponse(itemQuery: ItemQuery)(implicit context: ExecutionContext): Future[ItemResponse] =
fetchResponse(itemQuery) map { response =>
if (useThrift) ThriftDeserializer.deserialize(response, ItemResponse)
else JsonParser.parseItem(new String(response, StandardCharsets.UTF_8))
ThriftDeserializer.deserialize(response, ItemResponse)
}

def getResponse(searchQuery: SearchQuery)(implicit context: ExecutionContext): Future[SearchResponse] =
fetchResponse(searchQuery) map { response =>
if (useThrift) ThriftDeserializer.deserialize(response, SearchResponse)
else JsonParser.parseSearch(new String(response, StandardCharsets.UTF_8))
ThriftDeserializer.deserialize(response, SearchResponse)
}

def getResponse(tagsQuery: TagsQuery)(implicit context: ExecutionContext): Future[TagsResponse] =
fetchResponse(tagsQuery) map { response =>
if (useThrift) ThriftDeserializer.deserialize(response, TagsResponse)
else JsonParser.parseTags(new String(response, StandardCharsets.UTF_8))
ThriftDeserializer.deserialize(response, TagsResponse)
}

def getResponse(sectionsQuery: SectionsQuery)(implicit context: ExecutionContext): Future[SectionsResponse] =
fetchResponse(sectionsQuery) map { response =>
if (useThrift) ThriftDeserializer.deserialize(response, SectionsResponse)
else JsonParser.parseSections(new String(response, StandardCharsets.UTF_8))
ThriftDeserializer.deserialize(response, SectionsResponse)
}

def getResponse(editionsQuery: EditionsQuery)(implicit context: ExecutionContext): Future[EditionsResponse] =
fetchResponse(editionsQuery) map { response =>
if (useThrift) ThriftDeserializer.deserialize(response, EditionsResponse)
else JsonParser.parseEditions(new String(response, StandardCharsets.UTF_8))
ThriftDeserializer.deserialize(response, EditionsResponse)
}

def getResponse(removedContentQuery: RemovedContentQuery)(implicit context: ExecutionContext): Future[RemovedContentResponse] =
fetchResponse(removedContentQuery) map { response =>
if (useThrift) ThriftDeserializer.deserialize(response, RemovedContentResponse)
else JsonParser.parseRemovedContent(new String(response, StandardCharsets.UTF_8))
ThriftDeserializer.deserialize(response, RemovedContentResponse)
}

def getResponse(videoStatsQuery: VideoStatsQuery)(implicit context: ExecutionContext): Future[VideoStatsResponse] =
fetchResponse(videoStatsQuery) map { response =>
if (useThrift) ThriftDeserializer.deserialize(response, VideoStatsResponse)
else JsonParser.parseVideoStats(new String(response, StandardCharsets.UTF_8))
ThriftDeserializer.deserialize(response, VideoStatsResponse)
}

/**
Expand All @@ -151,6 +134,5 @@ trait ContentApiClientLogic {

}

class GuardianContentClient(val apiKey: String, val useThrift: Boolean = false) extends ContentApiClientLogic

class GuardianContentClient(val apiKey: String) extends ContentApiClientLogic

@@ -1,4 +1,4 @@
package com.gu.contentapi.client.parser
package com.gu.contentapi.client.thrift

import java.io.ByteArrayInputStream

Expand Down
8 changes: 0 additions & 8 deletions src/test/scala/com.gu.contentapi.client/ClientTest.scala

This file was deleted.

Expand Up @@ -7,11 +7,18 @@ import com.gu.contentapi.client.model.{SearchQuery, ItemQuery}
import org.joda.time.DateTime
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.{Seconds, Span}
import org.scalatest.{OptionValues, FlatSpec, Matchers}
import org.scalatest.{OptionValues, FlatSpec, Matchers, BeforeAndAfterAll}

import scala.concurrent.ExecutionContext.Implicits.global

class GuardianContentClientTest extends FlatSpec with Matchers with ClientTest with ScalaFutures with OptionValues {
class GuardianContentClientTest extends FlatSpec with Matchers with ScalaFutures with OptionValues with BeforeAndAfterAll {

val api = new GuardianContentClient("test")

override def afterAll() {
api.shutdown()
}

implicit override val patienceConfig = PatienceConfig(timeout = Span(5, Seconds))

"client interface" should "successfully call the Content API" in {
Expand Down

This file was deleted.

0 comments on commit 4d823bd

Please sign in to comment.