Skip to content

Commit

Permalink
added postWithHeaders instead of adding parameter to the existing post
Browse files Browse the repository at this point in the history
  • Loading branch information
pvighi committed Nov 16, 2018
1 parent 2d79b22 commit aeb3a1b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 21 deletions.
Expand Up @@ -12,7 +12,7 @@ class CreateGuestAccountTest extends FlatSpec with Matchers {
it should "create a request ok" in {
val actual = CreateGuestAccount.toRequest(EmailAddress("hello@gu.com"))

val expected = new PostRequest(JsObject(List("primaryEmailAddress" -> JsString("hello@gu.com"))), RelativePath("/guest"), List.empty)
val expected = new PostRequest(JsObject(List("primaryEmailAddress" -> JsString("hello@gu.com"))), RelativePath("/guest"))
actual should be(expected)
}

Expand Down
Expand Up @@ -66,7 +66,7 @@ object StartJobHandler {
for {
sfConfig <- loadConfig[SFAuthConfig](SFExportAuthConfig.location, SFAuthConfig.reads).leftMap(_.error).toTry
sfClient <- SalesforceClient(getResponse, sfConfig).value.toTry
createJobOp = sfClient.wrapWith(JsonHttp.post).wrapWith(CreateJob.wrapper).runRequest _
createJobOp = sfClient.wrapWith(JsonHttp.postWithHeaders).wrapWith(CreateJob.wrapper).runRequest _
addQueryToJobOp = sfClient.wrapWith(AddQueryToJob.wrapper).runRequest _
wiredSteps = steps(getCurrentDate, createJobOp, addQueryToJobOp) _
response <- wiredSteps(ObjectName(request.objectName))
Expand Down
Expand Up @@ -39,22 +39,23 @@ object CreateJob {
}

case class CreateJobRequest(
objectType : SfObjectName,
objectType: SfObjectName,
maybeChunkSize: Option[BatchSize]
)

def toRequest(request:CreateJobRequest):PostRequest = {
val wireRequest = WireRequest(request.objectType.value)
val maybeChunkingHeader = request.maybeChunkSize.map { chunkSize =>
Header(name = "Sforce-Enable-PKChunking", value = s"chunkSize=${chunkSize.value}")
}
val headers = maybeChunkingHeader.toList
val relativePath = RelativePath("/services/async/44.0/job")
PostRequest(wireRequest, relativePath, headers)
def toRequest(request: CreateJobRequest): PostRequestWithHeaders = {
val wireRequest = WireRequest(request.objectType.value)
val maybeChunkingHeader = request.maybeChunkSize.map { chunkSize =>
Header(name = "Sforce-Enable-PKChunking", value = s"chunkSize=${chunkSize.value}")
}
val headers = maybeChunkingHeader.toList
val relativePath = RelativePath("/services/async/44.0/job")
PostRequestWithHeaders(wireRequest, relativePath, headers)
}
def toResponse(wireResponse: WireResponse) : JobId = JobId(wireResponse.id)

val wrapper: HttpOpWrapper[CreateJobRequest, PostRequest, JsValue, JobId] =
HttpOpWrapper[CreateJobRequest, PostRequest, JsValue, JobId](toRequest, RestRequestMaker.toResult[WireResponse](_).map(toResponse))
def toResponse(wireResponse: WireResponse): JobId = JobId(wireResponse.id)

val wrapper: HttpOpWrapper[CreateJobRequest, PostRequestWithHeaders, JsValue, JobId] =
HttpOpWrapper[CreateJobRequest, PostRequestWithHeaders, JsValue, JobId](toRequest, RestRequestMaker.toResult[WireResponse](_).map(toResponse))

}
Expand Up @@ -16,7 +16,7 @@ class CloseJobTest extends FlatSpec with Matchers {
"state" -> JsString("Closed"),

))
val expected = new PostRequest(expectedBody, RelativePath("/services/async/44.0/job/someJobId"), List.empty)
val expected = new PostRequest(expectedBody, RelativePath("/services/async/44.0/job/someJobId"))
actual should be(expected)
}

Expand Down
Expand Up @@ -3,7 +3,7 @@ package com.com.gu.sf_datalake_export.salesforce_bulk_api
import com.gu.sf_datalake_export.salesforce_bulk_api.BulkApiParams.{BatchSize, SfObjectName}
import com.gu.sf_datalake_export.salesforce_bulk_api.CreateJob
import com.gu.sf_datalake_export.salesforce_bulk_api.CreateJob.{CreateJobRequest, JobId, WireResponse}
import com.gu.util.resthttp.RestRequestMaker.{Header, PostRequest, RelativePath}
import com.gu.util.resthttp.RestRequestMaker.{Header, PostRequestWithHeaders, RelativePath}
import org.scalatest.{FlatSpec, Matchers}
import play.api.libs.json.{JsObject, JsString}

Expand All @@ -22,7 +22,7 @@ class CreateJobTest extends FlatSpec with Matchers {
"contentType" -> JsString("CSV"),
"object" -> JsString("Contact")
))
val expected = new PostRequest(expectedBody, RelativePath("/services/async/44.0/job"), List(Header("Sforce-Enable-PKChunking", "chunkSize=250000")))
val expected = new PostRequestWithHeaders(expectedBody, RelativePath("/services/async/44.0/job"), List(Header("Sforce-Enable-PKChunking", "chunkSize=250000")))
actual should be(expected)
}

Expand Down
Expand Up @@ -36,11 +36,18 @@ object JsonHttp {
val post =
HttpOpWrapper[PostRequest, StringHttpRequest, BodyAsString, JsValue](
(postRequest: PostRequest) =>
StringHttpRequest(PostMethod(BodyAsString(Json.stringify(postRequest.body))), postRequest.path, UrlParams.empty, postRequest.headers),
StringHttpRequest(PostMethod(BodyAsString(Json.stringify(postRequest.body))), postRequest.path, UrlParams.empty),

deserialiseJsonResponse
)

val postWithHeaders =
HttpOpWrapper[PostRequestWithHeaders, StringHttpRequest, BodyAsString, JsValue](
(postRequest: PostRequestWithHeaders) =>
StringHttpRequest(PostMethod(BodyAsString(Json.stringify(postRequest.body))), postRequest.path, UrlParams.empty, postRequest.headers),
deserialiseJsonResponse
)

def get = {
HttpOpWrapper[GetRequest, StringHttpRequest, BodyAsString, JsValue](
(getRequest: GetRequest) =>
Expand Down
Expand Up @@ -61,10 +61,16 @@ object RestRequestMaker extends Logging {
def apply[REQ: Writes](body: REQ, path: RelativePath): PatchRequest = new PatchRequest(Json.toJson(body), path)
}

case class PostRequest(body: JsValue, path: RelativePath, headers: List[Header])
case class PostRequest(body: JsValue, path: RelativePath)

object PostRequest {
//TODO REMOVE DEFAULT EMPTY HEADERS?
def apply[REQ: Writes](body: REQ, path: RelativePath, headers: List[Header] = List.empty): PostRequest = new PostRequest(Json.toJson(body), path, headers)
def apply[REQ: Writes](body: REQ, path: RelativePath): PostRequest = new PostRequest(Json.toJson(body), path)
}

case class PostRequestWithHeaders(body: JsValue, path: RelativePath, headers: List[Header])

object PostRequestWithHeaders {
def apply[REQ: Writes](body: REQ, path: RelativePath, headers: List[Header] = List.empty): PostRequestWithHeaders = new PostRequestWithHeaders(Json.toJson(body), path, headers)
}

case class GetRequest(path: RelativePath)
Expand Down

0 comments on commit aeb3a1b

Please sign in to comment.