Skip to content

Commit 734cc34

Browse files
committed
made changes
1 parent 8dd00ec commit 734cc34

File tree

8 files changed

+278
-48
lines changed

8 files changed

+278
-48
lines changed

.continueignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ vendor/*
22
*.lock
33
*.cache
44
phpunit.phar
5+
.env
6+
.prompts/*

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ vendor/*
22
*.lock
33
*.cache
44
phpunit.phar
5+
.env
6+
.prompts/*

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"minimum-stability": "dev",
2424
"prefer-stable": true,
2525
"require-dev": {
26-
"phpunit/phpunit": "^11.4"
26+
"phpunit/phpunit": "^11.4",
27+
"vlucas/phpdotenv": "^v5.6.1",
28+
"kint-php/kint": "^5.1"
2729
}
2830
}

readme.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Multipurpose PHP Rest Client
2+
=============================
3+
4+
A multipurpose PHP rest client for consuming RESTful web services. This package is designed to be very simple and easy to use.
5+
6+
### Installation
7+
8+
You can install the package via composer:
9+
10+
```bash
11+
composer require ay4t/php-rest-client
12+
```
13+
14+
### Description
15+
Example usage of the `Client` class.
16+
17+
### Example Usage
18+
19+
```php
20+
21+
$config = new Config();
22+
$config->setBaseUri('https://api.openai.com/v1/')
23+
->setApiKey('your-api-key-here')
24+
// optional
25+
->setSecretKey('your-secret-key-here');
26+
27+
$client = new Client($config);
28+
$response = $client->cmd('GET', 'models');
29+
30+
echo '<pre>';
31+
print_r($response);
32+
echo '</pre>';
33+
```
34+
35+
or
36+
37+
```php
38+
$cmd = $client->cmd('POST', 'chat/completions', [
39+
'model' => 'llama-3.1-70b-versatile',
40+
'messages' => [
41+
[
42+
'role' => 'user',
43+
'content' => 'hi, why is sea water salty?',
44+
]
45+
]
46+
]);
47+
48+
echo '<pre>';
49+
print_r($cmd);
50+
echo '</pre>';
51+
```

src/Abstracts/AbstractClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ abstract class AbstractClient implements ClientInterface
1414
public function __construct(Config $config)
1515
{
1616
$this->config = $config;
17-
$this->client = new GuzzleClient(['base_uri' => $config->apiUrl]);
17+
$this->client = new GuzzleClient(['base_uri' => $config->getBaseUri()]);
1818
}
1919

2020
protected function prepareHeaders(): array
2121
{
2222
return [
23-
'Authorization' => 'Bearer ' . $this->config->apiKey,
23+
'Authorization' => 'Bearer ' . $this->config->getApiKey(),
2424
'Accept' => 'application/json',
2525
];
2626
}

src/Client.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ class Client extends AbstractClient implements ClientInterface
1212
{
1313
use RequestTrait;
1414

15+
/**
16+
* $request_options
17+
* @var array
18+
*/
19+
private $request_options = [];
20+
21+
/**
22+
* setRequestOptions
23+
* @param array $request_options
24+
* @return void
25+
*/
26+
public function setRequestOptions(array $request_options) {
27+
$this->request_options = $request_options;
28+
return $this;
29+
}
30+
31+
1532
/**
1633
* Perform a request to the API server.
1734
*
@@ -23,15 +40,25 @@ class Client extends AbstractClient implements ClientInterface
2340
*/
2441
public function cmd(string $method = 'GET', string $command, array $params = [])
2542
{
26-
$url = $this->config->apiUrl . '/' . $command;
27-
$options = [
28-
'headers' => $this->prepareHeaders(),
29-
'query' => $method === 'GET' ? $params : [],
30-
'json' => $method === 'POST' ? $params : [],
43+
$url = $this->config->getBaseUri() . '/' . $command;
44+
$requestOptions = [];
45+
46+
// Merge default options with user-provided options
47+
$defaultOptions = [
48+
'headers' => $this->prepareHeaders(),
3149
];
3250

51+
// Handle different HTTP methods and params
52+
if (in_array(strtoupper($method), ['GET', 'DELETE'])) {
53+
$defaultOptions['query'] = $params;
54+
} elseif (in_array(strtoupper($method), ['POST', 'PUT', 'PATCH'])) {
55+
$defaultOptions['json'] = $params;
56+
}
57+
58+
$requestOptions = array_merge($defaultOptions, $this->request_options);
59+
3360
try {
34-
$response = $this->client->request($method, $url, $options);
61+
$response = $this->client->request($method, $url, $requestOptions);
3562
return json_decode($response->getBody()->getContents(), $this->response_associative);
3663
} catch (GuzzleException $e) {
3764
// Error handling

src/Config/Config.php

Lines changed: 135 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,149 @@
44

55
class Config
66
{
7+
/**
8+
* The base URI of the API endpoint.
9+
* @var string
10+
*/
11+
private $baseUri;
12+
713
/**
814
* The API key used for authentication.
915
* @var string
1016
*/
11-
public $apiKey;
17+
private $apiKey;
1218

19+
/**
20+
* The secret key used for authentication (optional).
21+
* @var string|null
22+
*/
23+
private $secretKey;
1324

1425
/**
15-
* The secret key used for authentication.
16-
* @var string
26+
* Constructor.
27+
*
28+
* @param string|null $baseUri
29+
* @param string|null $apiKey
30+
* @param string|null $secretKey
31+
*/
32+
public function __construct(?string $baseUri = null, ?string $apiKey = null, ?string $secretKey = null)
33+
{
34+
if ($baseUri !== null) {
35+
$this->setBaseUri($baseUri);
36+
}
37+
if ($apiKey !== null) {
38+
$this->setApiKey($apiKey);
39+
}
40+
if ($secretKey !== null) {
41+
$this->setSecretKey($secretKey);
42+
}
43+
}
44+
45+
/**
46+
* Set the base URI for the API.
47+
*
48+
* @param string $baseUri
49+
* @return self
1750
*/
18-
public $secretKey;
51+
public function setBaseUri(string $baseUri): self
52+
{
53+
$this->validateBaseUri($baseUri);
54+
$this->baseUri = $baseUri;
55+
return $this;
56+
}
1957

2058
/**
21-
* The base URL of the API endpoint.
22-
* @var string
59+
* Set the API key.
60+
*
61+
* @param string $apiKey
62+
* @return self
2363
*/
24-
public $apiUrl;
25-
26-
private function validateParameters(string $apiKey, string $secretKey, string $apiUrl): bool
27-
{
28-
if (empty($apiKey) || empty($secretKey) || empty($apiUrl)) {
29-
throw new \InvalidArgumentException('API key, secret key, dan API URL tidak boleh kosong');
30-
}
31-
32-
if (!is_string($apiKey) || !is_string($secretKey) || !is_string($apiUrl)) {
33-
throw new \InvalidArgumentException('Semua parameter harus berupa string');
34-
}
35-
36-
if (strlen($apiKey) < 10 || strlen($secretKey) < 10) {
37-
throw new \InvalidArgumentException('API key dan secret key harus memiliki panjang minimal 10 karakter');
38-
}
39-
40-
if (!preg_match('/^https?:\/\//', $apiUrl)) {
41-
throw new \InvalidArgumentException('API URL tidak valid');
42-
}
43-
44-
return true;
45-
}
46-
47-
public function __construct(string $apiKey, string $secretKey, string $apiUrl)
48-
{
49-
if ($this->validateParameters($apiKey, $secretKey, $apiUrl)) {
50-
$this->apiKey = $apiKey;
51-
$this->secretKey = $secretKey;
52-
$this->apiUrl = $apiUrl;
53-
}
54-
}
55-
56-
}
64+
public function setApiKey(string $apiKey): self
65+
{
66+
$this->validateApiKey($apiKey);
67+
$this->apiKey = $apiKey;
68+
return $this;
69+
}
70+
71+
/**
72+
* Set the secret key (optional).
73+
*
74+
* @param string $secretKey
75+
* @return self
76+
*/
77+
public function setSecretKey(string $secretKey): self
78+
{
79+
$this->validateSecretKey($secretKey);
80+
$this->secretKey = $secretKey;
81+
return $this;
82+
}
83+
84+
/**
85+
* Get the base URI.
86+
*
87+
* @return string
88+
*/
89+
public function getBaseUri(): string
90+
{
91+
return $this->baseUri;
92+
}
93+
94+
/**
95+
* Get the API key.
96+
*
97+
* @return string
98+
*/
99+
public function getApiKey(): string
100+
{
101+
return $this->apiKey;
102+
}
103+
104+
/**
105+
* Get the secret key.
106+
*
107+
* @return string|null
108+
*/
109+
public function getSecretKey(): ?string
110+
{
111+
return $this->secretKey;
112+
}
113+
114+
/**
115+
* Validate the base URI.
116+
*
117+
* @param string $baseUri
118+
* @throws \InvalidArgumentException
119+
*/
120+
private function validateBaseUri(string $baseUri): void
121+
{
122+
if (empty($baseUri) || !filter_var($baseUri, FILTER_VALIDATE_URL)) {
123+
throw new \InvalidArgumentException('Base URI is not valid');
124+
}
125+
}
126+
127+
/**
128+
* Validate the API key.
129+
*
130+
* @param string $apiKey
131+
* @throws \InvalidArgumentException
132+
*/
133+
private function validateApiKey(string $apiKey): void
134+
{
135+
if (empty($apiKey) || strlen($apiKey) < 10) {
136+
throw new \InvalidArgumentException('API key must be at least 10 characters long');
137+
}
138+
}
139+
140+
/**
141+
* Validate the secret key.
142+
*
143+
* @param string $secretKey
144+
* @throws \InvalidArgumentException
145+
*/
146+
private function validateSecretKey(string $secretKey): void
147+
{
148+
if (!empty($secretKey) && strlen($secretKey) < 10) {
149+
throw new \InvalidArgumentException('Secret key, if provided, must be at least 10 characters long');
150+
}
151+
}
152+
}

tests/test.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Ay4t\RestClient\Client;
4+
use Ay4t\RestClient\Config\Config;
5+
use Kint\Kint;
6+
7+
require_once __DIR__ . '/../vendor/autoload.php';
8+
9+
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
10+
$env = $dotenv->load();
11+
12+
13+
/* Kint::dump( $env );
14+
die; */
15+
16+
$groq_api_key = $env['GROQ_API_KEY'];
17+
$groq_secret_key = $env['GROQ_SECRET_KEY'];
18+
$groq_api_url = $env['GROQ_API_URL'];
19+
20+
$config = new Config();
21+
$config->setBaseUri($groq_api_url)
22+
->setApiKey($groq_api_key);
23+
24+
$client = new Client($config);
25+
26+
/* list all models */
27+
$cmd = $client->cmd('GET', 'models', [
28+
29+
]);
30+
echo '<pre>';
31+
print_r($cmd);
32+
echo '</pre>';
33+
die;
34+
35+
/* chat/completions */
36+
$cmd = $client->cmd('POST', 'chat/completions', [
37+
'model' => 'llama-3.1-70b-versatile',
38+
'messages' => [
39+
[
40+
'role' => 'user',
41+
'content' => 'hi, why is sea water salty?',
42+
]
43+
]
44+
]);
45+
46+
47+
echo '<pre>';
48+
print_r($cmd);
49+
echo '</pre>';
50+
die;

0 commit comments

Comments
 (0)