Skip to content

Conversation

@edgardmessias
Copy link

@hiqsol
This project is still being developed

  • Easy override, you need only override Connection and QueryBuilder.
  • Support cache

Example for LibrariesIO:

class QueryBuilder extends BaseQueryBuilder {

    public function buildAuth(Request $request, $params = array()) {
        if ($this->db->apiKey) {
            $request->addData([
                'api_key' => $this->db->apiKey,
            ]);
        }
        return parent::buildAuth($request, $params);
    }

    public function buildMethod(Request $request, $action, $params = array()) {
        return $request->setMethod('GET');
    }

    public function buildFrom(Request $request, $from, $params = []) {
        if ($from === 'project') {
            $from = 'search';
        } else {
            $from .= 's';
        }

        return parent::buildFrom($request, $from, $params);
    }

    public function buildCount(Request $request, $count, $params = []) {
        if ($count) {
            $request->setMethod('HEAD');
            $request->addData(['per_page' => 1]);
            $request->on(Request::EVENT_AFTER_SEND, function (RequestEvent $event) {
                $count = $event->response->headers->get('total', 0);
                $event->response->setData((int) $count);
            });
        }

        return $request;
    }

    public function buildOrderBy(Request $request, $orderBy, $params = array()) {
        if (empty($orderBy)) {
            return $request;
        }

        foreach ($orderBy as $name => $direction) {
            $request->addData([
                'sort'  => $name,
                'order' => $direction === SORT_DESC ? 'desc' : 'asc',
            ]);
            break;
        }

        return $request;
    }

    public function buildLimit(Request $request, $limit, $offset = 0, $params = []) {
        if ($limit && $limit > 0) {
            $request->addData([
                'per_page' => $limit,
            ]);

            if ($offset && $offset > 0) {
                $request->addData([
                    'page' => 1 + $offset / $limit,
                ]);
            }
        }
        return $request;
    }

}

@hiqsol
Copy link
Member

hiqsol commented Jun 2, 2017

Have you seen yii2-hiart-httpclient? It is mentioned in README.

@edgardmessias
Copy link
Author

Yes, I saw.

My change tries to work better with Request and Response simplifying override.

@hiqsol
Copy link
Member

hiqsol commented Jun 2, 2017

These are huge changes. Not really compatible... And we are using hiart in production!
Need time to look closer.
@SilverFire, @tafid please take a look too.

@hiqsol
Copy link
Member

hiqsol commented Jun 2, 2017

First look:

  • I'm not sure about the main change, I know we're doubling a lot from yii2-httpclient but I need more time to consider and evaluate
  • we need Collection, it must be returned back
  • abstract query builder must be returned back, definitely not all apis are REST, our is not
  • also API can be non HTTP based, eg SOAP. Is yii2-httpclient compatible this way?

Such huge PR is really difficult to work with...

Let's summarize​ changes:

  • rewrite with yii2-httpclient
  • refactored Query builder
  • added caching

Please continue what I missed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants