Giter Site home page Giter Site logo

vetmanager-rest-api's Introduction

vetmanager-rest-api

GitHub CI Coverage Status

Vetmanager - CRM for veterinary with REST API. vetmanager-rest-api is library will help work with them.

vetmanager.ru

vetmanager REST API Docs

vetmanager REST API in Postman

For what?

  1. Full url only by domain name (host might to change)
  2. Model name validation in uri()
  3. Simplify apiKey and token authorization
  4. Sorting, Filtering and others
  5. Get all sorted and filtered records from model
use GuzzleHttp\Client;
use function Otis22\VetmanagerUrl\url;
use function Otis22\VetmanagerRestApi\uri;
use function Otis22\VetmanagerRestApi\byApiKey;

$client = new Client(['base_uri' => url("myclinic")->asString()]);
$request = $client->request(
    'GET',
    uri("invoice")->asString(),
    ['headers' => byApiKey("myapikey")->asKeyValue()]
);

Warning! function url requires "otis22/vetmanager-url" package.

Installation

composer require otis22/vetmanager-rest-api

Usage

Usage for auth

Api key auth

$client = new GuzzleHttp\Client(['base_uri' => 'http://some.vetmanager.ru']);

$authHeaders = new Otis22\VetmanagerRestApi\Headers\WithAuth(
    new \Otis22\VetmanagerRestApi\Headers\Auth\ByApiKey(
        new \Otis22\VetmanagerRestApi\Headers\Auth\ApiKey('test-key')
    )
);

$client->request(
    'GET',
    '/rest/api/user/1',
    ['headers' => $authHeaders->asKeyValue()]
);    

or with function

$authHeaders = Otis22\VetmanagerRestApi\byApiKey('test-key');
# use this after ['headers' => $authHeaders->asKeyValue()]

With custom timezone

$client = new GuzzleHttp\Client(['base_uri' => 'http://some.vetmanager.ru']);

$myHeaders = [
    'X-REST-TIME-ZONE' => '+02:00'
];
$allHeaders = new Otis22\VetmanagerRestApi\Headers\WithAuthAndParams(
    new \Otis22\VetmanagerRestApi\Headers\Auth\ByApiKey(
        new \Otis22\VetmanagerRestApi\Headers\Auth\ApiKey('test-key')
    ),
    $myHeaders  
);

$client->request(
    'GET',
    '/rest/api/user/1',
    ['headers' => $allHeaders->asKeyValue()]
);    

Token auth

$client = new GuzzleHttp\Client(['base_uri' => 'http://some.vetmanager.ru']);

$authHeaders = new Otis22\VetmanagerRestApi\Headers\WithAuth(
    new \Otis22\VetmanagerRestApi\Headers\Auth\ByToken(
        new \Otis22\VetmanagerToken\Credentials\AppName("myapp"),
        new \Otis22\VetmanagerToken\Token\Concrete("mytoken")
    )
);

$client->request(
    'GET',
    '/rest/api/user/1',
    ['headers' => $authHeaders->asKeyValue()]
); 

or with function

$authHeaders = Otis22\VetmanagerRestApi\byToken('myapp', 'mytoken');
# use this after ['headers' => $authHeaders->asKeyValue()]

Usage for create valid URI

Only model

$client = new GuzzleHttp\Client(['base_uri' => 'http://some.vetmanager.ru']);

$uri = new \Otis22\VetmanagerRestApi\URI\OnlyModel(
    new \Otis22\VetmanagerRestApi\Model('invoice')
);

// request to /rest/api/invoice
$client->request('GET', $uri->asString()); 

or with function

$uriString = \Otis22\VetmanagerRestApi\uri('invoice')->asString();

Model with particular id

$client = new GuzzleHttp\Client(['base_uri' => 'http://some.vetmanager.ru']);

$uri = new \Otis22\VetmanagerRestApi\URI\WithId(
    new \Otis22\VetmanagerRestApi\Model('invoice'),
    5
);

// request to /rest/api/invoice/5
$client->request('GET', $uri->asString()); 

or with function

$uriString = \Otis22\VetmanagerRestApi\uri('invoice', 5)->asString();

Usage for filtering and sorting

How to use Filters

$client = new GuzzleHttp\Client(['base_uri' => 'http://some.vetmanager.ru']);

