Skip to content

Commit

Permalink
Initial commit 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfausk committed Dec 9, 2020
1 parent b46ab7b commit 3283de8
Show file tree
Hide file tree
Showing 16 changed files with 404 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
/vendor/
/phpunit.xml
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0.0.1 / 2020-12-09
==================

Initial Release :tada:
29 changes: 29 additions & 0 deletions Dockerfile
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
60 changes: 60 additions & 0 deletions README.md
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).
47 changes: 47 additions & 0 deletions composer.json
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"
}
}
}
11 changes: 11 additions & 0 deletions docker-compose.yml
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
18 changes: 18 additions & 0 deletions phpunit.xml.dist
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>
8 changes: 8 additions & 0 deletions src/Exception/BadRequestException.php
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
{
}
11 changes: 11 additions & 0 deletions src/Exception/Exception.php
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.');
}
}
8 changes: 8 additions & 0 deletions src/Exception/ForbiddenException.php
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
{
}
8 changes: 8 additions & 0 deletions src/Exception/LockedException.php
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
{
}
8 changes: 8 additions & 0 deletions src/Exception/NoResponseException.php
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
{
}
8 changes: 8 additions & 0 deletions src/Exception/TooManyRequestsException.php
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
{
}
13 changes: 13 additions & 0 deletions src/Exception/UnexpectedResponseException.php
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);
}
}
111 changes: 111 additions & 0 deletions src/KurzlinkClient.php
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;
}
}
Loading

0 comments on commit 3283de8

Please sign in to comment.