-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b46ab7b
commit 3283de8
Showing
16 changed files
with
404 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.lock | ||
/vendor/ | ||
/phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0.0.1 / 2020-12-09 | ||
================== | ||
|
||
Initial Release :tada: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
ARG PHP_VERSION=7.2 | ||
|
||
FROM composer:latest as composer | ||
FROM php:${PHP_VERSION} | ||
|
||
# replace shell with bash so we can source files | ||
RUN rm /bin/sh && ln -s /bin/bash /bin/sh | ||
|
||
RUN apt-get update && apt-get install -y \ | ||
libzip-dev \ | ||
zlib1g-dev \ | ||
unzip \ | ||
git-core \ | ||
&& docker-php-ext-install zip | ||
|
||
RUN apt-get update && apt-get install -y libzip-dev zlib1g-dev unzip chromium && docker-php-ext-install zip | ||
|
||
COPY --from=composer /usr/bin/composer /usr/bin/composer | ||
|
||
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser | ||
ENV COMPOSER_ALLOW_SUPERUSER 1 | ||
ENV COMPOSER_HOME /home/docker/.composer | ||
# contains dev-mode packages | ||
RUN composer global require "sllh/composer-versions-check:^2.0" "pyrech/composer-changelogs:^1.7" --prefer-dist --no-progress --no-suggest --classmap-authoritative | ||
|
||
WORKDIR /var/www/html | ||
COPY . /var/www/html | ||
|
||
RUN ls -l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# KurzlinkApiClient | ||
[![Latest Stable Version](https://poser.pugx.org/robertfausk/kurzlink-api-client/v/stable.svg)](https://packagist.org/packages/robertfausk/kurzlink-api-client) | ||
[![Latest Unstable Version](https://poser.pugx.org/robertfausk/kurzlink-api-client/v/unstable.svg)](https://packagist.org/packages/robertfausk/kurzlink-api-client) | ||
[![Total Downloads](https://poser.pugx.org/robertfausk/kurzlink-api-client/downloads.svg)](https://packagist.org/packages/robertfausk/kurzlink-api-client) | ||
[![Build Status](https://travis-ci.com/robertfausk/kurzlink-api-client.svg?branch=master)](https://travis-ci.com/robertfausk/kurzlink-api-client) | ||
[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/robertfausk/kurzlink-api-client/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/robertfausk/kurzlink-api-client/) | ||
[![Code Coverage](https://scrutinizer-ci.com/g/robertfausk/kurzlink-api-client/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/robertfausk/kurzlink-api-client/) | ||
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) | ||
![PHP7 Compatible](https://img.shields.io/travis/php-v/robertfausk/kurzlink-api-client/master?style=flat-square) | ||
[![Open Issues](https://img.shields.io/github/issues-raw/robertfausk/kurzlink-api-client?style=flat-square)](https://github.com/robertfausk/kurzlink-api-client/issues) | ||
[![Closed Issues](https://img.shields.io/github/issues-closed-raw/robertfausk/kurzlink-api-client?style=flat-square)](https://github.com/robertfausk/kurzlink-api-client/issues?q=is%3Aissue+is%3Aclosed) | ||
[![Contributors](https://img.shields.io/github/contributors/robertfausk/kurzlink-api-client?style=flat-square)](https://github.com/robertfausk/kurzlink-api-client/graphs/contributors) | ||
![Contributors](https://img.shields.io/maintenance/yes/2020?style=flat-square) | ||
|
||
Very simple api client written in PHP for url shortener kurzelinks.de which supports ogy.de, t1p.de, | ||
0cn.de, kurzelinks.de and also own domains. | ||
|
||
## Install | ||
|
||
composer require --dev robertfausk/kurzlink-api-client | ||
|
||
Usage Example | ||
------------- | ||
|
||
```PHP | ||
<?php | ||
|
||
use Robertfausk\KurzlinkApiClient\KurzlinkClient; | ||
|
||
$apiKey = 'your secret api key'; | ||
$kurzlinkAddress = 't1p.de'; | ||
$useSandbox = true; | ||
$client = KurzlinkClient::create($apiKey, $kurzlinkAddress, $useSandbox); | ||
$urlToShorten = 'https://google.de/'; | ||
$requestParams = [ // feel free to use every param from api doc | ||
'requesturl' => '', | ||
]; | ||
$shortUrl = $client->request($urlToShorten, $requestParams); | ||
``` | ||
|
||
### How to upgrade? | ||
|
||
Have a look at [CHANGELOG](CHANGELOG.md) for detailed information. | ||
|
||
## How to contribute? | ||
|
||
Copy phpunit.xml.dist and insert your api key to be able to run the integration tests. | ||
|
||
Start docker-compose with php web driver | ||
|
||
docker-compose up php7.2 | ||
|
||
Run phpunit tests | ||
|
||
docker-compose exec php7.2 vendor/bin/phpunit | ||
|
||
|
||
## Credits | ||
|
||
Created by Robert Freigang [robertfausk](https://github.com/robertfausk). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "robertfausk/kurzlink-api-client", | ||
"description": "api client for url shortener kurzelinks.de", | ||
"keywords": [ | ||
"kurzelinks.de", | ||
"url shortener", | ||
"t1p.de", | ||
"ogy.de", | ||
"ogy.de", | ||
"api-client" | ||
], | ||
"type": "api-client", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Robert Freigang", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.2", | ||
"symfony/http-client": "~3.4|~4.0|~5.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~8.0" | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"suggest": { | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Robertfausk\\KurzlinkApiClient\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Robertfausk\\KurzlinkApiClient\\Tests\\": "tests" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.0.x-dev" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: "3" | ||
services: | ||
|
||
php7.2: | ||
tty: true | ||
build: | ||
context: . | ||
args: | ||
- PHP_VERSION=7.2 | ||
volumes: | ||
- .:/var/www/html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit colors="true" bootstrap="vendor/autoload.php"> | ||
<php> | ||
<env name="apiKey" value="your secret api key"/> | ||
</php> | ||
<testsuites> | ||
<testsuite name="test suite"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Robertfausk\KurzlinkApiClient\Exception; | ||
|
||
class BadRequestException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Robertfausk\KurzlinkApiClient\Exception; | ||
|
||
class Exception extends \Exception | ||
{ | ||
public function __construct(array $content) { | ||
parent::__construct($content['messages']['error'] ?? 'No error message in response.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Robertfausk\KurzlinkApiClient\Exception; | ||
|
||
class ForbiddenException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Robertfausk\KurzlinkApiClient\Exception; | ||
|
||
class LockedException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Robertfausk\KurzlinkApiClient\Exception; | ||
|
||
class NoResponseException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Robertfausk\KurzlinkApiClient\Exception; | ||
|
||
class TooManyRequestsException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Robertfausk\KurzlinkApiClient\Exception; | ||
|
||
class UnexpectedResponseException extends Exception | ||
{ | ||
public function __construct(array $content) | ||
{ | ||
$content['messages']['error'] ? $content['messages']['error'] : 'Response has unknown format.'; | ||
parent::__construct($content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Robertfausk\KurzlinkApiClient. | ||
* (c) Robert Freigang <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace Robertfausk\KurzlinkApiClient; | ||
|
||
use Robertfausk\KurzlinkApiClient\Exception\BadRequestException; | ||
use Robertfausk\KurzlinkApiClient\Exception\Exception; | ||
use Robertfausk\KurzlinkApiClient\Exception\ForbiddenException; | ||
use Robertfausk\KurzlinkApiClient\Exception\LockedException; | ||
use Robertfausk\KurzlinkApiClient\Exception\NoResponseException; | ||
use Robertfausk\KurzlinkApiClient\Exception\TooManyRequestsException; | ||
use Robertfausk\KurzlinkApiClient\Exception\UnexpectedResponseException; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
|
||
final class KurzlinkClient | ||
{ | ||
/** @var array<string,mixed> */ | ||
private $config = [ | ||
'json' => true, | ||
]; | ||
/** @var string */ | ||
private $kurzlinkAddress; | ||
/** @var bool */ | ||
private $useSandbox; | ||
/** @var int */ | ||
private $apiVersion; | ||
/** @var string */ | ||
private $apiKey; | ||
|
||
private function __construct(string $apiKey, string $kurzlinkAddress, bool $useSandbox) | ||
{ | ||
$this->kurzlinkAddress = $kurzlinkAddress; | ||
$this->useSandbox = $useSandbox; | ||
$this->apiVersion = 21; | ||
if ($this->useSandbox) { | ||
$this->kurzlinkAddress .= '/sandbox'; | ||
} else { | ||
$this->kurzlinkAddress .= '/api'; | ||
} | ||
$this->apiKey = $apiKey; | ||
} | ||
|
||
public static function create(string $apiKey, string $kurzlinkAddress, bool $useSandbox = true): self | ||
{ | ||
return new self($apiKey, $kurzlinkAddress, $useSandbox); | ||
} | ||
|
||
public function request(string $urlToShorten, ?array $requestParams = null): string | ||
{ | ||
$client = HttpClient::create(); | ||
|
||
$url = \sprintf( | ||
'https://%s', | ||
$this->kurzlinkAddress | ||
); | ||
|
||
$headers = [ | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
'Connection' => 'keep-alive' | ||
]; | ||
|
||
$options = ['headers' => $headers]; | ||
$options['body'] = $this->config; | ||
$options['body']['url'] = urldecode($urlToShorten); | ||
$options['body']['key'] = $this->apiKey; | ||
$options['body']['apiversion'] = $this->apiVersion; | ||
if ($requestParams) { | ||
$options['body'] = \array_merge($options['body'], $requestParams); | ||
} | ||
|
||
$response = $client->request('POST', $url, $options); | ||
$statusCode = $response->getStatusCode(); | ||
$content = $response->toArray(false); | ||
|
||
switch ($statusCode) { | ||
case 200: | ||
break; | ||
case 400: | ||
throw new BadRequestException($content); | ||
case 403: | ||
throw new ForbiddenException($content); | ||
case 423: | ||
throw new LockedException($content); | ||
case 429: | ||
throw new TooManyRequestsException($content); | ||
case 444: | ||
throw new NoResponseException($content); | ||
default: | ||
throw new Exception($content); | ||
} | ||
|
||
$shortUrl = $content['shorturl']['url'] ?? false; | ||
if (false === $shortUrl || !\is_string($shortUrl)) { | ||
throw new UnexpectedResponseException($content); | ||
} | ||
|
||
return $shortUrl; | ||
} | ||
|
||
public function getApiVersion(): int | ||
{ | ||
return $this->apiVersion; | ||
} | ||
} |
Oops, something went wrong.