Skip to content

Latest commit

 

History

History
210 lines (167 loc) · 4.23 KB

README.md

File metadata and controls

210 lines (167 loc) · 4.23 KB

🔗 Short Url Package 🔗

A Http client to interact with the Short Url api service offered by PDM


Table of contents

Installation

Composer:

  • In your composer.json add the following to the "repositories" section:
{
    "type": "vcs",
    "url": "https://github.com/pdmfc/short-url-package-php-8-support.git"
}
  • Require the package:
composer require pdmfc/short-url-package-php-8-support

Configuration

  • Publish the config file:
php artisan vendor:publish --tag="short_config"
  • Add the following environment variables to your .env file with the necessary values defined for your current application:
SHORT_URL_API_ID=
SHORT_URL_API_TOKEN=  
SHORT_URL_API_BASE_URL=

Api

  • If the request headers are incorrect, the response will be the following:
{
    "message": "Access is not allowed"
}

getUrl(string $shortUrl)

Usage:

use Pdmfc\Shorturl\Client\ShortUrlClient;

$client = new ShortUrlClient();

$response = $client->getUrl('Nc');

Successful response:

{
  "Id": 2,
  "shortUrl": "http://teste.ll/Nc",
  "qr_code": "<?xml version=\"1.0\" encodin..."
}

Unsuccessful response:

{
  "message": "shortUrls Nc not found"
}

createUrl(array $params)

Usage:

use Pdmfc\Shorturl\Client\ShortUrlClient;

$client = new ShortUrlClient();

$response = $client->createUrl([
    'domainUrl' => 's.pdm.pt',
    'originalUrl' => 'www.original-url.com/long',
    'liveTime' => 0,
    'active' => true,
    'shortUrl' => '1C',
]);

Successful response:

{
    "Id": 26,
    "shortUrl": "http://s.pdm.pt/1C",
    "qrCode": "<?xml version=\"1.0\" encoding..."
}

Unsuccessful response:

{
    "message": "Is not possible create a short url"
}

changeUrl(string $shortUrl, array $params)

use Pdmfc\Shorturl\Client\ShortUrlClient;

$client = new ShortUrlClient();


$response = $client->updateUrl('Nc', [
        'originalUrl' => 'www.original-url-changed.com',
]);

Successful response:

{
  "Id": 26,
  "shortUrl": "http://teste.ll/1C",
  "qrCode": "<?xml version=\"1.0\" encoding..."
}

Unsuccessful response:

{
  "message": "Is not possible update the short url"
}

deleteUrl(string $shortUrl)

use Pdmfc\Shorturl\Client\ShortUrlClient;

$client = new ShortUrlClient();

$response = $client->deleteUrl('Nc');

Successful response:

{
  "message": "Code Nc was deleted with success"
}

Unsuccessful responses:

{
  "message": "Is not possible delete a short url"
}
{
  "message": "shortUrls Nc not found"
}

Params:
Param Type Required Default Description Example
domainUrl string Short url domain http://teste.com
originalUrl string ✔️ Url where you will be redirected www.original-url.com/long
liveTime integer 0 expiration time 60
active boolean true Define if the link is active false
shortUrl string Uri to be generated. If none provided, it will automatically generated a12

Tests

To run the test suite, first you must copy the phpunit.xml.dist:

cp phpunit.xml.dist phpunit.xml

Make sure to uncomment the ENV variables on your phpunit configuration file and add the necessary values:

<env name="SHORT_URL_API_BASE_URL" value=""/>
<env name="SHORT_URL_API_ID" value=""/>
<env name="SHORT_URL_API_TOKEN" value=""/>

Now you can run the test suite:

vendor/bin/phpunit

⚠️ Be careful!

Do not add your phpunit config values directly into the phpunit.xml.dist file since this file will be in the version control repository!