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

PathHandler for /:id style API #157

Open
antoinewaugh opened this issue Aug 12, 2021 · 0 comments
Open

PathHandler for /:id style API #157

antoinewaugh opened this issue Aug 12, 2021 · 0 comments

Comments

@antoinewaugh
Copy link

antoinewaugh commented Aug 12, 2021

Hello,

I wish to follow the convention for my REST interface:

http://xxx.xxx.xxx.xxx:8080/resources/:id

Where GET http://xxx.xxx.xxx.xxx:8080/resources will return a list of resources, GET http://xxx.xxx.xxx.xxx:8080/resources/:id will return a single resource with id == :id.

Given id is part of the URI, my approach thus far has been to have a single PathHandler on resources, and inspect the CrackedURI::path()[0] element to infer if an id has been passed.

Is there a more elegant way of achieving this?

std::shared_ptr<seasocks::Response> ResourceEndpoint::handle(const CrackedUri &uri, const Request &request) {
 if (uri.path().size() == 1) {
    const auto base = uri.path()[0];
    if (base == "") {                                   
      switch (request.verb()) {
        case Request::Verb::Get: // return all resources
        ...
      }
    } else {
      try {
        const auto id = static_cast<uint32_t>(std::stol(base));   // return resource for 
        switch (request.verb()) {
        case Request::Verb::Get: // return resource for 'id' only
        ...
        }
      }
   }
}
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

1 participant