Skip to content

Commit aa02818

Browse files
authored
Merge pull request #2 from coderflexx/feat/api-call-handling
Feat: API Resource Methods.
2 parents 2b3e0f1 + 32710a9 commit aa02818

File tree

8 files changed

+154
-13
lines changed

8 files changed

+154
-13
lines changed

composer.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
],
1818
"require": {
1919
"php": "^8.4",
20-
"spatie/laravel-package-tools": "^1.16",
21-
"illuminate/contracts": "^10.0||^11.0||^12.0",
2220
"ext-curl": "*",
2321
"ext-json": "*",
22+
"guzzlehttp/guzzle": "7.x",
23+
"illuminate/contracts": "^10.0||^11.0||^12.0",
2424
"league/oauth2-client": "^2",
25-
"guzzlehttp/guzzle": "7.x"
25+
"spatie/laravel-data": "^4.15",
26+
"spatie/laravel-package-tools": "^1.16"
2627
},
2728
"require-dev": {
2829
"laravel/pint": "^1.14",

src/DTOs/CompaignDTO.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Coderflex\LaravelSendy\DTOs;
44

5-
class CompaignDTO
5+
use Spatie\LaravelData\Data;
6+
7+
class CompaignDTO extends Data
68
{
79
public function __construct(
810
//

src/DTOs/SubscribersDTO.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Coderflex\LaravelSendy\DTOs;
44

5-
class SubscribersDTO
5+
use Spatie\LaravelData\Data;
6+
7+
class SubscribersDTO extends Data
68
{
79
public function __construct(
810
//

src/LaravelSendy.php

+82
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,30 @@
66
use Coderflex\LaravelSendy\Resources\Resources\Campaigns;
77
use Coderflex\LaravelSendy\Resources\Resources\Lists;
88
use Coderflex\LaravelSendy\Resources\Resources\Subscribers;
9+
use Exception;
10+
use GuzzleHttp\Client;
11+
use GuzzleHttp\Exception\ClientException;
912

1013
class LaravelSendy
1114
{
15+
protected string $apiKey;
16+
17+
protected string $apiUrl;
18+
19+
public function __construct()
20+
{
21+
if (blank(config('laravel-sendy.api_key'))) {
22+
throw new Exception('API Key is not set in the config file.');
23+
}
24+
25+
if (blank(config('laravel-sendy.api_url'))) {
26+
throw new Exception('API URL is not set in the config file.');
27+
}
28+
29+
$this->apiKey = config('laravel-sendy.api_key');
30+
$this->apiUrl = config('laravel-sendy.api_url');
31+
}
32+
1233
public function subscribers(): Subscribers
1334
{
1435
return new Subscribers;
@@ -28,4 +49,65 @@ public function campaigns(): Campaigns
2849
{
2950
return new Campaigns;
3051
}
52+
53+
public function __call(string $function, array $args)
54+
{
55+
$options = ['get', 'post', 'put', 'delete', 'patch'];
56+
$path = (isset($args[0])) ? $args[0] : null;
57+
$data = (isset($args[1])) ? $args[1] : [];
58+
$headers = (isset($args[2])) ? $args[2] : [];
59+
60+
if (! in_array($function, $options)) {
61+
throw new Exception("Method {$function} not found.");
62+
}
63+
64+
return self::guzzle(
65+
type: $function,
66+
request: $path,
67+
data: $data,
68+
headers: $headers
69+
);
70+
}
71+
72+
/**
73+
* @throws \Exception
74+
*/
75+
protected function guzzle(string $type, string $request, array $data = [], array $headers = []): mixed
76+
{
77+
try {
78+
$client = new Client;
79+
80+
$mainHeaders = [
81+
'Content-Type' => 'application/json',
82+
'Accept' => 'application/json',
83+
'Authorization' => 'Bearer '.$this->apiKey,
84+
];
85+
86+
$headers = is_array($headers) && count($headers) > 0
87+
? array_merge($mainHeaders, $headers)
88+
: $mainHeaders;
89+
90+
$response = $client->$type($this->apiUrl.$request, [
91+
'headers' => $headers,
92+
'body' => json_encode($data),
93+
]);
94+
95+
$responseObject = $response->getBody()->getContents();
96+
97+
return $this->isJson($responseObject)
98+
? json_decode($responseObject, true)
99+
: $responseObject;
100+
101+
} catch (ClientException $th) {
102+
throw new Exception('Error: '.$th->getMessage());
103+
} catch (Exception $th) {
104+
throw new Exception('Error: '.$th->getMessage());
105+
}
106+
}
107+
108+
protected function isJson(string $string): bool
109+
{
110+
return is_array(json_decode($string)) &&
111+
(json_last_error() === JSON_ERROR_NONE);
112+
}
31113
}

src/Resources/Brands.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Coderflex\LaravelSendy\Resources\Resources;
44

5+
use Coderflex\LaravelSendy\Facades\LaravelSendy;
6+
57
class Brands
68
{
79
public function get()
810
{
9-
//
11+
return LaravelSendy::get('/api/brands/get-brands.php');
1012
}
1113
}

src/Resources/Campaigns.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
namespace Coderflex\LaravelSendy\Resources\Resources;
44

5+
use Coderflex\LaravelSendy\DTOs\CompaignDTO;
6+
use Coderflex\LaravelSendy\Facades\LaravelSendy;
7+
58
class Campaigns
69
{
710
public function create(array $data)
811
{
9-
//
12+
$data = CompaignDTO::from($data)->toArray();
13+
14+
// validate the data
15+
// $this->validate($data);
16+
17+
return LaravelSendy::post('/api/campaigns/create.php', $data);
1018
}
1119
}

src/Resources/Lists.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22

33
namespace Coderflex\LaravelSendy\Resources\Resources;
44

5+
use Coderflex\LaravelSendy\Facades\LaravelSendy;
6+
57
class Lists
68
{
9+
/**
10+
* Get all lists for a specific brand.
11+
*
12+
* @return array
13+
*/
714
public function get(int $brandId, bool $includeHidden = false)
815
{
9-
// Implementation to get lists
16+
$params = http_build_query([
17+
'brand_id' => $brandId,
18+
'include_hidden' => $includeHidden,
19+
]);
20+
21+
return LaravelSendy::get('/api/lists/get-lists.php', $params);
1022
}
1123
}

src/Resources/Subscribers.php

+37-5
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,57 @@
22

33
namespace Coderflex\LaravelSendy\Resources\Resources;
44

5+
use Coderflex\LaravelSendy\DTOs\SubscribersDTO;
6+
use Coderflex\LaravelSendy\Facades\LaravelSendy;
7+
58
class Subscribers
69
{
710
public function subscribe(array $data)
811
{
9-
//
12+
$data = SubscribersDTO::from($data)->toArray();
13+
14+
// validate the data
15+
16+
return LaravelSendy::post('subscribe', $data);
1017
}
1118

12-
public function unsubscribe(int $listId, string $email)
19+
public function unsubscribe(int $listId, string $email, bool $plainTextResponse)
1320
{
14-
//
21+
$data = http_build_query([
22+
'list' => $listId,
23+
'email' => $email,
24+
'boolean' => $plainTextResponse,
25+
]);
26+
27+
return LaravelSendy::post('/api/subscribers/unsubscribe.php', $data);
1528
}
1629

1730
public function delete(int $listId, string $email)
1831
{
19-
//
32+
$data = http_build_query([
33+
'list_id' => $listId,
34+
'email' => $email,
35+
]);
36+
37+
return LaravelSendy::post('/api/subscribers/delete.php', $data);
38+
}
39+
40+
public function status(int $listId, string $email)
41+
{
42+
$data = http_build_query([
43+
'list_id' => $listId,
44+
'email' => $email,
45+
]);
46+
47+
return LaravelSendy::post('/api/subscribers/subscription-status.php', $data);
2048
}
2149

2250
public function count(int $listId)
2351
{
24-
//
52+
$data = http_build_query([
53+
'list_id' => $listId,
54+
]);
55+
56+
return LaravelSendy::post('/api/subscribers/active-subscriber-count.php', $data);
2557
}
2658
}

0 commit comments

Comments
 (0)