-
Notifications
You must be signed in to change notification settings - Fork 412
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
added cookie helpers #2439
Changes from 1 commit
64e128c
3179ba6
47a0fb4
8c99c0a
854557c
b85d635
1c8f53b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -19,7 +19,7 @@ package zio.http | |||||||||||
import java.net.InetAddress | ||||||||||||
|
||||||||||||
import zio.stacktracer.TracingImplicits.disableAutoTrace | ||||||||||||
import zio.{Trace, ZIO} | ||||||||||||
import zio.{NonEmptyChunk, Trace, ZIO} | ||||||||||||
|
||||||||||||
import zio.http.internal.HeaderOps | ||||||||||||
|
||||||||||||
|
@@ -106,6 +106,39 @@ final case class Request( | |||||||||||
*/ | ||||||||||||
def unnest(prefix: Path): Request = | ||||||||||||
copy(url = self.url.copy(path = self.url.path.unnest(prefix))) | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Returns the cookie with the given name if it exists. | ||||||||||||
*/ | ||||||||||||
def cookie(name: String): Option[Cookie] = | ||||||||||||
cookies.flatMap(_.filter(_.name == name).headOption) | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* 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] = | ||||||||||||
cookieWithOrFailImpl(name)(identity)(f) | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Uses the cookie with the given name if it exists and runs `f` afterwards. | ||||||||||||
* | ||||||||||||
* 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] = | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||||||||
cookieWithOrFailImpl(name)(_ => e)(f) | ||||||||||||
|
||||||||||||
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) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Suggested change
where e is not a function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added your suggestions, thank you! |
||||||||||||
|
||||||||||||
/** | ||||||||||||
* Returns all cookies from the request. | ||||||||||||
*/ | ||||||||||||
def cookies: Option[NonEmptyChunk[Cookie]] = | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||||||||
header(Header.Cookie).map(_.value) | ||||||||||||
|
||||||||||||
} | ||||||||||||
|
||||||||||||
object Request { | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cookieWithZIO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done