Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
457 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
handlers/identity-backfill/src/main/scala/com/gu/identity/CreateGuestAccount.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.gu.identity | ||
|
||
import com.gu.identityBackfill.Types.EmailAddress | ||
import com.gu.identityBackfill.salesforce.UpdateSalesforceIdentityId.IdentityId | ||
import com.gu.util.resthttp.HttpOp.HttpOpWrapper | ||
import com.gu.util.resthttp.RestRequestMaker | ||
import com.gu.util.resthttp.RestRequestMaker._ | ||
import play.api.libs.json.{JsValue, Json, Reads} | ||
|
||
object CreateGuestAccount { | ||
|
||
case class WireGuestRegistrationResponse(userId: String) | ||
implicit val reads = Json.reads[WireGuestRegistrationResponse] | ||
|
||
case class WireGuestRegistrationRequest(primaryEmailAddress: String) | ||
implicit val writes = Json.writes[WireGuestRegistrationRequest] | ||
|
||
case class WireIdentityResponse(status: String, guestRegistrationRequest: WireGuestRegistrationResponse) | ||
implicit val userResponseReads: Reads[WireIdentityResponse] = Json.reads[WireIdentityResponse] | ||
|
||
def toRequest(emailAddress: EmailAddress): PostRequest = | ||
PostRequest(WireGuestRegistrationRequest(emailAddress.value), RelativePath("/guest")) | ||
|
||
def toResponse(wireGuestRegistrationResponse: WireIdentityResponse): IdentityId = | ||
IdentityId(wireGuestRegistrationResponse.guestRegistrationRequest.userId) | ||
|
||
val wrapper: HttpOpWrapper[EmailAddress, PostRequest, JsValue, IdentityId] = | ||
HttpOpWrapper[EmailAddress, PostRequest, JsValue, IdentityId](toRequest, RestRequestMaker.toResult[WireIdentityResponse](_).map(toResponse)) | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
handlers/identity-backfill/src/main/scala/com/gu/identity/IdentityClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.gu.identity | ||
|
||
import com.gu.util.config.ConfigLocation | ||
import com.gu.util.resthttp.HttpOp | ||
import com.gu.util.resthttp.JsonHttp.StringHttpRequest | ||
import com.gu.util.resthttp.RestRequestMaker.{BodyAsString, toClientFailableOp} | ||
import okhttp3.{HttpUrl, Request, Response} | ||
import play.api.libs.json.{Json, Reads} | ||
|
||
object IdentityClient { | ||
|
||
def apply( | ||
response: Request => Response, | ||
config: IdentityConfig | ||
): HttpOp[StringHttpRequest, BodyAsString] = | ||
HttpOp(response).flatMap { | ||
toClientFailableOp | ||
}.setupRequest[StringHttpRequest] { | ||
withAuth(config) | ||
} | ||
|
||
def withAuth(identityConfig: IdentityConfig)(requestInfo: StringHttpRequest): Request = { | ||
val builder = requestInfo.requestMethod.builder | ||
val authedBuilder = builder.addHeader("X-GU-ID-Client-Access-Token", s"Bearer ${identityConfig.apiToken}") | ||
val url = requestInfo.urlParams.value.foldLeft(HttpUrl.parse(identityConfig.baseUrl + requestInfo.relativePath.value).newBuilder()) { | ||
case (nextBuilder, (key, value)) => nextBuilder.addQueryParameter(key, value) | ||
}.build() | ||
authedBuilder.url(url).build() | ||
} | ||
|
||
} | ||
|
||
case class IdentityConfig( | ||
baseUrl: String, | ||
apiToken: String | ||
) | ||
|
||
object IdentityConfig { | ||
implicit val reads: Reads[IdentityConfig] = Json.reads[IdentityConfig] | ||
implicit val location = ConfigLocation[IdentityConfig](path = "identity", version = 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.