Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add 'Accept' to headers #30

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions TooGoodToGo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class TooGoodToGo(http: Backend[IO])(using log: Logger[IO]):
def getItems(cache: CacheService, config: TgtgConfig) =
def retrieveItems(access: AccessToken) =
headers()
.flatMap(ua =>
.flatMap(baseHeaders =>
basicRequest
.body(GetItemsRequest(config.userId))
.post(itemsEndpoint)
.cookies(access.cookies.toSeq*)
.headers(ua)
.headers(baseHeaders*)
.auth
.bearer(access.access_token)
.response(asJson[GetItemsResponse])
Expand All @@ -43,12 +43,12 @@ class TooGoodToGo(http: Backend[IO])(using log: Logger[IO]):

def getAccessToken(cache: CacheService, config: TgtgConfig): IO[AccessToken] =
val action = headers()
.flatMap: ua =>
.flatMap: baseHeaders =>
basicRequest
.body(RefreshRequest(config.refreshToken))
.post(refreshEndpoint)
.response(asJson[RefreshResponse])
.headers(ua)
.headers(baseHeaders*)
.responseGetRight
.send(http)
.map(r =>
Expand All @@ -66,12 +66,12 @@ class TooGoodToGo(http: Backend[IO])(using log: Logger[IO]):
end getAccessToken

def getCredentials(email: Email): IO[TgtgConfig] =
headers().flatMap: ua =>
headers().flatMap: baseHeaders =>
basicRequest
.body(LoginRequest(email))
.post(loginEndpoint)
.response(asJson[LoginResponse])
.headers(ua)
.headers(baseHeaders*)
.responseGetRight
.send(http)
.flatMap: r =>
Expand All @@ -91,7 +91,7 @@ class TooGoodToGo(http: Backend[IO])(using log: Logger[IO]):
.body(PollRequest(email, pollId))
.post(authPollEndpoint)
.response(asJson[Option[PollResponse]])
.headers(ua)
.headers(baseHeaders*)
.responseGetRight
.send(http)
.logTimed("poll")
Expand All @@ -116,14 +116,15 @@ class TooGoodToGo(http: Backend[IO])(using log: Logger[IO]):
case StatusCode.TooManyRequests => IO.raiseError(new Exception("Too many requests. Try again later."))
case _: StatusCode => IO.raiseError(new Exception(show"Unexpected response: ${r.show()}"))

private def headers(): IO[Map[String, String]] = Random
private def headers(): IO[Seq[Header]] = Random
.scalaUtilRandom[IO]
.flatMap(_.betweenInt(0, userAgents.length))
.map(i =>
Map(
HeaderNames.AcceptEncoding -> "gzip",
HeaderNames.AcceptLanguage -> "en-GB",
HeaderNames.UserAgent -> userAgents(i)
Seq(
Header.accept(MediaType.ApplicationJson),
Header.acceptEncoding("gzip"),
Header(HeaderNames.AcceptLanguage, "en-GB"),
Header.userAgent(userAgents(i))
)
)

Expand Down