Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add marshal/unmarshal exception processing
- Loading branch information
1 parent
0d41dca
commit 9bd1e53
Showing
17 changed files
with
230 additions
and
190 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
10 changes: 10 additions & 0 deletions
10
api/src/main/scala/com/github/unknownnpc/psw/api/Utils.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,10 @@ | ||
package com.github.unknownnpc.psw.api | ||
|
||
object Utils { | ||
|
||
def safeParse[T](fn: => T): Either[ExternalAPIPayloadParseException, T] = { | ||
import scala.util.control.Exception.allCatch | ||
allCatch.either(fn).left.map(e => ExternalAPIPayloadParseException(cause = e)) | ||
} | ||
|
||
} |
18 changes: 10 additions & 8 deletions
18
api/src/main/scala/com/github/unknownnpc/psw/api/action/ActionContext.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 |
---|---|---|
@@ -1,20 +1,22 @@ | ||
package com.github.unknownnpc.psw.api.action | ||
|
||
import com.github.unknownnpc.psw.api.executor.Executor | ||
import com.github.unknownnpc.psw.api.{APIException, ExternalAPIException, Serializer} | ||
import com.github.unknownnpc.psw.api.{APIException, ExternalAPICallException, Serializer} | ||
|
||
import scala.util.Try | ||
import scala.util.control.Exception.allCatch | ||
|
||
trait ActionContext[IN, OUT, REQ, RES] { | ||
executor: Executor[REQ, RES] => | ||
|
||
def run(in: IN)(implicit ser: Serializer[IN, OUT, REQ, RES]): Either[APIException, OUT] = { | ||
Try(ser.fromRes( | ||
executor.execute( | ||
ser.toReq(in) | ||
) | ||
)).toEither.left | ||
.map(e => ExternalAPIException(cause = e)) | ||
|
||
for { | ||
request <- ser.toReq(in).right | ||
rawResponse <- allCatch.either(executor.execute(request)) | ||
.left.map(e => ExternalAPICallException(cause = e)).right | ||
response <- ser.fromRes(rawResponse).right | ||
} yield response | ||
|
||
} | ||
|
||
} |
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
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
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
24 changes: 14 additions & 10 deletions
24
...main/scala/com/github/unknownnpc/psw/qiwi/serializer/AccountBalanceReqResSerializer.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 |
---|---|---|
@@ -1,29 +1,33 @@ | ||
package com.github.unknownnpc.psw.qiwi.serializer | ||
|
||
import com.github.unknownnpc.psw.api.Serializer | ||
import com.github.unknownnpc.psw.api.Utils.safeParse | ||
import com.github.unknownnpc.psw.api.{ExternalAPIPayloadParseException, Serializer} | ||
import com.github.unknownnpc.psw.qiwi.model.{AccountBalanceRequest, AccountBalanceResponse} | ||
import org.apache.http.client.methods.HttpGet | ||
|
||
private[serializer] class AccountBalanceReqResSerializer extends Serializer[AccountBalanceRequest, AccountBalanceResponse, HttpGet, String] { | ||
|
||
private val urlTarget: String = "https://edge.qiwi.com/funding-sources/v2/persons/%s/accounts" | ||
|
||
override def toReq(req: AccountBalanceRequest): HttpGet = { | ||
val fullRequestUrl = String.format(urlTarget, req.wallet) | ||
val httpGet = new HttpGet(fullRequestUrl) | ||
httpGet.setHeader("Authorization", "Bearer " + req.apiToken) | ||
httpGet.setHeader("Accept", "application/json") | ||
override def toReq(req: AccountBalanceRequest): Either[ExternalAPIPayloadParseException, HttpGet] = { | ||
safeParse { | ||
val fullRequestUrl = String.format(urlTarget, req.wallet) | ||
val httpGet = new HttpGet(fullRequestUrl) | ||
httpGet.setHeader("Authorization", "Bearer " + req.apiToken) | ||
httpGet.setHeader("Accept", "application/json") | ||
|
||
httpGet | ||
httpGet | ||
} | ||
} | ||
|
||
override def fromRes(out: String): AccountBalanceResponse = { | ||
override def fromRes(out: String): Either[ExternalAPIPayloadParseException, AccountBalanceResponse] = { | ||
import org.json4s._ | ||
import org.json4s.native.Serialization | ||
import org.json4s.native.Serialization.read | ||
implicit val formats = Serialization.formats(NoTypeHints) | ||
|
||
read[AccountBalanceResponse](out) | ||
safeParse { | ||
read[AccountBalanceResponse](out) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.