Skip to content

Commit f1de052

Browse files
author
bryanadamloh97
committed
Added two need drivers (Hide URI & Ouo.io)
1 parent 0975087 commit f1de052

File tree

5 files changed

+161
-4
lines changed

5 files changed

+161
-4
lines changed

.github/workflows/ci-test.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
name: run-tests
22

3-
on:
4-
pull_request:
3+
on: [push, pull_request]
54

65
jobs:
76
run-tests:
@@ -38,4 +37,4 @@ jobs:
3837
composer update --prefer-dist --no-interaction --no-suggest
3938
4039
- name: Execute tests
41-
run: vendor/bin/phpunit
40+
run: vendor/bin/phpunit

config/url-shortener.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
return [
4-
'default' => env('URL_SHORTENER_DRIVER', 'bit_ly'),
4+
'default' => env('URL_SHORTENER_DRIVER', 'hide_uri'),
55

66
'shorteners' => [
77

@@ -37,6 +37,15 @@
3737
'domain' => env('URL_SHORTENER_PREFIX'),
3838
'token' => env('URL_SHORTENER_API_TOKEN'),
3939
'suffix' => env('URL_SHORTENER_SUFFIX', 'UNGUESSABLE')
40+
],
41+
42+
'hide_uri' => [
43+
'driver' => 'hide_uri'
44+
],
45+
46+
'ouo_io' => [
47+
'driver' => 'ouo_io',
48+
'token' => env('URL_SHORTENER_API_TOKEN')
4049
]
4150
]
4251
];
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
4+
5+
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
7+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
8+
use CodeOfDigital\LaravelUrlShortener\Exceptions\TooManyRequestException;
9+
use GuzzleHttp\ClientInterface;
10+
use GuzzleHttp\Exception\RequestException;
11+
use GuzzleHttp\Psr7\Request;
12+
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Str;
14+
use Psr\Http\Message\ResponseInterface;
15+
16+
class HideUriDriverShortener extends DriverShortener
17+
{
18+
protected $client;
19+
protected $object;
20+
21+
public function __construct(ClientInterface $client)
22+
{
23+
$this->client = $client;
24+
$this->object = [
25+
'allow_redirects' => false,
26+
'base_uri' => 'https://hideuri.com',
27+
];
28+
}
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function shortenAsync($url, array $options = [])
34+
{
35+
if (!Str::startsWith($url, ['http://', 'https://']))
36+
throw new ShortUrlException('The given URL must begin with http/https');
37+
38+
$options = array_merge_recursive(Arr::add($this->object, 'json.url', $url), $options);
39+
$request = new Request('POST', '/api/v1/shorten');
40+
41+
return $this->client->sendAsync($request, $options)->then(
42+
function (ResponseInterface $response) {
43+
return json_decode($response->getBody()->getContents())->result_url;
44+
},
45+
function (RequestException $e) {
46+
$this->getErrorMessage($e->getCode(), json_decode($e->getResponse()->getBody()->getContents())->error);
47+
}
48+
);
49+
}
50+
51+
protected function getErrorMessage($code, $message = null)
52+
{
53+
switch ($code) {
54+
case 400: throw new BadRequestException($message);
55+
case 429: throw new TooManyRequestException($message);
56+
default: throw new InvalidResponseException($message);
57+
}
58+
}
59+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
4+
5+
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException;
7+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
8+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
9+
use GuzzleHttp\ClientInterface;
10+
use GuzzleHttp\Exception\RequestException;
11+
use GuzzleHttp\Psr7\Request;
12+
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Str;
14+
use Psr\Http\Message\ResponseInterface;
15+
16+
class OuoIoDriverShortener extends DriverShortener
17+
{
18+
protected $client;
19+
protected $object;
20+
protected $token;
21+
22+
public function __construct(ClientInterface $client, $token)
23+
{
24+
$this->client = $client;
25+
$this->token = $token;
26+
$this->object = [
27+
'allow_redirects' => false,
28+
'base_uri' => 'https://ouo.io',
29+
];
30+
}
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function shortenAsync($url, array $options = [])
36+
{
37+
if (!Str::startsWith($url, ['http://', 'https://']))
38+
throw new ShortUrlException('The given URL must begin with http/https');
39+
40+
$options = array_merge_recursive(Arr::add($this->object, 'query.s', urlencode($url)), ['query' => $options]);
41+
$request = new Request('GET', "/api/{$this->token}");
42+
43+
return $this->client->sendAsync($request, $options)->then(
44+
function (ResponseInterface $response) {
45+
if ($response->getStatusCode() == 302)
46+
$this->getErrorMessage($response->getStatusCode(), "Your API Token is invalid. Please try a new API Token.");
47+
48+
return $response->getBody()->getContents();
49+
},
50+
function (RequestException $e) {
51+
$this->getErrorMessage($e->getCode(), $e->getResponse()->getBody()->getContents());
52+
}
53+
);
54+
}
55+
56+
protected function getErrorMessage($code, $message = null)
57+
{
58+
switch ($code) {
59+
case 302: throw new InvalidApiTokenException($message);
60+
case 400: throw new BadRequestException($message);
61+
default: throw new InvalidResponseException($message);
62+
}
63+
}
64+
}

src/UrlShortener.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use CodeOfDigital\LaravelUrlShortener\Drivers\BitLyDriverShortener;
77
use CodeOfDigital\LaravelUrlShortener\Drivers\CuttLyDriverShortener;
88
use CodeOfDigital\LaravelUrlShortener\Drivers\FirebaseDriverShortener;
9+
use CodeOfDigital\LaravelUrlShortener\Drivers\HideUriDriverShortener;
910
use CodeOfDigital\LaravelUrlShortener\Drivers\IsGdDriverShortener;
11+
use CodeOfDigital\LaravelUrlShortener\Drivers\OuoIoDriverShortener;
1012
use CodeOfDigital\LaravelUrlShortener\Drivers\ShorteStDriverShortener;
1113
use CodeOfDigital\LaravelUrlShortener\Drivers\TinyUrlDriverShortener;
1214
use GuzzleHttp\ClientInterface;
@@ -136,6 +138,30 @@ protected function createFirebaseDriver(array $config)
136138
);
137139
}
138140

141+
/**
142+
* Create an instance of HideUri driver
143+
*
144+
* @return HideUriDriverShortener
145+
*/
146+
protected function createHideUriDriver()
147+
{
148+
return new HideUriDriverShortener($this->app->make(ClientInterface::class));
149+
}
150+
151+
/**
152+
* Create an instance of Ouo.io driver
153+
*
154+
* @param array $config
155+
* @return OuoIoDriverShortener
156+
*/
157+
protected function createOuoIoDriver(array $config)
158+
{
159+
return new OuoIoDriverShortener(
160+
$this->app->make(ClientInterface::class),
161+
Arr::get($config, 'token')
162+
);
163+
}
164+
139165
/**
140166
* Get the default URL shortener driver
141167
*

0 commit comments

Comments
 (0)