Skip to content

Commit

Permalink
Enhance HTTP Request Methods in Request.php (#229)
Browse files Browse the repository at this point in the history
* Update Request.php

* Update RequestTest.php

* Update HealthChecks.php

* Update Logger.php

* Update OneSignal.php

* Create Constants.php

* style: format code with PHP CS Fixer

This commit fixes the style issues introduced in 472011b according to the output
from PHP CS Fixer.

Details: #229

* Update HealthChecks.php

* Update Logger.php

* Update OneSignal.php

---------

Co-authored-by: gstraccini[bot] <150967461+gstraccini[bot]@users.noreply.github.com>
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 2, 2024
1 parent 6af21cc commit 38113f4
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 20 deletions.
12 changes: 12 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace GuiBranco\Pancake;

class Constants
{
public const USER_AGENT_VENDOR = "Pancake/0.11 (+https://github.com/guibranco/pancake)";

public const USER_AGENT_HEADER = "User-Agent: Pancake/0.11 (+https://github.com/guibranco/pancake)";

public const CONTENT_TYPE_JSON_HEADER = "Content-Type: application/json; charset=utf-8";
}
6 changes: 3 additions & 3 deletions src/HealthChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function checkHeaders(): void
if ($this->headersSet) {
return;
}
$this->headers = ["User-Agent: Pancake/0.8 (+https://github.com/guibranco/pancake)", "Content-Type: application/json; charset=utf-8"];
$this->headers = [Constants::USER_AGENT_HEADER, Constants::CONTENT_TYPE_JSON_HEADER];
}

public function setHeaders($headers): void
Expand Down Expand Up @@ -98,14 +98,14 @@ public function fail(): stdClass
public function log($message): stdClass
{
$this->checkHeaders();
return $this->request->post($this->logUrl, $message, $this->headers);
return $this->request->post($this->logUrl, $this->headers, $message);
}

public function error($errorMessage): stdClass
{
$this->checkHeaders();
$this->failed = true;
return $this->request->post($this->failUrl, $errorMessage, $this->headers);
return $this->request->post($this->failUrl, $this->headers, $errorMessage);
}

public function end(): stdClass
Expand Down
12 changes: 6 additions & 6 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Logger implements ILogger
public function __construct($loggerUrl, $loggerApiKey, $loggerApiToken, $customUserAgent = null)
{
$this->headers = array(
"User-Agent: " . ($customUserAgent ?? "Pancake/0.9 (+https://github.com/guibranco/pancake)"),
"Content-Type: application/json; charset=UTF-8",
"X-API-KEY: " . $loggerApiKey,
"X-API-TOKEN: " . $loggerApiToken,
"X-Correlation-Id: " . GUIDv4::random()
"User-Agent: ".($customUserAgent ?? Constants::USER_AGENT_VENDOR),
Constants::CONTENT_TYPE_JSON_HEADER,
"X-API-KEY: ".$loggerApiKey,
"X-API-TOKEN: ".$loggerApiToken,
"X-Correlation-Id: ".GUIDv4::random()
);
$this->baseUrl = $loggerUrl;
$this->request = new Request();
Expand All @@ -35,7 +35,7 @@ public function log($message, $details): bool

$body = json_encode($caller);

$result = $this->request->post($this->baseUrl . "log-message", $body, $this->headers);
$result = $this->request->post($this->baseUrl . "log-message", $this->headers, $body);

return $result->statusCode == 200;
}
Expand Down
8 changes: 4 additions & 4 deletions src/OneSignal.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function __construct($token, $logger = null, $customUserAgent = null, $cu
$this->request = new Request();
$this->logger = $logger;
$this->headers = array(
"User-Agent: " . ($customUserAgent ?? "Pancake/0.8 (+https://github.com/guibranco/pancake)"),
"Content-Type: application/json; charset=utf-8",
"Authorization: Basic " . $token
"User-Agent: ".($customUserAgent ?? Constants::USER_AGENT_VENDOR),
Constants::CONTENT_TYPE_JSON_HEADER,
"Authorization: Basic ".$token
);
$this->endpoint = $customEndpoint ?? self::BASE_URL;
}
Expand All @@ -35,7 +35,7 @@ public function sendNotification($fields)

