2017 © Pedro Peláez
 

library apify

A pretty library to help developers build RESTful APIs lightly, quickly and properly even without writing code

image

megaads/apify

A pretty library to help developers build RESTful APIs lightly, quickly and properly even without writing code

  • Saturday, July 21, 2018
  • by megaads-vn
  • Repository
  • 9 Watchers
  • 12 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 16 Versions
  • -62 % Grown

The README.md

Apify

A pretty library to help developers build RESTful APIs lightly, quickly and properly even without writing code., (*1)

It's always easy to customize to suit any need such as defining data relationships, authorization, caching, communicating or integrating into other systems., (*2)

Features

  • Serves RESTful APIs for any MySql database
    • Pagination
    • Sorting
    • Selection
    • Grouping, Having
    • Filtering
    • Relationships
    • Metadata
  • Supports Event Bus

Using Apify

  • Apify client for PHP: https://github.com/megaads-vn/apify-client-php
  • Use HTTP clients like Postman to invoke RESTful API calls.
  • Combine with API Gateway is also recommended to build a completely development environment for microservice.

Installation

Apify is packed as a composer package. So it's installed quickly in 2 steps 1. Require the composer package, (*3)

`composer require megaads/apify`
  1. Register the provider:, (*4)

    Megaads\Apify\ApifyServiceProvider, (*5)

System requirements

  • PHP: >= 5.6
  • Laravel/ Lumen Framework: 5.4.*
  • MySQL
  • Message queue server: optional

API Overview

HTTP Method API URL Description For example
GET /api/entity List all records of table that match the query curl http://my-api.com/api/user?filters=age>20
GET /api/entity/:id Retrieve a record by primary key :id curl http://my-api.com/api/user/123
POST /api/entity Insert a new record, bulk inserting is also avaiable curl -X POST http://my-api.com/api/user -d '[{"username":"user1", "age":"20"},{"username":"user2", "age":"25"}]' -H "Content-Type: application/json"
PUT /api/entity/:id Replaces existed record with new one curl -X PUT http://my-api.com/api/user/123 -d '{"id":"123", "username":"user1", "age":"20"}' -H "Content-Type: application/json"
PATCH /api/entity/:id Update record element by primary key curl -X PATCH http://my-api.com/api/user/123 -d '{"age":"21"}' -H "Content-Type: application/json"
DELETE /api/entity/:id Delete a record by primary key curl -X DELETE http://my-api.com/api/user/123
DELETE /api/entity Delete bulk records that match the query curl -X DELETE http://my-api.com/api/user?filters=age>100
POST /api/upload Upload a file curl -X POST http://my-api.com/api/upload -H "Content-Type: multipart/form-data" -F "data=@song.mp3"

Pagination

Parameter Required Default Description
page_id No 0 Page index, start at 0
page_size No 50 Number of rows to retrieve per page
/api/post?page_id=2&page_size=20

Sorting

Order by multiple columns using sorts parameter, (*6)

Sort ascending

/api/post?sorts=user_id

Sort descending

/api/post?sorts=-created_at

Sort by multiple columns

/api/post?sorts=user_id,-created_at

Selection

Select columns from the records using fields parameter. SQL aggregate functions such as COUNT, MAX, MIN, SUM, AVG, SQL aliases are also available, (*7)

/api/post?fields=id,content,user_id,sum(view_count) as view_sum

Group By

Group the result-set by one or more columns using groups parameter and combine with aggregate functions using Selection, (*8)

/api/post?fields=user_id,sum(view_count)&groups=user_id

Filtering

Operator Condition For example
= Equal to /api/post?filters=user_id=1
!= Not equal /api/post?filters=user_id!=1
> Greater /api/post?filters=user_id>1
>= Greater or equal /api/post?filters=user_id>=1
< Less /api/post?filters=user_id<1
<= Less or equal /api/post?filters=user_id<=1
={} In /api/post?filters=user_id={1;2;3}
!={} Not in /api/post?filters=user_id!={1;2;3}
=[] Between /api/post?filters=user_id=[1;20]
!=[] Not between /api/post?filters=user_id!=[1;20]
~ Like /api/post?filters=title~hello
!~ Not like /api/post?filters=title!~hello

Apify supports filtering records based on more than one AND, NOT condition by using comma. For example:, (*9)

/api/post?filters=user_id=1,status={enabled;pending},tile~hello,view_count!=null

Complex conditions that combine AND, OR and NOT will be available soon., (*10)

Scope functions

/api/order?scopes=orderMeta(values=[adwords,123123];keys=[from,campaign_id]),customer(keyword=nguyen van nam)
namespace App\Models;

class Order extends \Megaads\Apify\Models\BaseModel
{
    public function scopeCustomer($query, $params) {
        if (isset($params['keyword'])) {
            //do something
        }
    }

    public function scopeOrderMeta($query, $params) {
        if (isset($params['keys']) && isset($params['values'])) {
            //do something
        }
    }
}

Entity conventions

Apify works by a simple mechanism, looking for a model class that correspond to the API entity, otherwise the API entity will be matched to a suitable DB table. That means no model class is required to create, do it only in the case of defining relationships, customizing., (*11)

So API entity name should follow one of the conventions:, (*12)

  • The API entity name is the same as a model class name, (*13)

  • Or the API entity name in snake_case that correspond to a model class with the name in CamelCase, (*14)

  • Or the API entity name is the same as a DB table name, (*15)

Relationships

Apify is packed into a Laravel/ Lumen package so relationships also are defined as methods on Eloquent model classes., (*16)

