teamicon/apikit

A simple toolkit to help a team icon's developer to create a native php API

v1.1.4 2020-07-27 10:21 UTC

This package is auto-updated.

Last update: 2024-09-27 19:28:58 UTC


README

This library is a simple toolkit to help Team icon's developer to create a native php API.

Installation

You need to use a composer installation to add this library on your application. Open a terminal or a command prompt and digit:

composer require teamicon/apikit:*

after some minutes it should appear a vendor folder and a composer.json file in the root.

Usage

In the application you'll need to write as first line of your script:

require_once(__DIR__ . "/your-path/vendor/autoload.php");
use \teamicon\apikit\className;
$foo = new className();
...

the your-path token is the right path where the folder vendor is located. For instance if your script is put in the sources folder in the root, your path becomes ../ because you need to browse back.

require_once(__DIR__ . "/../vendor/autoload.php");

The body of the index

Creating an API application is simple but required some features for working well. In the body we need to put the references of the scripts which will use in the application, after we'll put the headers required and finally we can use the apikit to manage fast all route rules.

require_once(__DIR__. "/vendor/autoload.php");
use \teamicon\apikit\{list of classes that you will use separated by comma}
...
//header for CORS calls
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, PATCH, DELETE, OPTIONS");
header("Access-Control-Max-Age: 100");
//header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Headers: X-SC, X-LNG, AccountKey,x-requested-with, Content-Type, origin, authorization, accept, client-security-token, host, date, cookie, cookie2");
header("Content-Type: application/json");
//preflight test
if ($_SERVER['REQUEST_METHOD'] != 'OPTIONS') echo RouteManager::Start( 'route');
function route(string $sc, string $url, string $lng, RouteParameters $rp) {
    //put here your logic
}

List of classes in the apikit

ApiKitException

This class is inherit from Exception and it will use to identify internal exception.

ApiResult

It's used to create a standard response. You can use the static function OK to return the right message with the associative payload array or you can use the KO function to advise about the presence of errors in the call.

DbManager

This class helps user to use the database with a set of fuction pre built. You must be create an instance of the class with all parameters to connect to db. After this operation you might use Query or Execute functions to extract dataset in an associative array or receive the number of rows edited. The parameters for these functions are similar: a query statement, a list of params type and the parameters as an associative array.

Logger

It's use to log some activities and error in the specific file.

RouteManager

It's the core of the apikit and we had discussed before about you can use it.

RouteParameters

It's an internal class to exchange info about routing. You can receive info about:

public function GetSC() : string { return $this->sc; }
public function GetLanguage() : string { return $this->lng; }
public function GetMethod() : string { return $this->method; }
public function GetEntity() : string { return $this->entity; }
public function GetAction() : string { return $this->action; }
public function GetKeys() : array { return $this->keys; }

Utility

It contains some tips to simplyfy the life of the developer. The list of funcitions is:

public static function Curl(string $url, string $method, bool $forceHeaders = false, array $headers = [], array $params = []) : array
public static function GenerateRandomToken(int $numOfChar) : string
public static function GetClientIp() : string
public  static function GetClientLanguage() : string

License

Creative Commons Attribution Non Commercial No Derivatives 4.0 International CC-BY-NC-ND-4.0