Joe Beeker
Joe Beeker

Reputation: 11

Where to put calls to 3rd party APIs in Apigility/ZF2?

I have just completed my first API in Apigility. Right now it is basically a gateway to a database, storing and retrieving multi-page documents uploaded through an app.

Now I want to run some processing on the documents, e.g. process them through a 3rd party API or modify the image quality etc., and return them to the app users.

  1. Where (in which class) do I generally put such logic? My first reflex would be to implement such logic in the Resource-Classes. However I feel that they will become quite messy, obstructing a clear view on the API's interface in the code and creating a dependency on a foreign API. Also, I feel limited because each method corresponds to an API call.
  2. What if there is a certain processing/computing time? Meaning I cannot direct respond the result through a GET request. I thought about running an asynchronous process and send a push notification to the app, once the processing is complete. But again, where in the could would I ideally implement such processing logic?

I would be very happy to receive some architectural advice from someone who is more seasoned in developing APIs. Thank you.

Upvotes: 0

Views: 216

Answers (1)

Wilt
Wilt

Reputation: 44326

  1. You are able to use the zf-rest resource events to connect listeners with your additional custom logic without polluting your resources.
    These events are fired in the RestController class (for example a post.create event here on line 382).

    When you use Apigility-Doctrine module you can also use the events triggered in the DoctrineResource class (for example the DoctrineResourceEvent::EVENT_CREATE_POST event here on line 361) to connect your listeners.

  2. You can use a queueing service like ZendQueue or something (a third party module) built on top of ZendQueue for managing that. You can find different ZF2 queueing systems/modules using Google.

    By injecting the queueing service into your listener you can simply push your jobs directly into your queue.

Upvotes: 1

Related Questions