[New Generator] Rust API client generator#6092
Conversation
|
I'll add a few comments on the specifics inline |
| @@ -0,0 +1,17 @@ | |||
| [package] | |||
| name = "petstore-client" | |||
There was a problem hiding this comment.
these settings would be exposed as flags.
| @@ -0,0 +1,44 @@ | |||
| extern crate petstore_client; | |||
There was a problem hiding this comment.
a sample file, not to be part of generated api
| use super::models; | ||
| use super::Error; | ||
|
|
||
| pub fn add_pet<C: hyper::client::Connect>( |
There was a problem hiding this comment.
rust naming guidelines dictate snake_case in function names
| use super::Error; | ||
|
|
||
| pub fn add_pet<C: hyper::client::Connect>( | ||
| prefix: &str, |
There was a problem hiding this comment.
I'm not sure about what's the common pattern for api use, really. This sample client will require two things: a http (or https) prefix as a string and the hyper.rs Client (the web client) plus any arguments.
| let mut req = hyper::Request::new( | ||
| hyper::Method::Post, | ||
| format!("{}/pet", prefix).parse().unwrap()); | ||
| let serialized = serde_json::to_string(pet).unwrap(); |
There was a problem hiding this comment.
no error handling for brevity of the example.
| Box::new( | ||
| cli.request(req).and_then(|res| { res.body().concat2() }) | ||
| .map_err(|e| Error::from(e)) | ||
| .and_then(|_| futures::future::ok(())) |
There was a problem hiding this comment.
the api doesn't dictate the return value so nothing is returned even though the sample server returns a pet instance.
npm server choked up on {"id":1337,"category":null,"name":"barker","photoUrls":[],"tags":[],"status":null} here, http://petstore.swagger.io worked fine.
|
|
||
| use super::models; | ||
|
|
||
| mod get_pet_by_id_api; |
There was a problem hiding this comment.
there's a single namespace, so modules (and files) would have to be named differently from the actual functions.
| id: Option<i64>, | ||
| category: Option<super::Category>, | ||
| name: String, | ||
| #[serde(rename = "photoUrls")] photo_urls: Vec<String>, |
There was a problem hiding this comment.
again, snake case here, and renamed fields need an annotation.
|
@farcaller thanks for the Rust Petstore sample, I'll start the reverse engineering process tomorrow and let you know if I've any question. |
|
I poked more around the go generated code and brought the example to the similar level of api. The changes, basically, are that there's an APIClient now, that incapsulates access to the API, and a dedicated pet_api.rs that's supposedly implements all the Pet apis. This extension fully builds on the previous example, though, so it would be fine if the generator produced the functional API as in original commit, and I can tackle the object API later myself when I'll have a better understanding of swagger internals. |
|
Quick question: would it be possible to group |
|
Absolutely. I'll update the PR |
|
@farcaller I've submitted the initial version via #6105 Please comment on the code in that PR and we'll fix the remaining issues one by one. |
| base64 = "*" | ||
| futures = "*" | ||
| hyper = "*" | ||
| url = "*" |
There was a problem hiding this comment.
Would you mind annotating which dependency version is the codegen targeting?
There was a problem hiding this comment.
yep, as soon as I figure those myself :-) so far the generated code doesn't compile
| prefix: &str, | ||
| cli: &hyper::client::Client<C>, | ||
| pet_id: i64, | ||
| ) -> Box<Future<Item = models::Pet, Error = Error>> { |
There was a problem hiding this comment.
(Here and in other generated methods)
Would you consider returning a more complex type? The Item here is losing any additional information about the response (header, status, etc) which I've found useful in some situations (e.g. token authenticated endpoints redirecting to token provider). For reference, reqwest does this with a richer Response struct + trait.
There was a problem hiding this comment.
For other generators (e.g. PHP, Ruby), there's a corresponding "WithHttpInfo" method (e.g. getPetByIdWithHttpInfo) to return a list with the response (if any), HTTP status code and HTTP headers.
Not sure if Rust supports returning a list. If not, we will need to create an ApiResponse object with response, status and headers as properties.
For Rust, we can follow similar approach by creating a corresponding WithHttpInfo method or we can default the method (e.g. getPetById) to return the response together with status code and headers.
There was a problem hiding this comment.
I think we can return response object along with the parsed body. Let's focus on making the current implementation compileable and then we can improve.
There was a problem hiding this comment.
It's also worth sharing other API clients usually come with a corresponding "Async" method (e.g. addPetAsync) for async function call. Not sure how that should be done in Rust but worth thinking ahead with this future requirement.
There was a problem hiding this comment.
All this code is async-only. I don't think that it makes sense to have a matching sync version.
There was a problem hiding this comment.
Ah ok. Good to know. One less thing to consider then.
|
Merging this (Rust Petstore sample) into master first similar to what we've done for PowerShell and other generators that follow similar approach so as to give credits back to the contributor. For further discussion on the Rust generator, please continue in #6105, which is work-in-progress. |
PR checklist
./bin/to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.shand./bin/security/{LANG}-petstore.shif updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\.3.0.0branch for breaking (non-backward compatible) changes.Description of the PR
Tracking PR for Rust client implementation.
The first commit adds a subset of apis requested in #5905.