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

add generic type to FetchCatsBackend stub method #1889

Merged

Conversation

MirkoManojlovic
Copy link

@MirkoManojlovic MirkoManojlovic commented Jul 7, 2023

Before submitting pull request:

  • Check if the project compiles by running sbt compile
  • Verify docs compilation by running sbt compileDocs
  • Check if tests pass by running sbt test
  • Format code by running sbt scalafmt

This PR adds a generic type to stub method in FetchCatsBackend companion object.
Reasoning for this change is, when you have SttpBackend defined as a dependency like this

 val catsBackend: SttpBackend[IO, capabilities.WebSockets] = FetchCatsBackend[IO]()

it is not possible to mock it using this code

val stubedBackend: SttpBackendStub[IO, Any] = FetchCatsBackend.stub[IO]

adding a type resolves this issue 😃

val stubedBackend: SttpBackendStub[IO, WebSockets] = FetchCatsBackend.stub[IO, WebSockets]

@MirkoManojlovic MirkoManojlovic marked this pull request as ready for review July 7, 2023 09:08
@@ -48,5 +48,5 @@ object FetchCatsBackend {
*
* See [[SttpBackendStub]] for details on how to configure stub responses.
*/
def stub[F[_]: Concurrent: ContextShift]: SttpBackendStub[F, Any] = SttpBackendStub(new CatsMonadAsyncError)
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the PR - following suit for other stub backends, I think it would be sufficient to simply type this using the WebSockets capability. Then you can upcast to Any because of variance. See e.g.:

def stub[F[_]: Async]: SttpBackendStub[F, Fs2Streams[F] with WebSockets] = SttpBackendStub(implicitly)

Copy link
Author

@MirkoManojlovic MirkoManojlovic Jul 7, 2023

Choose a reason for hiding this comment

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

No no, thank you for the awesome lib, I'm just trying to give back a little 😄

For the reply, I'm not sure I get it. The docs for the second type parameter in SttpBackend say

P – Capabilities supported by this backend, in addition to sttp.capabilities.Effect. This might be Any (no special capabilities), sttp.capabilities.Streams (the ability to send and receive streaming bodies) or sttp.capabilities.WebSockets (the ability to handle websocket requests).

So should the change be, this? (then we only support Websockets? Not sure if FetchCatsBackend can support anything else?)

  def stub[F[_]: Async, WebSockets]: SttpBackendStub[F, WebSockets] = SttpBackendStub(new CatsMonadAsyncError)

or there should ne now change at all 😄

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it should be changed as you described. In general this should be the maximal set of capabilities - you can always upcast a backend to narrow the supported capabilities list. In case of cats, it's only WebSockets, no streaming is supported.

Copy link
Author

Choose a reason for hiding this comment

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

Oh okay understood, thank you.

Code changed 😄

@adamw adamw merged commit e954e04 into softwaremill:v3 Jul 8, 2023
13 checks passed
@adamw
Copy link
Member

adamw commented Jul 8, 2023

Thanks! :)

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.

2 participants