Skip to content

Commit

Permalink
Implement Proxies endpoint
Browse files Browse the repository at this point in the history
- Endpoint does not use /profiles prefix, but decided to keep it consistent since this is how it's listed in the documentation
- Even though the data structure is simple, created the entity and factory to keep it standard
  • Loading branch information
rapkis committed Aug 31, 2023
1 parent 4779ed6 commit 6e59b1b
Show file tree
Hide file tree
Showing 9 changed files with 1,119 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Entities/Proxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Entities;

class Proxy
{
public function __construct(
public readonly string $pk,
public readonly string $uid,
public readonly string $country,
public readonly string $countryName,
public readonly string $city,
public readonly float $gpsLat,
public readonly float $gpsLong
) {
}
}
24 changes: 24 additions & 0 deletions src/Factories/ProxyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Factories;

use Rapkis\Controld\Contracts\Factories\Factory;
use Rapkis\Controld\Entities\Proxy;

class ProxyFactory implements Factory
{
public function make(array $data): Proxy
{
return new Proxy(
pk: $data['PK'],
uid: $data['uid'],
country: $data['country'],
countryName: $data['country_name'],
city: $data['city'],
gpsLat: $data['gps_lat'],
gpsLong: $data['gps_long'],
);
}
}
6 changes: 6 additions & 0 deletions src/Resources/Profiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rapkis\Controld\Resources\Profiles\DefaultRule;
use Rapkis\Controld\Resources\Profiles\Filters;
use Rapkis\Controld\Resources\Profiles\Options;
use Rapkis\Controld\Resources\Profiles\Proxies;
use Rapkis\Controld\Resources\Profiles\RuleFolders;
use Rapkis\Controld\Resources\Profiles\Services;
use Rapkis\Controld\Responses\Profile;
Expand Down Expand Up @@ -93,4 +94,9 @@ public function defaultRule(): DefaultRule
{
return app(DefaultRule::class, ['client' => $this->client]);
}

public function proxies(): Proxies
{
return app(Proxies::class, ['client' => $this->client]);
}
}
29 changes: 29 additions & 0 deletions src/Resources/Profiles/Proxies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Resources\Profiles;

use Illuminate\Http\Client\PendingRequest;
use Rapkis\Controld\Factories\ProxyFactory;

class Proxies
{
public function __construct(private readonly PendingRequest $client, private readonly ProxyFactory $proxy)
{
}

public function list(): \Rapkis\Controld\Responses\Proxies
{
$response = $this->client->get('proxies')->json('body.proxies');

$result = new \Rapkis\Controld\Responses\Proxies();

foreach ($response as $proxy) {
$proxy = $this->proxy->make($proxy);
$result->put($proxy->pk, $proxy);
}

return $result;
}
}
11 changes: 11 additions & 0 deletions src/Responses/Proxies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Responses;

use Illuminate\Support\Collection;

class Proxies extends Collection
{
}
31 changes: 31 additions & 0 deletions tests/Factories/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

use Rapkis\Controld\Entities\Proxy;
use Rapkis\Controld\Factories\ProxyFactory;

it('builds a proxy', function (array $data, Proxy $expected) {
expect((new ProxyFactory())->make($data))->toEqual($expected);
})->with([
[
[
'city' => 'Washington DC',
'country' => 'US',
'country_name' => 'United States',
'PK' => 'WAS',
'gps_lat' => 38.9047,
'gps_long' => -77.0164,
'uid' => 'United States:Washington DC',
],
new Proxy(
pk: 'WAS',
uid: 'United States:Washington DC',
country: 'US',
countryName: 'United States',
city: 'Washington DC',
gpsLat: 38.9047,
gpsLong: -77.0164,
),
],
]);
Loading

0 comments on commit 6e59b1b

Please sign in to comment.