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

added cookie helpers #2439

Merged
merged 7 commits into from
Sep 27, 2023
Merged

added cookie helpers #2439

merged 7 commits into from
Sep 27, 2023

Conversation

TomTriple
Copy link
Contributor

@TomTriple TomTriple commented Sep 8, 2023

If the operators are ok I'll send another PR with updated docs

@codecov-commenter
Copy link

codecov-commenter commented Sep 8, 2023

Codecov Report

Patch coverage: 87.69% and project coverage change: +0.48% 🎉

Comparison is base (1408f9c) 64.28% compared to head (854557c) 64.76%.
Report is 2 commits behind head on main.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2439      +/-   ##
==========================================
+ Coverage   64.28%   64.76%   +0.48%     
==========================================
  Files         135      135              
  Lines        7101     7133      +32     
  Branches     1211     1200      -11     
==========================================
+ Hits         4565     4620      +55     
+ Misses       2536     2513      -23     
Files Changed Coverage Δ
zio-http/src/main/scala/zio/http/Request.scala 40.00% <0.00%> (-3.91%) ⬇️
zio-http/src/main/scala/zio/http/Header.scala 63.46% <94.54%> (+2.09%) ⬆️
zio-http/src/main/scala/zio/http/MediaType.scala 96.55% <100.00%> (+0.12%) ⬆️
.../src/main/scala/zio/http/codec/RichTextCodec.scala 88.58% <100.00%> (+4.52%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@TomTriple
Copy link
Contributor Author

zio.http.ClientHttpsSpec seems flaky - running it locally works

@TomTriple TomTriple force-pushed the add-cookie-helper branch 2 times, most recently from 69eb79f to 1de88cb Compare September 10, 2023 13:46
@TomTriple TomTriple changed the title add cookie helper added cookie helpers Sep 10, 2023
/**
* Uses the cookie with the given name if it exists and runs `f` afterwards.
*/
def cookieWith[R, A](name: String)(f: Cookie => ZIO[R, Throwable, A])(implicit trace: Trace): ZIO[R, Throwable, A] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cookieWithZIO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/**
* Returns all cookies from the request.
*/
def cookies: Option[NonEmptyChunk[Cookie]] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def cookies: Option[NonEmptyChunk[Cookie]] =
def cookies: Chunk[Cookie] =

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* Also, you can replace a `NoSuchElementException` from an absent cookie with
* `E`.
*/
def cookieWithOrFail[R, E, A](name: String)(e: E)(f: Cookie => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def cookieWithOrFail[R, E, A](name: String)(e: E)(f: Cookie => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
def cookieWithOrFail[R, E, A](name: String)(missingCookieError: E)(f: Cookie => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@TomTriple
Copy link
Contributor Author

I added all your suggestions!

private def cookieWithOrFailImpl[R, E, A](name: String)(e: Throwable => E)(f: Cookie => ZIO[R, E, A])(implicit
trace: Trace,
): ZIO[R, E, A] =
ZIO.getOrFailWith(e(new java.util.NoSuchElementException(s"cookie doesn't exist: $name")))(cookie(name)).flatMap(f)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will create an exception with stacktrace just to be thrown away immediately. This does not seem to be efficient.
I'd change it just simply to

Suggested change
ZIO.getOrFailWith(e(new java.util.NoSuchElementException(s"cookie doesn't exist: $name")))(cookie(name)).flatMap(f)
cookie(name) match {
case Some(cookie) => f(e)
case None => ZIO.fail(e)
}

where e is not a function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added your suggestions, thank you!

@jdegoes jdegoes merged commit 588ab4f into zio:main Sep 27, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants