Replace Requests with HTTP Client (#13689)

This commit is contained in:
Jellyfrog
2022-01-26 23:04:26 +01:00
committed by GitHub
parent 0b85bceb34
commit 7d0bbb98eb
4 changed files with 30 additions and 110 deletions

View File

@@ -27,8 +27,7 @@
namespace LibreNMS\Util; namespace LibreNMS\Util;
use Exception; use Exception;
use Requests; use Illuminate\Support\Facades\Http;
use Requests_Response;
class GitHub class GitHub
{ {
@@ -112,9 +111,9 @@ class GitHub
*/ */
public function getRelease($tag) public function getRelease($tag)
{ {
$release = Requests::get($this->github . "/releases/tags/$tag", $this->getHeaders()); $release = Http::withHeaders($this->getHeaders())->get($this->github . "/releases/tags/$tag");
return json_decode($release->body, true); return $release->json();
} }
/** /**
@@ -122,8 +121,8 @@ class GitHub
*/ */
public function getPullRequest() public function getPullRequest()
{ {
$pull_request = Requests::get($this->github . "/pulls/{$this->pr}", $this->getHeaders()); $pull_request = Http::withHeaders($this->getHeaders())->get($this->github . "/pulls/{$this->pr}");
$this->pr = json_decode($pull_request->body, true); $this->pr = $pull_request->json();
} }
/** /**
@@ -180,9 +179,8 @@ class GitHub
} }
GRAPHQL; GRAPHQL;
$data = json_encode(['query' => $query]); $prs = Http::withHeaders($this->getHeaders())->post($this->graphql, ['query' => $query]);
$prs = Requests::post($this->graphql, $this->getHeaders(), $data); $prs = $prs->json();
$prs = json_decode($prs->body, true);
if (! isset($prs['data'])) { if (! isset($prs['data'])) {
var_dump($prs); var_dump($prs);
} }
@@ -367,14 +365,14 @@ GRAPHQL;
$this->createChangelog(false); $this->createChangelog(false);
} }
$release = Requests::post($this->github . '/releases', $this->getHeaders(), json_encode([ $release = Http::withHeaders($this->getHeaders())->post($this->github . '/releases', [
'tag_name' => $this->tag, 'tag_name' => $this->tag,
'target_commitish' => $updated_sha, 'target_commitish' => $updated_sha,
'body' => $this->markdown, 'body' => $this->markdown,
'draft' => false, 'draft' => false,
])); ]);
return $release->status_code == 201; return $release->status() == 201;
} }
/** /**
@@ -420,19 +418,18 @@ GRAPHQL;
* @param string $file Path in git repo * @param string $file Path in git repo
* @param string $contents new file contents * @param string $contents new file contents
* @param string $message The commit message * @param string $message The commit message
* @return Requests_Response
*/ */
private function pushFileContents($file, $contents, $message) private function pushFileContents($file, $contents, $message): string
{ {
$existing = Requests::get($this->github . '/contents/' . $file, $this->getHeaders()); $existing = Http::withHeaders($this->getHeaders())->get($this->github . '/contents/' . $file);
$existing_sha = json_decode($existing->body)->sha; $existing_sha = $existing->json()['sha'];
$updated = Requests::put($this->github . '/contents/' . $file, $this->getHeaders(), json_encode([ $updated = Http::withHeaders($this->getHeaders())->put($this->github . '/contents/' . $file, [
'message' => $message, 'message' => $message,
'content' => base64_encode($contents), 'content' => base64_encode($contents),
'sha' => $existing_sha, 'sha' => $existing_sha,
])); ]);
return json_decode($updated->body)->commit->sha; return $updated->json()['commit']['sha'];
} }
} }

View File

@@ -49,7 +49,6 @@
"php-flasher/flasher-laravel": "^0.9", "php-flasher/flasher-laravel": "^0.9",
"phpmailer/phpmailer": "~6.0", "phpmailer/phpmailer": "~6.0",
"predis/predis": "^1.1", "predis/predis": "^1.1",
"rmccue/requests": "^1.7",
"symfony/yaml": "^4.0", "symfony/yaml": "^4.0",
"tecnickcom/tcpdf": "^6.4", "tecnickcom/tcpdf": "^6.4",
"tightenco/ziggy": "^0.9" "tightenco/ziggy": "^0.9"

62
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "cba65087f343b0ca7fa81ea5cb3dc1b5", "content-hash": "17d76cfe55a8adb13cb6df7c52662b86",
"packages": [ "packages": [
{ {
"name": "amenadiel/jpgraph", "name": "amenadiel/jpgraph",
@@ -4657,66 +4657,6 @@
], ],
"time": "2021-09-25T23:10:38+00:00" "time": "2021-09-25T23:10:38+00:00"
}, },
{
"name": "rmccue/requests",
"version": "v1.8.1",
"source": {
"type": "git",
"url": "https://github.com/WordPress/Requests.git",
"reference": "82e6936366eac3af4d836c18b9d8c31028fe4cd5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/WordPress/Requests/zipball/82e6936366eac3af4d836c18b9d8c31028fe4cd5",
"reference": "82e6936366eac3af4d836c18b9d8c31028fe4cd5",
"shasum": ""
},
"require": {
"php": ">=5.2"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"php-parallel-lint/php-console-highlighter": "^0.5.0",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpcompatibility/php-compatibility": "^9.0",
"phpunit/phpunit": "^4.8 || ^5.7 || ^6.5 || ^7.5",
"requests/test-server": "dev-master",
"squizlabs/php_codesniffer": "^3.5",
"wp-coding-standards/wpcs": "^2.0"
},
"type": "library",
"autoload": {
"psr-0": {
"Requests": "library/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"ISC"
],
"authors": [
{
"name": "Ryan McCue",
"homepage": "http://ryanmccue.info"
}
],
"description": "A HTTP library written in PHP, for human beings.",
"homepage": "http://github.com/WordPress/Requests",
"keywords": [
"curl",
"fsockopen",
"http",
"idna",
"ipv6",
"iri",
"sockets"
],
"support": {
"issues": "https://github.com/WordPress/Requests/issues",
"source": "https://github.com/WordPress/Requests/tree/v1.8.1"
},
"time": "2021-06-04T09:56:25+00:00"
},
{ {
"name": "spomky-labs/base64url", "name": "spomky-labs/base64url",
"version": "v2.0.4", "version": "v2.0.4",

View File

@@ -9,6 +9,7 @@
*/ */
use App\Models\Device; use App\Models\Device;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use LibreNMS\Config; use LibreNMS\Config;
use LibreNMS\Exceptions\HostExistsException; use LibreNMS\Exceptions\HostExistsException;
@@ -942,26 +943,6 @@ function set_curl_proxy($curl)
\LibreNMS\Util\Proxy::applyToCurl($curl); \LibreNMS\Util\Proxy::applyToCurl($curl);
} }
/**
* Return the proxy url in guzzle format
*
* @return 'tcp://' + $proxy
*/
function get_guzzle_proxy()
{
return \LibreNMS\Util\Proxy::forGuzzle();
}
/**
* Return the proxy url
*
* @return array|bool|false|string
*/
function get_proxy()
{
return \LibreNMS\Util\Proxy::get();
}
function target_to_id($target) function target_to_id($target)
{ {
if ($target[0] . $target[1] == 'g:') { if ($target[0] . $target[1] == 'g:') {
@@ -1436,9 +1417,9 @@ function cache_mac_oui()
//$mac_oui_url_mirror = 'https://raw.githubusercontent.com/wireshark/wireshark/master/manuf'; //$mac_oui_url_mirror = 'https://raw.githubusercontent.com/wireshark/wireshark/master/manuf';
echo ' -> Downloading ...' . PHP_EOL; echo ' -> Downloading ...' . PHP_EOL;
$get = Requests::get($mac_oui_url, [], ['proxy' => get_proxy()]); $get = Http::withOptions(['proxy' => Proxy::forGuzzle()])->get($mac_oui_url);
echo ' -> Processing CSV ...' . PHP_EOL; echo ' -> Processing CSV ...' . PHP_EOL;
$csv_data = $get->body; $csv_data = $get->body();
foreach (explode("\n", $csv_data) as $csv_line) { foreach (explode("\n", $csv_data) as $csv_line) {
unset($oui); unset($oui);
$entry = str_getcsv($csv_line, "\t"); $entry = str_getcsv($csv_line, "\t");
@@ -1494,8 +1475,8 @@ function cache_peeringdb()
$ix_keep = []; $ix_keep = [];
foreach (dbFetchRows('SELECT `bgpLocalAs` FROM `devices` WHERE `disabled` = 0 AND `ignore` = 0 AND `bgpLocalAs` > 0 AND (`bgpLocalAs` < 64512 OR `bgpLocalAs` > 65535) AND `bgpLocalAs` < 4200000000 GROUP BY `bgpLocalAs`') as $as) { foreach (dbFetchRows('SELECT `bgpLocalAs` FROM `devices` WHERE `disabled` = 0 AND `ignore` = 0 AND `bgpLocalAs` > 0 AND (`bgpLocalAs` < 64512 OR `bgpLocalAs` > 65535) AND `bgpLocalAs` < 4200000000 GROUP BY `bgpLocalAs`') as $as) {
$asn = $as['bgpLocalAs']; $asn = $as['bgpLocalAs'];
$get = Requests::get($peeringdb_url . '/net?depth=2&asn=' . $asn, [], ['proxy' => get_proxy()]); $get = Http::withOptions(['proxy' => Proxy::forGuzzle()])->get($peeringdb_url . '/net?depth=2&asn=' . $asn);
$json_data = $get->body; $json_data = $get->body();
$data = json_decode($json_data); $data = json_decode($json_data);
$ixs = $data->{'data'}[0]->{'netixlan_set'}; $ixs = $data->{'data'}[0]->{'netixlan_set'};
foreach ($ixs as $ix) { foreach ($ixs as $ix) {
@@ -1515,8 +1496,8 @@ function cache_peeringdb()
$pdb_ix_id = dbInsert($insert, 'pdb_ix'); $pdb_ix_id = dbInsert($insert, 'pdb_ix');
} }
$ix_keep[] = $pdb_ix_id; $ix_keep[] = $pdb_ix_id;
$get_ix = Requests::get("$peeringdb_url/netixlan?ix_id=$ixid", [], ['proxy' => get_proxy()]); $get_ix = Http::withOptions(['proxy' => Proxy::forGuzzle()])->get("$peeringdb_url/netixlan?ix_id=$ixid");
$ix_json = $get_ix->body; $ix_json = $get_ix->body();
$ix_data = json_decode($ix_json); $ix_data = json_decode($ix_json);
$peers = $ix_data->{'data'}; $peers = $ix_data->{'data'};
foreach ($peers as $index => $peer) { foreach ($peers as $index => $peer) {
@@ -1706,20 +1687,23 @@ function oxidized_node_update($hostname, $msg, $username = 'not_provided')
$postdata = ['user' => $username, 'msg' => $msg]; $postdata = ['user' => $username, 'msg' => $msg];
$oxidized_url = Config::get('oxidized.url'); $oxidized_url = Config::get('oxidized.url');
if (! empty($oxidized_url)) { if (! empty($oxidized_url)) {
Requests::put("$oxidized_url/node/next/$hostname", [], json_encode($postdata), ['proxy' => Proxy::get($oxidized_url)]); $response = Http::withOptions(['proxy' => Proxy::forGuzzle($oxidized_url)])->put("$oxidized_url/node/next/$hostname", $postdata);
return true; if ($response->successful()) {
return true;
}
} }
return false; return false;
}//end oxidized_node_update() }//end oxidized_node_update()
/** /**
* Take a BGP error code and subcode to return a string representation of it
*
* @params int code * @params int code
* @params int subcode * @params int subcode
* *
* @return string * @return string
* Take a BGP error code and subcode to return a string representation of it
*/ */
function describe_bgp_error_code($code, $subcode) function describe_bgp_error_code($code, $subcode)
{ {