$filters = new \Otis22\VetmanagerRestApi\Query\Filters(
    new \Otis22\VetmanagerRestApi\Query\Filter\EqualTo(
        new \Otis22\VetmanagerRestApi\Model\Property('propertyName'),
        new \Otis22\VetmanagerRestApi\Query\Filter\Value\StringValue('propertyValue')
    )
    # ... we can use mach more filters new Filters($filterOne, $filterTwo, ... );
);

$client->request(
    'GET',
    '/rest/api/user/1',
    [
        'headers' => $authHeaders->asKeyValue(),
        'query' => $filters->asKeyValue()
    ]
); 

Full filter list

  • Otis22\VetmanagerRestApi\Query\Filter\EqualTo - where property is equal to value
  • Otis22\VetmanagerRestApi\Query\Filter\InArray - where property is in list
  • Otis22\VetmanagerRestApi\Query\Filter\LessOrEqualThan - where property is less or equal than value
  • Otis22\VetmanagerRestApi\Query\Filter\LessThan - where property is less than value
  • Otis22\VetmanagerRestApi\Query\Filter\Like - where property is like value(for using MySQL LIKE)
  • Otis22\VetmanagerRestApi\Query\Filter\MoreOrEqualThan - where property is more or equal than value
  • Otis22\VetmanagerRestApi\Query\Filter\MoreThan - where property more than value
  • Otis22\VetmanagerRestApi\Query\Filter\NotEqualTo - where propery is not equal to value
  • Otis22\VetmanagerRestApi\Query\Filter\NotInArray - where property is not in list

How to use Sorts

$client = new GuzzleHttp\Client(['base_uri' => 'http://some.vetmanager.ru']);

$sorts = new \Otis22\VetmanagerRestApi\Query\Sorts(
    new \Otis22\VetmanagerRestApi\Query\Sort\AscBy(
        new \Otis22\VetmanagerRestApi\Model\Property('propertyName')
    ), 
    new \Otis22\VetmanagerRestApi\Query\Sort\DescBy(
        new \Otis22\VetmanagerRestApi\Model\Property('property2Name')
    )
);

$client->request(
    'GET',
    '/rest/api/user/1',
    [
        'headers' => $authHeaders->asKeyValue(),
        'query' => $sorts->asKeyValue()
    ]
); 

How to use both Sorts and Filters

$client = new GuzzleHttp\Client(['base_uri' => 'http://some.vetmanager.ru']);

$query = new \Otis22\VetmanagerRestApi\Query\Query(
    new \Otis22\VetmanagerRestApi\Query\Filters(),
    new \Otis22\VetmanagerRestApi\Query\Sorts()
);


$client->request(
    'GET',
    '/rest/api/user/1',
    [
        'headers' => $authHeaders->asKeyValue(),
        'query' => $query->asKeyValue()
    ]
); 

How to get all records

$paged =  PagedQuery::forGettingAll(
    new Query(
        // Sorts Required!
    )
);
$result = [];
do {
    $response = json_decode(
        strval(
            $client->request(
                'GET',
                uri('invoice')->asString(),
                [
                    'headers' => $headers->asKeyValue(),
                    'query' => $paged->asKeyValue()
                ]
            )->getBody()
        ),
        true
    );
    $paged = $paged->next();
    $result = array_merge(
        $response['data']['invoice'],
        $result
    );
} while (count($result) < $response['data']['totalCount']);

How to get top n records

$top1 =  PagedQuery::forGettingTop(
    new Query(
        // Sorts Required!
    ),
    1
);
$response = json_decode(
    strval(
        $client->request(
            'GET',
            uri('invoice')->asString(),
            [
                'headers' => $headers->asKeyValue(),
                'query' => $top1->asKeyValue()
            ]
        )->getBody()
    ),
    true
);

Contributing

For run all tests

make all

or connect to terminal

make exec

Dafault php version is 7.4. Use PHP_VERSION= for using custom version.

make all PHP_VERSION=8.0
# run both 
make all PHP_VERSION=7.4 && make all PHP_VERSION=8.0

For integration tests copy .env.example to .env and fill with yours values

all commands

# security check
make security
# composer install
make install
# composer install with --no-dev
make install-no-dev
# check code style
make style
# run static analyze tools
make static-analyze
# run unit tests
make unit
#  check coverage
make coverage
# check integration, .env required
make integration

vetmanager-rest-api's People

Contributors

otis22 avatar danilyer228 avatar

Stargazers

Mnats Avetisyan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.