-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Requests implementation + Add implicit instance for v2_akka lookupuse…
…r HttpEndpoint
- Loading branch information
Miguel Gómez Cofrades
committed
Sep 27, 2022
1 parent
6edd3c0
commit 1a5f071
Showing
17 changed files
with
289 additions
and
28 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.habla.twitter | ||
package main | ||
|
||
import caseapp._ | ||
|
||
import scala.util.Try | ||
|
||
object MainLi extends CommandApp[Command] { | ||
|
||
|
||
def run(command: Command, rargs: RemainingArgs): Unit = | ||
command match { | ||
case cmd: LookupTweet => runLookupTweet(cmd) | ||
case cmd: LookupUser => runLookupUser(cmd) | ||
} | ||
|
||
def runLookupTweet(cmd: LookupTweet): Unit = | ||
Try(v2_requests.lookupt.Run(cmd.toLookupTweetRequest)) | ||
.fold(msg => println(msg.getMessage), println) | ||
|
||
def runLookupUser(cmd: LookupUser): Unit = { | ||
cmd.toLookupUserRequest.left.map(new Exception(_)).toTry | ||
.flatMap(req => Try(v2_requests.lookupuser.Run(req)) | ||
.map(identity)).fold(msg => println(msg.getMessage), println) | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/main/scala/dev/habla/twitter/v2_requests/HttpBody.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,15 @@ | ||
package dev.habla.twitter | ||
package v2_requests | ||
|
||
import spray.json._ | ||
import scala.util.Try | ||
|
||
trait HttpBody{ | ||
|
||
def parseBody(response: requests.Response): Either[String, JsValue] = { | ||
parseJson(response.text()) | ||
} | ||
|
||
def parseJson(body: String): Either[String, JsValue] = | ||
Try(body.parseJson).toEither.left.map(_ => body) | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/scala/dev/habla/twitter/v2_requests/HttpEndpoint.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,45 @@ | ||
package dev.habla.twitter | ||
package v2_requests | ||
|
||
|
||
import requests.RequestBlob | ||
import v2._ | ||
|
||
trait HttpEndpoint[Request]{ | ||
/* abstract interface */ | ||
|
||
type Response | ||
|
||
def from(response: requests.Response): Response | ||
|
||
def to(request: Request): requests.Request | ||
|
||
/* concrete interface */ | ||
|
||
def apply(request: Request): Response = | ||
from{ | ||
requests.get | ||
.apply(to(request), RequestBlob.EmptyRequestBlob, requests.chunkedUpload) | ||
} | ||
} | ||
|
||
object HttpEndpoint{ | ||
type Aux[Req, Res] = HttpEndpoint[Req]{type Response = Res } | ||
} | ||
|
||
trait HttpEndpointSyntax{ | ||
|
||
implicit class HttpEndpointRequestOps[Req, Res](request: Req)(implicit ep: HttpEndpoint.Aux[Req, Res]){ | ||
def single: Res = | ||
ep.apply(request) | ||
} | ||
} | ||
|
||
trait HttpEndpointInstances{ | ||
implicit val lookuptEndpoint: HttpEndpoint.Aux[lookupt.Request, lookupt.Response] = | ||
v2_requests.lookupt.Run | ||
|
||
implicit val lookupuserEndpoint: HttpEndpoint.Aux[lookupuser.Request, lookupuser.Response] = | ||
v2_requests.lookupuser.Run | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
src/main/scala/dev/habla/twitter/v2_requests/QueryParams.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,12 @@ | ||
package dev.habla.twitter | ||
package v2_requests | ||
|
||
trait QueryParams{ | ||
|
||
implicit class Params(params: Map[String, String]){ | ||
def add(name: String, value: Option[String]): Map[String, String] = | ||
value.fold(params){ v => params + ((name, v)) } | ||
def add(name: String, value: String): Map[String, String] = | ||
add(name, Some(value)) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/scala/dev/habla/twitter/v2_requests/RateLimitHeaders.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,15 @@ | ||
package dev.habla.twitter | ||
package v2_requests | ||
|
||
import scala.util.Try | ||
|
||
trait RateLimitHeaders { | ||
def parseRateLimitHeaders(response: requests.Response): Option[(Int, Long)] = { | ||
for { | ||
rateResetH <- response.headers("x-rate-limit-reset").headOption | ||
rateReset <- Try(rateResetH.toLong).toOption | ||
rateRemainingH <- response.headers("x-rate-limit-remaining").headOption | ||
rateRemaining <- Try(rateRemainingH.toInt).toOption | ||
} yield (rateRemaining, rateReset) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/scala/dev/habla/twitter/v2_requests/lookupt/From.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,38 @@ | ||
package dev.habla.twitter | ||
package v2_requests.lookupt | ||
|
||
import v2_requests._ | ||
import v2.lookupt._ | ||
import spray.json._ | ||
|
||
import scala.util.Try | ||
|
||
|
||
trait From extends HttpBody with RateLimitHeaders{ | ||
|
||
def from(response: requests.Response): Response = { | ||
val bodyE = parseBody(response) | ||
parseTweetInfo(response, bodyE) | ||
.orElse(parseRateLimitExceeded(response)) | ||
.orElse(parseErroneousTextResponse(bodyE)) | ||
.orElse(parseErroneousJsonResponse(bodyE)) | ||
.getOrElse(ErroneousTextResponse("Not a lookup response")) | ||
} | ||
|
||
def parseTweetInfo(response: requests.Response, bodyE: Either[String,JsValue]): Option[TweetInfo] = | ||
for { | ||
body <- bodyE.toOption if response.statusCode == 200 | ||
tweets <- Try(body.convertTo[TweetInfo.Body]).toOption | ||
(rateRemaining, rateReset) <- parseRateLimitHeaders(response) | ||
} yield TweetInfo(tweets, rateRemaining, rateReset) | ||
|
||
def parseRateLimitExceeded(response: requests.Response): Option[RateLimitExceeded] = | ||
if (response.statusCode != 429) None | ||
else parseRateLimitHeaders(response).map{ case (_, l) => RateLimitExceeded(l) } | ||
|
||
def parseErroneousTextResponse(body: Either[String, JsValue]): Option[ErroneousJsonResponse] = | ||
body.toOption.map(ErroneousJsonResponse) | ||
|
||
def parseErroneousJsonResponse(body: Either[String, JsValue]): Option[ErroneousTextResponse] = | ||
body.swap.toOption.map(ErroneousTextResponse) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/scala/dev/habla/twitter/v2_requests/lookupt/Run.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,9 @@ | ||
package dev.habla.twitter | ||
package v2_requests | ||
package lookupt | ||
|
||
object Run extends HttpEndpoint[v2.lookupt.Request] | ||
with From | ||
with To{ | ||
type Response = v2.lookupt.Response | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/scala/dev/habla/twitter/v2_requests/lookupt/To.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,19 @@ | ||
package dev.habla.twitter | ||
package v2_requests.lookupt | ||
|
||
import v2.lookupt.Request | ||
import v2_requests.QueryParams | ||
|
||
trait To extends QueryParams{ | ||
|
||
def to(request: Request): requests.Request = { | ||
requests.Request( | ||
url = s"https://api.twitter.com/2/tweets/${request.id}", | ||
params = Map[String, String]() | ||
.add("expansions", request.expansions) | ||
.add("tweet.fields", request.tweetFields) | ||
.add("place.fields", request.placeFields), | ||
headers = Map("Authorization" -> request.bearerToken) | ||
) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/scala/dev/habla/twitter/v2_requests/lookupuser/From.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,39 @@ | ||
package dev.habla.twitter | ||
package v2_requests | ||
package lookupuser | ||
|
||
import dev.habla.twitter.v2 | ||
import dev.habla.twitter.v2.lookupuser._ | ||
import spray.json.JsValue | ||
|
||
import scala.util.Try | ||
|
||
|
||
trait From extends HttpBody with RateLimitHeaders{ | ||
|
||
def from(response: requests.Response): Response = { | ||
val bodyE = parseBody(response) | ||
parseTweetInfo(response, bodyE) | ||
.orElse(parseRateLimitExceeded(response)) | ||
.orElse(parseErroneousTextResponse(bodyE)) | ||
.orElse(parseErroneousJsonResponse(bodyE)) | ||
.getOrElse(ErroneousTextResponse("Not a lookup response")) | ||
} | ||
|
||
def parseTweetInfo(response: requests.Response, bodyE: Either[String,JsValue]): Option[UserInfo] = | ||
for { | ||
body <- bodyE.toOption if response.statusCode == 200 | ||
users <- Try(body.convertTo[UserInfo.Body]).toOption | ||
(rateRemaining, rateReset) <- parseRateLimitHeaders(response) | ||
} yield UserInfo(users, rateRemaining, rateReset) | ||
|
||
def parseRateLimitExceeded(response: requests.Response): Option[RateLimitExceeded] = | ||
if (response.statusCode != 429) None | ||
else parseRateLimitHeaders(response).map{ case (_, l) => RateLimitExceeded(l) } | ||
|
||
def parseErroneousTextResponse(body: Either[String, JsValue]): Option[ErroneousJsonResponse] = | ||
body.toOption.map(ErroneousJsonResponse) | ||
|
||
def parseErroneousJsonResponse(body: Either[String, JsValue]): Option[ErroneousTextResponse] = | ||
body.swap.toOption.map(ErroneousTextResponse) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/scala/dev/habla/twitter/v2_requests/lookupuser/Run.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,9 @@ | ||
package dev.habla.twitter | ||
package v2_requests | ||
package lookupuser | ||
|
||
object Run extends HttpEndpoint[v2.lookupuser.Request] | ||
with From | ||
with To{ | ||
type Response = v2.lookupuser.Response | ||
} |
Oops, something went wrong.