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

Help with a strange API #13

Open
SirVer opened this issue Aug 5, 2017 · 4 comments
Open

Help with a strange API #13

SirVer opened this issue Aug 5, 2017 · 4 comments

Comments

@SirVer
Copy link

SirVer commented Aug 5, 2017

I am trying to wrap createComment from the LaunchPad API - search for createComment on the page as the method has no direct link.

This looks like this in my code:

fn create_comment_by_url(&self, link: &str, subject: &str, content: &str) {
    POST("{}", link);
    fields! { 
        "ws.op" => "createComment",
        "subject" => subject,
        "content" => content
    }
}

With link being a merge_proposal.self_link.

This does work, however the API states:

On success, the response status will be 201 and the Location header will contain the link to the newly created code_review_comment.

I would like to expose this returned link in my API wrapper, but found no way of extracting a header from the response. Could you give me a hint?

@abonander
Copy link
Owner

abonander commented Aug 8, 2017

Then you would use anterofit::net::RawResponse as your service method return type:

fn create_comment_by_url(&self, link: &str, subject: &str, content: &str) -> RawResponse {
    POST("{}", link);
    fields! { 
        "ws.op" => "createComment",
        "subject" => subject,
        "content" => content
    }
}

and then the response containing the headers is within.

If you have a type to decode and you also want to inspect the response, you can use anterofit::net::response::WithRaw, or anterofit::net::response::TryWithRaw if you want to inspect the response even if there was an error decoding the body.

@abonander
Copy link
Owner

However, if you want to abstract over this within the service definition alone, I don't really have anything for that yet. I've been conceptualizing a sort of map_response!{} macro, but I don't have a prototype for that yet.

@SirVer
Copy link
Author

SirVer commented Aug 8, 2017

Thanks for your comments, that is really helpful. An example would have helped in the code base, since the WithRaw types are hard to find in the docs.

I take your last comment that you mean that a wrapper class to abstract the service a bit more idiomatic is required to hide the WithRaw handling. I agree it would be nice if such a wrapper was not needed, but frankly most returns from Web apis will require some sort of higher level abstraction anyways, so it does not hurt a lot.

@abonander
Copy link
Owner

The goal is to be able to abstract a lot of the weirdness inside the service definition but I'm not completely sure how I want to do that.

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

No branches or pull requests

2 participants