GithubHelp home page GithubHelp logo

Reuse Endpoints. about oatpp HOT 4 CLOSED

oatpp avatar oatpp commented on May 20, 2024
Reuse Endpoints.

from oatpp.

Comments (4)

lganzzzo avatar lganzzzo commented on May 20, 2024

Hello @c-lgrant !
Thanks you for the question.

You have to create separate method for your business logic processing, and call it from your endpoint mappings.
Example:

  /**
   * Process request 
   */
  std::shared_ptr<OutgoingResponse> handleRequest(String param, String body) {
    OATPP_ASSERT_HTTP(param && body, Status::CODE_404, "param and body should not be null");
    return createResponse(Status::CODE_200, param + body);
  }

  /**
   * Add endpoint mapping
   */
  ENDPOINT("GET", "endpoint1/{param}", endpoint1, PATH(String, param), BODY_STRING(String, body)) {
    return handleRequest(param, body);
  }

  /**
   * Add endpoint mapping
   */
  ENDPOINT("GET", "endpoint2/{param}", endpoint2, PATH(String, param), BODY_STRING(String, body)) {
    return handleRequest(param, body);
  }

Best Regards,
Leonid

from oatpp.

c-lgrant avatar c-lgrant commented on May 20, 2024

Hello,

So there is no way to made two end points to one function?

In the example above, if the logic was different for each endpoint how could the handleRequest function decide which action to take.

Can you confirm if there is a better way to get the url than performing.

this->m_endpointInfo.find(__FUNCTION__).operator*().second.get()->path 

Thanks

Conor

from oatpp.

lganzzzo avatar lganzzzo commented on May 20, 2024

Hey @c-lgrant

It would be helpful, if you could describe what you are trying to achieve in a bit more details.

From what I can guess, it can be achieved in many different ways.

From the code snippet that you've posted:

this->m_endpointInfo.find(__FUNCTION__).operator*().second.get()->path

I would not recommend to do it this way, as it will be hard to maintain such code in case of endpoint name changed or path of the endpoint changed.

As a simplest example I can propose next:

  • Variant one:
  static const int ACTION_START = 0;
  static const int ACTION_STOP = 1;

  /**
   * Process request
   */
  std::shared_ptr<OutgoingResponse> handleRequest(String mapGuid, int action) {
    OATPP_ASSERT_HTTP(mapGuid, Status::CODE_404, "mapGuid should not be null");
    switch(action) {
      case ACTION_START:
        // TODO maps.start;
        break;
      case ACTION_STOP:
        // TODO maps.stop;
        break;
      default: throw std::runtime_error("unknown action");
    }
    return createResponse(Status::CODE_200, "OK");
  }

  /**
   * Add endpoint mapping
   */
  ENDPOINT("GET", "/app/maps/{mapGuid}/start", start, PATH(String, mapGuid)) {
    return handleRequest(mapGuid, ACTION_START);
  }

  /**
   * Add endpoint mapping
   */
  ENDPOINT("GET", "/app/maps/{mapGuid}/stop", stop, PATH(String, mapGuid)) {
    return handleRequest(mapGuid, ACTION_STOP);
  }
  • Variant two (better way):
    Move map-related common functionality to Map class.
  /**
   *  Inject map processing component
   */
  OATPP_COMPONENT(std::shared_ptr<Map>, m_map);

  /**
   * Add endpoint mapping
   */
  ENDPOINT("GET", "/app/maps/{mapGuid}/start", start, PATH(String, mapGuid)) {
    m_map->start(mapGuid);
    return createResponse(Status::CODE_200, "started");
  }

  /**
   * Add endpoint mapping
   */
  ENDPOINT("GET", "/app/maps/{mapGuid}/stop", stop, PATH(String, mapGuid)) {
    m_map->stop(mapGuid);
    return createResponse(Status::CODE_200, "stopped");
  }

Best Regards,
Leonid

from oatpp.

lganzzzo avatar lganzzzo commented on May 20, 2024

You may also consider to do it this way:

  ENDPOINT("GET", "/app/maps/{mapGuid}/{action}", map, 
           PATH(String, mapGuid), 
           PATH(String, action)) {

    OATPP_ASSERT_HTTP(action && (action == "start" || action == "stop"), Status::CODE_400, "Invalid Action");

    // do things based on mapGuid and action

    return createResponse(Status::CODE_200, "OK");
  }

Regards,
Leonid

from oatpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.