kotlin: add library response interfaces#260
Conversation
Signed-off-by: Alan Chiu <achiu@lyft.com>
Signed-off-by: Alan Chiu <achiu@lyft.com>
Signed-off-by: Alan Chiu <achiu@lyft.com>
Signed-off-by: Alan Chiu <achiu@lyft.com>
Signed-off-by: Alan Chiu <achiu@lyft.com>
Adds Swift interfaces for responses within Envoy. These should be at parity with the interfaces being added for Kotlin in #260. Resolves #118. Doc for reference: https://docs.google.com/document/d/1N0ZFJktK8m01uqqgfDRVB9mpC1iEn9dqkQaa_yMn_kE/edit#heading=h.i6ky65xaa9va Signed-off-by: Michael Rebello <mrebello@lyft.com>
library/kotlin/src/io/envoyproxy/envoymobile/ResponseBuilder.kt
Outdated
Show resolved
Hide resolved
Signed-off-by: Alan Chiu <achiu@lyft.com>
Signed-off-by: Alan Chiu <achiu@lyft.com>
|
This echoes my question on the Request interfaces, but have we thought at all about how might expose response data for streamed processing? This is standard in most HTTP library interfaces, and generally processing/handling of responses tends to be something that can make progress in an online fashion faster than bytes come over the network (i.e. you shouldn't wait to start processing until all bytes have arrived). |
|
I was sort of thinking that an Observer pattern (similar to the c-APIs, but with platform-specific facilities and sugar) would be our first-class response interface, given the flexibility it affords. |
|
Something very roughly like: |
| if (status != other.status) return false | ||
| if (body != null) { | ||
| if (other.body == null) return false | ||
| if (!body.contentEquals(other.body)) return false |
There was a problem hiding this comment.
does contentEquals not deal with null content?
There was a problem hiding this comment.
The primary issue is that we are using a ByteArray and that class doesn't have a predefined equals/hashcode method.
We can create our own byte array class/wrapper which handles this and then we'll be fine using that going forward. Since we are planning to swap this out with some input stream which is more common, I was thinking of addressing this issue then
| return true | ||
| } | ||
|
|
||
| override fun hashCode(): Int { |
There was a problem hiding this comment.
interesting there is no default hashing on user defined classes?
|
|
||
|
|
||
| /** | ||
| * A side effect function for successful Results |
| * result.fold({ response -> response.body }, { failure -> throw Exception(failure.cause) }) | ||
| * | ||
| * // To return a default value of 0 when a failure occurs | ||
| * result.fold({ response -> 1 }, { failure -> 0 }) |
There was a problem hiding this comment.
Sorry, this is my confused eyes as a kotlin ignorant, but how does the user interact with the result. Do they write complex folds where the logic is after -> for response, and failure? And they have access to the Response and the NetworkError.
There was a problem hiding this comment.
I have an example usage of this up in the description but you do have the right idea
|
I should say that I do think the Response object in here is a useful interface, and that the usage examples are elegant. But I'd like to maybe take some time to think about how it can work as part of or alongside an interface that allows application processing to proceed incrementally as data comes in. (This is possible for headers as well, but generally somewhat less important than for the body payload.) |
Definitely a great callout, and I think it's worth discussing prior to merging the Swift/Kotlin initial interface implementations. Some options @buildbreaker and I were discussing earlier, just to outline them here: The ergonomic
That said, we could also go down a path like what you suggested @goaway and essentially expose the interfaces available from C that are checked in today, and provide a bit of ergonomics on top of those (i.e., using If this is a path we want to explore, @buildbreaker and I can definitely take a stab at it |
|
@buildbreaker opened another PR with some options for response interfaces based on the discussion above: #265 I went ahead and left some comments if you want to also take a look @goaway @junr03 |
|
Going to go in a different direction: #265 |
#119
Adding a primitive Request and RequestBuilder with basic features:
Kotlin usages:
Java usages:
Tests are basic setting a builder and ensuring the result is expected. Additionally, the response builder transform creates an equivalent object when built back. These are basically testing the exact same behavior as the requests
Signed-off-by: Alan Chiu achiu@lyft.com
For an explanation of how to fill out the fields, please see the relevant section
in PULL_REQUESTS.md
Description: Add response objects
Risk Level: low
Testing: unit
Docs Changes: n/a
Release Notes: n/a
[Optional Fixes #Issue]
[Optional Deprecated:]