edoc automate Guide
Breadcrumbs

Use "Addons" in an app in edoc automate

In this example, you will learn how to create your own add-on using a third-party API.

The scenario is as follows:

You want to create an app in which you can call up the objects in space that are closest to Earth in the next seven days.

The publicly available NASA API (https://api.nasa.gov/) is used in the example.

The example is only a first and rough approach to demonstrate the function of the add-ons. Not all endpoints of the public NASA API are implemented in the example. You can also format the returned data as you require, e.g. select a different date format.

Here's how

  1. Create the structure for an add-on with the name NasaApi, e.g. via the WebDAV browser view.

  2. Create a new file with the name AbstractNasaApiAction.php in the lib folder of the new add-on.

  3. Edit and save the file AbstractNasaApiAction.php with the following content:

    PHP
    <?php
    
    namespace edoc\appserver\addons\nasaapi\lib;
    
    use edoc\appserver\addons\NasaApi\datasources\Nasaapi;
    
    use edoc\appserver\app\AbstractAction;
    use edoc\appserver\app\actions\DatasetAction;
    use edoc\appserver\datasources\DataSource;
    
    /**
     * @param DATASOURCE Datasource Specifies the NASA API data source.
     */
    abstract class AbstractNasaApiAction extends AbstractAction
    {
        use DatasetAction;
    
        abstract protected function initNasaApi();
        abstract protected function execNasaApi(Nasaapi $datasource);
    
        final protected function init()
        {
            $this->addParameter("Datasource", self::ACTION_TYPE_DATASOURCE);
    
            $this->initNasaApi();
    
        }
    
        final protected function exec(): AbstractAction
        {
            $dsName = $this->param('datasource');
            $result = [];
    
            try {
                $ds = DataSource::getInstance($dsName);
                //
                if ($ds instanceof Nasaapi) {
                    $result = $this->execNasaApi($ds);
                }
            } catch (\Throwable $e) {
                $this->returnError(
                    "Error in <b>Nasaapi\\GetNeoWs</b><br>\n" .
                    "<b>Message: </b><br>\n" .
                    $e->getMessage(),
                    $e->getCode());
            }
    
            return $this->returnDataset($result);
        }
    }
    
    
  4. Create a new file with the name Nasaapi.php in the datasources folder of the new add-on.

  5. Edit and save the file Nasaapi.php with the following content:

    PHP
    <?php
    
    namespace edoc\appserver\addons\NasaApi\datasources;
    
    use edoc\appserver\datasources\driver\RestAPI;
    
    class Nasaapi extends RestAPI
    {
    	public function init()
    	{
          $this->addProperty('Api-Key', 'password', '');
    	}
      
      public function details()
      {
        return "NASA API";
      }
      
      public function getNeoWs($startDate = null, $endDate = null)
      {
          $neows = [];
    
          $apiKey = $this->config['Api-Key'];
    
          if (is_null($startDate)) {
              $startDate = date("Y-m-d");
          }
          if (is_null($endDate)) {
            $endDate = date("Y-m-d", time() + (3600 * 24 * 7));
          }
    
          $result = $this->requestGet("https://api.nasa.gov/neo/rest/v1/feed?start_date={$startDate}&end_date={$endDate}&api_key={$apiKey}");
          $result = json_decode($result, true);
    
          $objects = $result["near_earth_objects"];
          foreach ($objects as $date => $dayObjects) {
              foreach ($dayObjects as $dayObject) {
    
                  $neows[] = [
                      'date' => $date,
                      'name' => $dayObject["name"],
                      'nasa_jpl_url' => $dayObject['nasa_jpl_url']
                  ];
              }
          }
          return $neows;
      }
    }
    
    
  6. Create a new file with the name GetNeoWs.php in the actions folder of the new add-on.

  7. Edit and save the file GetNeoWs.php with the following content and the corresponding description in your preferred language in which you explain the function:

    PHP
    <?php
    
    namespace edoc\appserver\addons\NasaApi\actions;
    
    use edoc\appserver\app\actions\DatasetAction;
    use edoc\appserver\addons\NasaApi\lib\AbstractNasaApiAction;
    use edoc\appserver\addons\NasaApi\datasources\Nasaapi;
    
    /**
     * GetNeoWs Action
     *
     * Action to query the nearest earth objects of the next seven days.
     *
     * @return DATASET Returns a list of the nearest object in the next seven days.
     */
    class GetNeoWs extends AbstractNasaApiAction
    {
        use DatasetAction;
        
        protected function initNasaApi()
        {
        }
        
        protected function execNasaApi(Nasaapi $ds)
        {
          return $ds->getNeoWs();
        }
    }
    
    

You can now use the data source and action in your app.