Yii Framework Forum: Api Explorer - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Api Explorer Rate Topic: -----

#1 User is offline   edin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 09-October 10

Posted 31 August 2014 - 02:27 PM

Hi there,

I am not sure if can explain well, but i will try.
I have started to work on something similar to Google Api's explorer (https://developers.g...is-explorer/#p/) for Yii2.

Basically, I want to be able to develop set of API's organised into services and have interfece to:
- list all services
- list all service methods
- be able to execute method and display results using Xml, Json etc.

For example i would like to define service as in following sample:

<?php
// Define Sample Service
class UserService
{
    public function getServiceInfo()
    {
        return [
            'title' => "User Service",
            'description' => 'Provides api to manage users'
        ];
    }
    public function getUserByUsernameOrEmail($userNameOrEmail)
    {
        return User::find()->where("Username = :P OR Email = :P", [':P' => $userNameOrEmail])->one();
    }
    public function getUserById($id)
    {
        return User::findOne($id);
    }
    public function getSampleData(MyDataObject $data) 
    {
	return $data; 
    }	
}

// Implement Service wrapper to provide additinal funcionality such as caching, 
// logging or performance monitoring
class ServiceWrapper
{
    public  $clientClass = "";
    public  $service = "";
    public  $endPoint = "http://my-url-api/%s?method=%s&args=%s";
    private $client  = null;

    public function getClient()
    {
        if ($this->client == null) {
            $this->client = new $this->clientClass;
        }
        return $this->client;
    }

    public function __call($name, $args)
    {
        $url = sprintf($this->endPoint, $this->service, $name, json_encode($args));
        $key = "{$this->clientClass}::$name";

        Yii::beginProfile($key, "Rest Service ($url)");
        $data = Yii::$app->cache->get($url);
        if ($data === false)
        {
            $data  = file_get_contents($url); 
            Yii::$app->cache->add($url, $data, 15);
        }
        Yii::endProfile($key);
        $result = unserialize($data);
        return $result;
    }
}

//And finaly configure all together using following 
class Service
{
    private static $di = null;
    private static function getContainer()
    {
        if (self::$di == null) {
            self::setupContainer();
        }
        return self::$di;
    }

    private static function setupContainer()
    {
        $di = new Container();
        $di->set('user', [
            'class' => 'app\services\ServiceWrapper',
            'clientClass' => 'app\services\UserService'
        ]);
        self::$di = $di;
    }

    public static function setupDirectAccess()
    {
        $di = new Container();
        $di->set('user', [
            'class' => 'app\services\UserService',
        ]);
        self::$di = $di;
    }

    /**
     * Returns User Service
     * @return \app\services\UserService
     */
    public static function user()
    {
        return self::getContainer()->get("user");
    }
}

?>


I belive one of the benefits would be:
- Be able to directly use service eg. using Service::user()->getUserById(1)
- Provide REST Api for services, using various serializers such as XML, Json, Bson, PHP Serialization
- Implement service wrapper, as in sample above to provide accesing services over HTTP or TCP, avoiding direct access to dabase
- Easier performance monitoring, caching etc.
- Unify access for multiple applications (Web, iOS, Android)

So far i have implemented sample services and UI for browsing services and methods using reflection, generating form for executing methods and serializers are not yet implemented.
I am wondering if there are similar projects like this, or if there is anyone that would like to work on this.


Thanks,
Edin
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 01 September 2014 - 04:42 AM

Nice. There should be something but quick search didn't reveal anything. I've implemented something like it about 2 years ago for internal use but didn't have time to OpenSource it.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#3 User is offline   yiqing95 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 27-December 10
  • Location:china

Posted 13 May 2015 - 09:38 PM

interesting ! this is another api building method beyond the restful and the ordinary json communication !
like an rpc style .

there are some rpc implementations for mainstream languages such as : jsonRpc , hprose , Hessian ...
0

#4 User is offline   edin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 09-October 10

Posted 23 November 2015 - 02:44 PM

View Postyiqing95, on 13 May 2015 - 09:38 PM, said:

interesting ! this is another api building method beyond the restful and the ordinary json communication !
like an rpc style .

there are some rpc implementations for mainstream languages such as : jsonRpc , hprose , Hessian ...


Goal was to be able to use service directly as a regular PHP class (without networking), but still be able to expose functionality to other clients using some protocol, also be able to generate web UI for exploring API.

Btw, hprose seems to be something i could use.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users