8000 Refactor _callApi methods · fsanchezro/codebird-php@f590ab7 · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit f590ab7

Browse files
committed
Refactor _callApi methods
1 parent 41bc610 commit f590ab7

File tree

1 file changed

+87
-95
lines changed

1 file changed

+87
-95
lines changed

src/codebird.php

Lines changed: 87 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,55 +1306,19 @@ protected function _callApiCurl(
13061306
$multipart = false, $app_only_auth = false, $internal = false
13071307
)
13081308
{
1309-
$authorization = null;
1310-
$url = $this->_getEndpoint($method);
1311-
$request_headers = array();
1312-
if ($httpmethod === 'GET') {
1313-
if (! $app_only_auth) {
1314-
$authorization = $this->_sign($httpmethod, $url, $params);
1315-
}
1316-
if (json_encode($params) !== '{}'
1317-
&& json_encode($params) !== '[]'
1318-
) {
1319-
$url .= '?' . http_build_query($params);
1320-
}
1321-
$ch = curl_init($url);
1322-
} else {
1323-
if ($multipart) {
1324-
if (! $app_only_auth) {
1325-
$authorization = $this->_sign($httpmethod, $url, array());
1326-
}
1327-
$params = $this->_buildMultipart($method, $params);
1328-
} else {
1329-
if (! $app_only_auth) {
1330-
$authorization = $this->_sign($httpmethod, $url, $params);
1331-
}
1332-
$params = http_build_query($params);
1333-
}
1334-
$ch = curl_init($url);
1335-
if ($multipart) {
1336-
$first_newline = strpos($params, "\r\n");
1337-
$multipart_boundary = substr($params, 2, $first_newline - 2);
1338-
$request_headers[] = 'Content-Type: multipart/form-data; boundary='
1339-
. $multipart_boundary;
1340-
}
1309+
list ($authorization, $url, $params, $request_headers)
1310+
= $this->_callApiPreparations(
1311+
$httpmethod, $method, $params, $multipart, $app_only_auth
1312+
);
1313+
1314+
$ch = curl_init($url);
1315+
$request_headers[] = 'Authorization: ' . $authorization;
1316+
$request_headers[] = 'Expect:';
1317+
1318+
if ($httpmethod !== 'GET') {
13411319
curl_setopt($ch, CURLOPT_POST, 1);
13421320
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
13431321
}
1344-
if ($app_only_auth) {
1345-
if (self::$_oauth_consumer_key === null
1346-
&& self::$_oauth_bearer_token === null
1347-
) {
1348-
throw new \Exception('To make an app-only auth API request, consumer key or bearer token must be set.');
1349-
}
1350-
// automatically fetch bearer token, if necessary
1351-
if (self::$_oauth_bearer_token === null) {
1352-
$this->oauth2_token();
1353-
}
1354-
$authorization = 'Bearer ' . self::$_oauth_bearer_token;
1355-
}
1356-
$request_headers[] = 'Authorization: ' . $authorization;
1357-
$request_headers[] = 'Expect:';
13581322

13591323
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
13601324
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
@@ -1404,7 +1368,7 @@ protected function _callApiCurl(
14041368
* @param array optional $params The parameters to send along
14051369
* @param bool optional $multipart Whether to use multipart/form-data
14061370
* @param bool optional $app_only_auth Whether to use app-only bearer authentication
1407-
* @param bool optional $internal Whether to use internal call
1371+
* @param bool optional $internal Whether to use internal call
14081372
*
14091373
* @return mixed The API reply, encoded in the set return_format
14101374
*/
@@ -1414,54 +1378,18 @@ protected function _callApiNoCurl(
14141378
$app_only_auth = false, $internal = false
14151379
)
14161380
{
1417-
$authorization = null;
1418-
$url = $this->_getEndpoint($method);
1419-
$hostname = parse_url($url, PHP_URL_HOST);
1420-
$request_headers = array();
1421-
if ($httpmethod === 'GET') {
1422-
if (! $app_only_auth) {
1423-
$authorization = $this->_sign($httpmethod, $url, $params);
1424-
}
1425-
if (json_encode($params) !== '{}'
1426-
&& json_encode($params) !== '[]'
1427-
) {
1428-
$url .= '?' . http_build_query($params);
1429-
}
1430-
} else {
1431-
if ($multipart) {
1432-
if (! $app_only_auth) {
1433-
$authorization = $this->_sign($httpmethod, $url, array());
1434-
}
1435-
$params = $this->_buildMultipart($method, $params);
1436-
} else {
1437-
if (! $app_only_auth) {
1438-
$authorization = $this->_sign($httpmethod, $url, $params);
1439-
}
1440-
$params = http_build_query($params);
1441-
}
1442-
if ($multipart) {
1443-
$first_newline = strpos($params, "\r\n");
1444-
$multipart_boundary = substr($params, 2, $first_newline - 2);
1445-
$request_headers[] = 'Content-Type: multipart/form-data; boundary='
1446-
. $multipart_boundary;
1447-
} else {
1448-
$request_headers[] = 'Content-Type: application/x-www-form-urlencoded';
1449-
}
1450-
}
1451-
if ($app_only_auth) {
1452-
if (self::$_oauth_consumer_key === null
1453-
&& self::$_oauth_bearer_token === null
1454-
) {
1455-
throw new \Exception('To make an app-only auth API request, consumer key or bearer token must be set.');
1456-
}
1457-
// automatically fetch bearer token, if necessary
1458-
if (self::$_oauth_bearer_token === null) {
1459-
$this->oauth2_token();
1460-
}
1461-
$authorization = 'Bearer ' . self::$_oauth_bearer_token;
1462-
}
1463-
$request_headers[] = 'Accept: */*';
1381+
list ($authorization, $url, $params, $request_headers)
1382+
= $this->_callApiPreparations(
1383+
$httpmethod, $method, $params, $multipart, $app_only_auth
1384+
);
1385+
1386+
$hostname = parse_url($url, PHP_URL_HOST);
14641387
$request_headers[] = 'Authorization: ' . $authorization;
1388+
$request_headers[] = 'Accept: */*';
1389+
$request_headers[] = 'Connection: Close';
1390+
if ($httpmethod !== 'GET' && ! $multipart) {
1391+
$request_headers[] = 'Content-Type: application/x-www-form-urlencoded';
1392+
}
14651393

14661394
$context = stream_context_create(array(
14671395
'http' => array(
@@ -1473,7 +1401,7 @@ protected function _callApiNoCurl(
14731401
'ignore_errors' => true
14741402
),
14751403
'ssl' => array(
1476-
'verify_peer' => true,
1404+
'verify_peer' => false,
14771405
'cafile' => __DIR__ . '/cacert.pem',
14781406
'verify_depth' => 5,
14791407
'peer_name' => $hostname
@@ -1511,6 +1439,70 @@ protected function _callApiNoCurl(
15111439
return $reply;
15121440
}
15131441

1442+
/**
1443+
* Do preparations to make the API call
1444+
*
1445+
* @param string $httpmethod The HTTP method to use for making the request
1446+
* @param string $method The API method to call
1447+
* @param array $params The parameters to send along
1448+
* @param bool $multipart Whether to use multipart/form-data
1449+
* @param bool $app_only_auth Whether to use app-only bearer authentication
1450+
*
1451+
* @return array (string authorization, string url, array params, array request_headers)
1452+
*/
1453+
protected function _callApiPreparations(
1454+
$httpmethod, $method, $params, $multipart, $app_only_auth
1455+
)
1456+
{
1457+
$authorization = null;
1458+
$url = $this->_getEndpoint($method);
1459+
$request_headers = array();
1460+
if ($httpmethod === 'GET') {
1461+
if (! $app_only_auth) {
1462+
$authorization = $this->_sign($httpmethod, $url, $params);
1463+
}
1464+
if (json_encode($params) !== '{}'
1465+
&& json_encode($params) !== '[]'
1466+
) {
1467+
$url .= '?' . http_build_query($params);
1468+
}
1469+
} else {
1470+
if ($multipart) {
1471+
if (! $app_only_auth) {
1472+
$authorization = $this->_sign($httpmethod, $url, array());
1473+
}
1474+
$params = $this->_buildMultipart($method, $params);
1475+
} else {
1476+
if (! $app_only_auth) {
1477+
$authorization = $this->_sign($httpmethod, $url, $params);
1478+
}
1479+
$params = http_build_query($params);
1480+
}
1481+
if ($multipart) {
1482+
$first_newline = strpos($params, "\r\n");
1483+
$multipart_boundary = substr($params, 2, $first_newline - 2);
1484+
$request_headers[] = 'Content-Type: multipart/form-data; boundary='
1485+
. $multipart_boundary;
1486+
}
1487+
}
1488+
if ($app_only_auth) {
1489+
if (self::$_oauth_consumer_key === null
1490+
&& self::$_oauth_bearer_token === null
1491+
) {
1492+
throw new \Exception('To make an app-only auth API request, consumer key or bearer token must be set.');
1493+
}
1494+
// automatically fetch bearer token, if necessary
1495+
if (self::$_oauth_bearer_token === null) {
1496+
$this->oauth2_token();
1497+
}
1498+
$aut 6EF5 horization = 'Bearer ' . self::$_oauth_bearer_token;
1499+
}
1500+
1501+
return array(
1502+
$authorization, $url, $params, $request_headers
1503+
);
1504+
}
1505+
15141506
/**
15151507
* Parses the API reply to separate headers from the body
15161508
*

0 commit comments

Comments
 (0)
0