See Laravel docs for details: https://laravel.com/docs/5.6/eloquent-relationships, (*17)

Let's consider the following relationship definations:, (*18)

  • A Nation has many City (one-to-many relationship)
namespace App\Models;
class Nation extends \Apify\Models\BaseModel {
    protected $table = 'location_nation';
    public function cities() {
        return $this->hasMany('App\Models\City', 'nation_id', id);
    }
}
  • A City belongs to a Nation (many-to-one relationship)
  • A City has many District (one-to-many relationship)
namespace App\Models;
class City extends \Apify\Models\BaseModel {
    protected $table = 'location_city';
    public function nation() {
        return $this->belongsTo('App\Models\Nation', 'nation_id');
    }
    public function districts() {
        return $this->hasMany('App\Models\District', 'city_id', id);
    }
}
  • A District belongs to a City (many-to-one relationship)
namespace App\Models;
class District extends \Apify\Models\BaseModel {
    protected $table = 'location_district';
    public function city() {
        return $this->belongsTo('App\Models\City', 'city_id');
    }
}    

Selection on relationships

Apify provides the ability to embed relational data into the results using embeds parameter, (*19)

For example, (*20)

/api/nation?embeds=cities
/api/city?embeds=nation,districts
/api/district?embeds=city

Even nested relationships, (*21)

/api/nation?embeds=cities.districts
/api/district?embeds=city.nation

Filtering on relationships

/api/city?filters=nation.location_code=EU,districts.name~land

Metric

metric=get (by default): Retrieve all records that match the query

/api/post

or, (*22)

/api/post?metric=get

Response format, (*23)

{
    "meta": {
        "has_next": true,
        "total_count": 100,
        "page_count": 2,
        "page_size": 50,
        "page_id": 0
    },
    "result": [],
    "status": "successful"
}

metric=first: Retrieve the first record that matchs the query

/api/post?metric=first

Response format, (*24)

{    
    "result": {},
    "status": "successful"
}

metric=count: Retrieve the number of records that match the query

/api/post?metric=count

Response format, (*25)

{    
    "result": 50,
    "status": "successful"
}

metric=increment/ decrement: Provides convenient methods for incrementing or decrementing the value of a selected column

/api/post?metric=increment&fields=view_count

Response format, (*26)

{    
    "result": 1,
    "status": "successful"
}

Event Bus

Is being updated ..., (*27)

.env configurations

Key Default value Description
APIFY_PREFIX_URL api API URL prefix
APIFY_MODEL_NAMESPACE App\Models Models namespace
APIFY_UPLOAD_PATH /home/upload Upload path
APIFY_MQ_ENABLE false Enable / Disable Message queue (Event Bus)
APIFY_MQ_HOST Message queue server host
APIFY_MQ_PORT Message queue server port
APIFY_MQ_USERNAME Message queue authentication - username
APIFY_MQ_PASSWORD Message queue authentication - password |
APIFY_MQ_EXCHANGE apify Message queue exchange name

Authenticate Apify & authorize Apify?

Read docs here : https://github.com/megaads-vn/apify/blob/master/README-AUTH.md, (*28)

License

The Apify is open-sourced software licensed under the MIT license, (*29)

Contact us/ Instant feedback

Email: phult.contact@gmail.com, (*30)

Skype: phult.bk, (*31)

If you find a bug, please report it here on Github., (*32)

The Versions

21/07 2018

dev-master

9999999-dev https://www.megaads.vn

A pretty library to help developers build RESTful APIs lightly, quickly and properly even without writing code

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn
by phuluong
by lapdam

laravel api microservice lumen rest generator webservice api generator

20/07 2018

1.1.1

1.1.1.0 https://www.megaads.vn

A pretty library to help developers build `RESTful API services` lightly, quickly and properly even without writing code

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn
by phuluong
by lapdam

laravel api microservice lumen rest webservice api generator

18/07 2018

1.1.0

1.1.0.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn
by phuluong

laravel api microservice lumen rest webservice api generator

18/07 2018

1.0.4.1

1.0.4.1 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn
by phuluong

laravel api microservice lumen rest webservice api generator

16/07 2018

1.0.4

1.0.4.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn
by phuluong

laravel api microservice lumen rest webservice api generator

11/07 2018

1.0.3.1

1.0.3.1 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn
by phuluong

laravel api microservice lumen rest webservice api generator

10/07 2018

1.0.3

1.0.3.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn
by phuluong

laravel api microservice lumen rest webservice api generator

29/06 2018

1.0.2.1

1.0.2.1 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn
by phuluong

laravel api microservice lumen rest webservice api generator

28/06 2018

1.0.2

1.0.2.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn
by phuluong

laravel api microservice lumen rest webservice api generator

14/06 2018

1.0.1

1.0.1.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn
by phuluong

laravel api microservice lumen rest webservice api generator

31/05 2018

1.0.0

1.0.0.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn

api microservice rest generator webservice

19/05 2018

0.9.45

0.9.45.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn

api microservice rest generator webservice

16/05 2018

0.9.4

0.9.4.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn

api microservice rest generator webservice

16/05 2018

0.9.3

0.9.3.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn

api microservice rest generator webservice

16/05 2018

0.9.2

0.9.2.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn

api microservice rest generator webservice

12/05 2018

0.9.1

0.9.1.0 https://www.megaads.vn

Dynamic generator for RESTful API

  Sources   Download

MIT

The Requires

 

by Avatar megaads-vn

api microservice rest generator webservice