0

I've created API REST collections on my project that will be called both internally (in the same PHP project) and externally (from other projects)

My question is : What is the best way to consume internal API REST ?

I've created a function to mock the call this way :

        // init mock 
        $env = Environment::mock([
            'REQUEST_METHOD'     => 'GET',
            'REQUEST_URI'        => $path,
            'QUERY_STRING'       => $urlParams,
            'HTTP_AUTHORIZATION' => 'Bearer '.$token
            ]);
        $req = Request::createFromEnvironment($env);

        // Instantiate CandidateRoute
        $app = (new $class)->get();
        $app->getContainer()['request'] = $req;
        // Run slim inst
        ob_flush();
        // Run Slim
        $response = $app->run(true);

This is working, but it's still an HTTP call in the end...

  • Any other way to consume internally API REST ?

  • Is better to call through CURL instead of mocking ?

  • Is it OK to make many HTTP calls for API REST that are in the same project ? (I guess no)

Thanks in advance!

simhamed
  • 75
  • 1
  • 7
  • I don't think you're doing a real HTTP call here...it's a mocked one, no? – Evert Nov 25 '19 at 11:52
  • Yes, but I read that we should only use mock for tests, never in production. That's why I'm trying to use Curl instead... – simhamed Nov 25 '19 at 14:02
  • I remember why I chose to go with mock in the first place : I get a '414 Request-URI Too Long' for a GET curl. The same query returns 200 OK when using mock. I cannot change apache params and I can't cast the GET into a POST, so I'm completely blocked with curl... :( – simhamed Nov 25 '19 at 15:42
  • 1
    I think using a mock-like request makes perfect sense here. Should be cheaper too. Where did you read it's not ok? – Evert Nov 26 '19 at 00:49
  • https://stackoverflow.com/a/1211795/7926959 https://stackoverflow.com/q/367308/7926959 It says mock is only for tests and not for production. There are other topics on the net if you look up for "don't mock in production". So yep, still don't know how to consume this internal API... – simhamed Nov 26 '19 at 09:02

2 Answers2

1

Based on Slim documentation : http://www.slimframework.com/docs/v3/cookbook/environment.html

Mock Environment objects are only useful when writing unit tests.

This rules out using mock on production, which means I'll have to consume internal APIs using cURL. I kept my function for PHPUnit tests.

I've decided to add a callApi() function that sends GET/POST/PUT cURL requests as if the API is external, since I couldn't find any better solution... I still have a problem with the 414 Request-URI Too Long when using GET, but that will be for another topic.

simhamed
  • 75
  • 1
  • 7
0

Edit: I think the problem here is the architecture. If you put the business login into the "controllers" then it's hard to reuse this code. Instead you could put the business logic into service classes and resuse the service with different clients like the controller, the CLI or the unit tests.

odan
  • 4,757
  • 5
  • 20
  • 49