FOSSology  4.5.1
Open Source License Compliance by Open Source Software
HttpUtils.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2024 Siemens AG
4  Author:
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Util;
10 
12 use GuzzleHttp\Client;
13 use GuzzleHttp\Exception\GuzzleException;
14 use GuzzleHttp\Exception\RequestException;
15 
16 class HttpUtils
17 {
25  public static function getGuzzleClient(array $SysConf, string $baseUri, string $token = "")
26  {
27  $proxy = [];
28  if (array_key_exists('http_proxy', $SysConf['FOSSOLOGY']) &&
29  !empty($SysConf['FOSSOLOGY']['http_proxy'])) {
30  $proxy['http'] = $SysConf['FOSSOLOGY']['http_proxy'];
31  }
32  if (array_key_exists('https_proxy', $SysConf['FOSSOLOGY']) &&
33  !empty($SysConf['FOSSOLOGY']['https_proxy'])) {
34  $proxy['https'] = $SysConf['FOSSOLOGY']['https_proxy'];
35  }
36  if (array_key_exists('no_proxy', $SysConf['FOSSOLOGY']) &&
37  !empty($SysConf['FOSSOLOGY']['no_proxy'])) {
38  $proxy['no'] = explode(',', $SysConf['FOSSOLOGY']['no_proxy']);
39  }
40 
41  $version = $SysConf['BUILD']['VERSION'];
42  $headers = ['User-Agent' => "fossology/$version"];
43  if (!empty($token)) {
44  $headers['Authorization'] = 'Bearer ' . $token;
45  }
46 
47  return new Client([
48  'http_errors' => false,
49  'proxy' => $proxy,
50  'base_uri' => $baseUri,
51  'headers' => $headers,
52  ]);
53  }
54 
62  public static function checkLicenseDBHealth(string $getHealth, $guzzleClient)
63  {
64  try {
65  $response = $guzzleClient->get($getHealth);
66  if ($response->getStatusCode() === 200) {
67  return true;
68  }
69  } catch (RequestException|GuzzleException $e) {
70  return false;
71  }
72 
73  return false;
74  }
75 
89  public static function processHttpResponse($response)
90  {
91  $statusCode = $response->getStatusCode();
92  switch ($statusCode) {
93  case 200:
94  $data = json_decode($response->getBody()->getContents());
95  if ($data === null) {
96  if (json_last_error() !== JSON_ERROR_NONE) {
97  throw new HttpClientException("Error decoding JSON: " . json_last_error_msg());
98  }
99  throw new HttpClientException("No Data Found");
100  }
101  if (empty($data)) {
102  throw new HttpClientException("There is no Data Present in the Database");
103  }
104  return $data;
105  case 401:
106  throw new HttpClientException("Unauthorized access. Please check your credentials or token.");
107  case 403:
108  throw new HttpClientException("Access forbidden. You may not have the necessary permissions.");
109  case 404:
110  throw new HttpClientException("Resource not found. The requested URL may be incorrect.");
111  case 500:
112  throw new HttpClientException("Internal Server Error. Please try again later.");
113  default:
114  throw new HttpClientException("Unexpected status code: $statusCode");
115  }
116  }
117 }
Exception when Guzzle client response is not as expected.
static processHttpResponse($response)
Definition: HttpUtils.php:89
static getGuzzleClient(array $SysConf, string $baseUri, string $token="")
Definition: HttpUtils.php:25
static checkLicenseDBHealth(string $getHealth, $guzzleClient)
Definition: HttpUtils.php:62