Skip to content

Commit 1115546

Browse files
Refactor Authenticator and Fetcher classes for improved authorization handling
- Introduced a new method `getAuthorizationHeaderValue()` in the Authenticator class to return the authorization header value without the header name. - Updated the Fetcher class to utilize the new method for cleaner authorization header management. - Enhanced error handling in the HttpClient class by implementing a custom error handler to capture detailed error messages during HTTP requests.
1 parent a7eb17f commit 1115546

3 files changed

Lines changed: 52 additions & 13 deletions

File tree

src/Libraries/Authenticator.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,30 @@
55
class Authenticator
66
{
77
/**
8+
* Authorization 헤더 전체 문자열 반환 (헤더 이름 포함)
89
* @param string $apiKey
910
* @param string $apiSecretKey
10-
* @return string
11+
* @return string "Authorization: HMAC-SHA256 apiKey=..., date=..., salt=..., signature=..."
12+
* @deprecated Use getAuthorizationHeaderValue() for PSR-7 compatible header value
1113
*/
1214
public static function getAuthorizationHeaderInfo(string $apiKey, string $apiSecretKey): string
15+
{
16+
return "Authorization: " . self::getAuthorizationHeaderValue($apiKey, $apiSecretKey);
17+
}
18+
19+
/**
20+
* Authorization 헤더 값만 반환 (헤더 이름 제외)
21+
* @param string $apiKey
22+
* @param string $apiSecretKey
23+
* @return string "HMAC-SHA256 apiKey=..., date=..., salt=..., signature=..."
24+
*/
25+
public static function getAuthorizationHeaderValue(string $apiKey, string $apiSecretKey): string
1326
{
1427
date_default_timezone_set("Asia/Seoul");
1528
$date = date("Y-m-d\TH:i:s.Z\Z", time());
1629
$salt = uniqid();
1730
$signature = hash_hmac("sha256", $date . $salt, $apiSecretKey);
1831

19-
return "Authorization: HMAC-SHA256 apiKey={$apiKey}, date={$date}, salt={$salt}, signature={$signature}";
32+
return "HMAC-SHA256 apiKey={$apiKey}, date={$date}, salt={$salt}, signature={$signature}";
2033
}
2134
}

src/Libraries/Fetcher.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ public function __destruct()
5656
*/
5757
public function request(string $method, string $uri, $data = false)
5858
{
59-
$authHeaderInfo = Authenticator::getAuthorizationHeaderInfo($this->apiKey, $this->apiSecretKey);
60-
$headerParts = explode(': ', $authHeaderInfo, 2);
61-
$authHeaderValue = $headerParts[1] ?? $authHeaderInfo;
59+
$authHeaderValue = Authenticator::getAuthorizationHeaderValue($this->apiKey, $this->apiSecretKey);
6260

6361
$url = self::API_URL . $uri;
6462
$body = '';

src/Libraries/HttpClient.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,46 @@ public function sendRequest(RequestInterface $request): ResponseInterface
6060

6161
$context = stream_context_create($contextOptions);
6262

63-
$responseBody = @file_get_contents($url, false, $context);
63+
// 커스텀 에러 핸들러로 에러 캡처 (@ 연산자 대신)
64+
$errorMessage = null;
65+
set_error_handler(function ($severity, $message) use (&$errorMessage) {
66+
$errorMessage = $message;
67+
return true; // 기본 에러 핸들러 실행 방지
68+
});
69+
70+
$responseBody = file_get_contents($url, false, $context);
71+
72+
restore_error_handler();
73+
74+
// $http_response_header는 file_get_contents 호출 후 자동으로 설정됨
75+
$httpResponseHeader = $http_response_header ?? [];
6476

6577
if ($responseBody === false) {
66-
$error = error_get_last();
67-
throw new HttpException(
68-
$error['message'] ?? 'HTTP request failed: ' . $url
69-
);
78+
// 에러 메시지에 더 상세한 정보 포함
79+
$errorDetails = [];
80+
81+
if ($errorMessage !== null) {
82+
$errorDetails[] = $errorMessage;
83+
}
84+
85+
// HTTP 응답 헤더가 있으면 상태 정보 추가
86+
if (!empty($httpResponseHeader)) {
87+
$statusLine = $httpResponseHeader[0] ?? null;
88+
if ($statusLine !== null) {
89+
$errorDetails[] = "Response: {$statusLine}";
90+
}
91+
}
92+
93+
$detailedMessage = !empty($errorDetails)
94+
? implode(' | ', $errorDetails)
95+
: 'HTTP request failed: ' . $url;
96+
97+
throw new HttpException($detailedMessage);
7098
}
7199

72-
$statusCode = $this->parseStatusCode($http_response_header ?? []);
73-
$reasonPhrase = $this->parseReasonPhrase($http_response_header ?? []);
74-
$responseHeaders = $this->parseHeaders($http_response_header ?? []);
100+
$statusCode = $this->parseStatusCode($httpResponseHeader);
101+
$reasonPhrase = $this->parseReasonPhrase($httpResponseHeader);
102+
$responseHeaders = $this->parseHeaders($httpResponseHeader);
75103

76104
return new Response($statusCode, $responseHeaders, $responseBody, '1.1', $reasonPhrase);
77105
}

0 commit comments

Comments
 (0)