private function sendInternal($content, $headers, $isRetry = false)
{
$result = $this->request->post($this->endpoint . self::NOTIFICATIONS_ENDPOINT, $content, $headers);
$result = $this->request->post($this->endpoint . self::NOTIFICATIONS_ENDPOINT, $headers, $content);

if ($result->statusCode == 200) {
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function get($url, $headers = array()): stdClass
return $this->execute($fields);
}

public function post($url, $data, $headers = array()): stdClass
public function post($url, $headers = array(), $data = null): stdClass
{
$fields = $this->getFields($url, $headers);
$fields[CURLOPT_CUSTOMREQUEST] = "POST";
Expand All @@ -103,7 +103,7 @@ public function post($url, $data, $headers = array()): stdClass
return $this->execute($fields);
}

public function put($url, $data, $headers = array()): stdClass
public function put($url, $headers = array(), $data = null): stdClass
{
$fields = $this->getFields($url, $headers);
$fields[CURLOPT_CUSTOMREQUEST] = "PUT";
Expand All @@ -123,7 +123,7 @@ public function delete($url, $headers = array(), $data = null): stdClass
return $this->execute($fields);
}

public function patch($url, $data, $headers = array()): stdClass
public function patch($url, $headers = array(), $data = null): stdClass
{
$fields = $this->getFields($url, $headers);
$fields[CURLOPT_CUSTOMREQUEST] = "PATCH";
Expand Down
50 changes: 46 additions & 4 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,42 @@ public function testCanGet(): void
public function testCanPost(): void
{
$request = new Request();
$response = $request->post('https://httpbin.org/post', ['name' => 'GuiBranco']);
$response = $request->post('https://httpbin.org/post');
$this->assertEquals(200, $response->statusCode);
}

public function testCanPostWithPayload(): void
{
$request = new Request();
$response = $request->post('https://httpbin.org/post', array(), ['name' => 'GuiBranco']);
$this->assertEquals(200, $response->statusCode);
}

public function testCanPut(): void
{
$request = new Request();
$response = $request->put('https://httpbin.org/put', ['name' => 'GuiBranco']);
$response = $request->put('https://httpbin.org/put');
$this->assertEquals(200, $response->statusCode);
}

public function testCanPutWithPayload(): void
{
$request = new Request();
$response = $request->put('https://httpbin.org/put', array(), ['name' => 'GuiBranco']);
$this->assertEquals(200, $response->statusCode);
}

public function testCanPatch(): void
{
$request = new Request();
$response = $request->patch('https://httpbin.org/patch', ['name' => 'GuiBranco']);
$response = $request->patch('https://httpbin.org/patch');
$this->assertEquals(200, $response->statusCode);
}

public function testCanPatchWithPayload(): void
{
$request = new Request();
$response = $request->patch('https://httpbin.org/patch', array(), ['name' => 'GuiBranco']);
$this->assertEquals(200, $response->statusCode);
}

Expand Down Expand Up @@ -75,7 +96,28 @@ public function testCanGetWithHeaders(): void
public function testCanPostWithHeaders(): void
{
$request = new Request();
$response = $request->post('https://httpbin.org/post', json_encode(['name' => 'GuiBranco']), ['X-Test: test']);
$response = $request->post('https://httpbin.org/post', ['X-Test: test'], json_encode(['name' => 'GuiBranco']));
$this->assertEquals(200, $response->statusCode);
}

public function testCanPutWithHeaders(): void
{
$request = new Request();
$response = $request->put('https://httpbin.org/put', ['X-Test: test'], json_encode(['name' => 'GuiBranco']));
$this->assertEquals(200, $response->statusCode);
}

public function testCanPatchWithHeaders(): void
{
$request = new Request();
$response = $request->patch('https://httpbin.org/patch', ['X-Test: test'], json_encode(['name' => 'GuiBranco']));
$this->assertEquals(200, $response->statusCode);
}

public function testCanDeleteWithHeaders(): void
{
$request = new Request();
$response = $request->delete('https://httpbin.org/delete', ['X-Test: test'], json_encode(['name' => 'GuiBranco']));
$this->assertEquals(200, $response->statusCode);
}

Expand Down

0 comments on commit 38113f4

Please sign in to comment.