Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Allow custom request timeouts to be passed to OkHttp client (#86) #134

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

finn-block
Copy link
Member

Allows customizing the OkHttpClient's request timeout. The default is still to not have any timeout.

Copy link

codecov bot commented Jan 16, 2024

Codecov Report

Merging #134 (ce9307c) into main (a433d95) will decrease coverage by 0.37%.
Report is 9 commits behind head on main.
The diff coverage is 50.00%.

❗ Current head ce9307c differs from pull request most recent head d50f535. Consider uploading reports for the commit d50f535 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #134      +/-   ##
==========================================
- Coverage   78.64%   78.27%   -0.37%     
==========================================
  Files          30       30              
  Lines         693      672      -21     
  Branches       65       65              
==========================================
- Hits          545      526      -19     
+ Misses        114      112       -2     
  Partials       34       34              
Components Coverage Δ
protocol 85.53% <ø> (+0.43%) ⬆️
httpclient 82.14% <50.00%> (-0.84%) ⬇️


/**
* A client of the tbDEX HTTP interface for communicating with a PFI.
*/
object TbdexHttpClient {
private val client = OkHttpClient()
private var client = OkHttpClient()
Copy link
Contributor

Choose a reason for hiding this comment

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

So, we originally made TbdexHttpClient an object because we didn't think people needed to instantiate it/one would be enough. However, if we're making the timeout customizable, I think we could make it a normal class, and leave OkHttpClient as val. So effectively you could pass in the timeout as an argument to TbdexHttpClient's constructor, which would set the timeout on OkHttpClient.

The reason for doing this is that it isn't immediately clear when you call setTimeout that you are constructing a new OkHttpClient every time. (Builder.build returns a new instance of the client).

Copy link
Contributor

Choose a reason for hiding this comment

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

Check out this section about init blocks for how you can use arguments from the constructor when creating a new instance of a class: https://kotlinlang.org/docs/classes.html#constructors

Copy link
Contributor

@phoebe-lew phoebe-lew left a comment

Choose a reason for hiding this comment

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

Maybe we should default to 10s or something!

@finn-block
Copy link
Member Author

since okhttp has default timeouts IMO we should just leave them unless we have some reason to change them. It might make sense to document the existence of the default timeouts from the library

Copy link
Contributor

@mistermoe mistermoe left a comment

Choose a reason for hiding this comment

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

Is there a way to pass timeouts per method vs as a constructor arg?

@mistermoe
Copy link
Contributor

cc: @angiejones

This would be a breaking change if yall have guides using the http client

@finn-block
Copy link
Member Author

finn-block commented Feb 2, 2024

Easy enough to add an optional param to all the methods with a timeout option. Should that a replace the constructor argument i've got currently? or just another way to do set the timeout (presumably overriding the constructor arg).

As for this being a breaking change: Maybe it's my unfamiliarity with Kotlin but I thought by providing a secondary constructor with no arguments it would make existing code work exactly how it currently does:

constructor() : this(Duration.ZERO)

@phoebe-lew
Copy link
Contributor

@mistermoe what's the reasoning for wanting this per-method? timeouts are configured at a client instance level, so if we want the timeout to be different on every call, we essentially need to reinstantiate the client every time.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants