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
Generic http/httpAsync DSL #189
Generic http/httpAsync DSL #189
Conversation
…amic HttpContext or HttpMethod
@rybalkinsd This feature was in my opinion quite challenging. It works but I don't think it is the most clever solution. What do you think? I still need to add all the unit test, javadoc and do some additional clean-up. |
Codecov Report
@@ Coverage Diff @@
## master #189 +/- ##
============================================
- Coverage 91.50% 91.28% -0.22%
Complexity 127 127
============================================
Files 41 44 +3
Lines 400 413 +13
Branches 49 52 +3
============================================
+ Hits 366 377 +11
Misses 9 9
- Partials 25 27 +2
Continue to review full report at Codecov.
|
Wow! I also expected it to be more challenging. I like the API
A couple of things to think about:
@IVSivak could you please share your thoughts? |
Sure, I will check if it is possible to combine the method and the context with generics. By the way I have the feeling that HttpContextUtil is a bit over complex, it is in my opinion not very readable. What do you guys think? Do you think there is a better way with plain kotlin to have something like that working? |
It looks like a bit complicated, imo. But yeah, as I mentioned, maybe it's possible to resolve this using types. Another option to consider is to make |
I refactored a bit and the resulting code is simplified compared to the initial commit. Functionality wise it is exact the same as the previous commit, the only difference is that with this commit a http method is required when calling the http function. The flows will be: For Get request
For Post request
For Delete request
etc... I also wanted to check the type of What do you think of the code change and of the flows? Is it ok to throw a class cast exception when someone is being funny by passing a HttpPostContext while executing a Get request? |
I like the simplified version! I was thinking about when it's possible to use exactly same request with any request method.
It will only allow to use the context initialisers applicable to any context.
Another opportunity here is to support methods with body (inherited from |
It looks like I need to learn a lot about Kotlin, it is quite different compared to Java. I didn't know that an existing enum could be enriched with a function outside the enum declaration. Awesome! I applied your feedback, but when I remove the generic type Your other option regarding supporting method with body by inheritance from HttpPostContext gives me the idea that it is the best option to move from here on, but looking at OOP aren't we misusing HttpPostContext? What do you think of moving the body property to HttpContext (the base class) and disable it at the child classes which don't have a body, such as the HttpGetContext. |
I Added both cases mentioned within my precious comment as two separate commits: Cases:
As a mentioned earlier, I couldn't remove the generic type within the http function for case 1 as it fail to compile wen passing a sub class of HttpContext. For example if I want to make a http post request with a body. I am forced to use the sub class HttpPostContext because the body isn't available within the base class. But without the generics I am not able to pass HttpPostContext. I am curious what you think of the both cases. |
We tried it once in a while. Users are getting frustrated :) So I would try to avoid it.
It looks good for me!
Imagine our use case: we have an identical request and we want time to time use POST or GET to for it. I would go with a code like
In this case the only possible |
kohttp-test/src/test/kotlin/io/github/rybalkinsd/kohttp/dsl/HttpDslKtTest.kt
Outdated
Show resolved
Hide resolved
… HEAD and GET request" This reverts commit 92cd43a
Thanks again for the feedback!
The reason was to support child classes of HttpContext such as HttPostContext and HttpGetContext. I removed the generic type T as you already mentioned that it is fine for the initial version to only support the base class HttpContext. I dropped case number 2 and refactored it. I have tried to finalize it. Added unit tests and updated the documentation. Let me know if you are ok with the changes. By the way I also added unit tests for all the async http requests, but somehow CodeCov isn't recognises it |
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.
Looks amazing! And it's a huge amount of work done!
I's supper happy with this pull request, there is just a couple of things to clean before merging.
- Please have a look at inline comments
- I see new
/dsl/async/
test, that's great we'll have them, but let's move them to another Pull Request to simplify history. - Let's also move typo/grammar/import fixes(like ClientBuilder.kt or HttpPutDsl.kt) in one another PR.
I'll be happy to merge all 3 (or more if you would go for) Pull Request as soon as possible!
* @see HttpContext | ||
* @see Method | ||
* | ||
* @since 0.11.2 |
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.
Let's put 0.12.0
for all new API features!
kohttp/src/main/kotlin/io/github/rybalkinsd/kohttp/dsl/async/HttpAsyncDsl.kt
Outdated
Show resolved
Hide resolved
kohttp/src/main/kotlin/io/github/rybalkinsd/kohttp/dsl/context/HttpContext.kt
Outdated
Show resolved
Hide resolved
kohttp/src/main/kotlin/io/github/rybalkinsd/kohttp/dsl/httpDsl.kt
Outdated
Show resolved
Hide resolved
* @since 0.11.2 | ||
* @author hakky54 | ||
*/ | ||
fun http(client: Call.Factory = defaultHttpClient, method: Method, init: HttpContext.() -> Unit): Response { |
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.
Let's use a new line for each parameter, as in httpAsync
kohttp/src/main/kotlin/io/github/rybalkinsd/kohttp/util/HttpContextUtil.kt
Outdated
Show resolved
Hide resolved
|
||
internal fun Method.makeHttpContext(): HttpContext { | ||
return when (this) { | ||
Method.GET -> HttpGetContext() |
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.
Let's import Method to use them like Post -> HttpPostContext()
No worries at all, I'll check codecov later this week |
…ntextUtil.kt Co-authored-by: Sergei Rybalkin <yan.brikl@gmail.com>
Co-authored-by: Sergei Rybalkin <yan.brikl@gmail.com>
…/HttpContext.kt Co-authored-by: Sergei Rybalkin <yan.brikl@gmail.com>
…ttpAsyncDsl.kt Co-authored-by: Sergei Rybalkin <yan.brikl@gmail.com>
It looks like you are enjoying all these changes 😄 |
Any idea when you will be releasing by the way? |
Want to put |
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.
Hehehe, firing all the commits to make this baby ready to ship. It think I applied all your remarks. I will also push the other branches |
This is a pull request to enable dynamic http request, see issue mentioned here: #169
Summary:
With the current version the end-user needs to explicitly specify which http context to use for each http method type, see here code snippet from the issue #169:
Provided solution
Within this pull request a http request can be executed by either specifying the http method or http context. The underlying function will determine during runtime which http context to use by the provided arguments.
The above snippet can be rewritten with the following snippet:
Note
This pull request is currently WIP to get review input on the initial solution. Some unit tests are already added to provide example usages.