Beispiel #1
0
 /**
  * @param string $url
  * @param array $queries
  *
  * @throws InvalidRequest
  * @throws OverQueryLimit
  * @throws RequestDenied
  * @throws Unknown
  *
  * @return array
  */
 protected static function query(string $url, array $queries = []) : array
 {
     if (!self::$client) {
         self::$client = new HttpClient();
         $base = new Uri('https://maps.googleapis.com/maps/api');
         self::$client->setBaseUri($base);
     }
     if (!isset($queries['key'])) {
         $queries['key'] = DI::config()->get('googleMaps/apikey');
     }
     $uri = new Uri($url);
     $uri->addQueries($queries);
     $response = self::$client->get($uri->get());
     $data = json_decode($response->getBody(), true);
     if (!($data['status'] == 'OK' || $data['status'] == 'ZERO_RESULTS')) {
         switch ($data['status']) {
             case 'OVER_QUERY_LIMIT':
                 throw new OverQueryLimit($data['error_message'] ?? $data['status']);
             case 'REQUEST_DENIED':
                 throw new RequestDenied($data['error_message'] ?? $data['status']);
             case 'INVALID_REQUEST':
                 throw new InvalidRequest($data['error_message'] ?? $data['status']);
             default:
                 throw new Unknown($data['error_message'] ?? $data['status']);
         }
     }
     return $data;
 }
Beispiel #2
0
 /**
  * @return string
  */
 private function download()
 {
     $client = new HttpClient();
     // file download
     $this->output->writeln('Downloading database');
     $progress = new ProgressBar($this->output, 100);
     $progress->setMessage(0, 'currentsize');
     $progress->setMessage('???', 'totalsize');
     $progress->setFormat('<comment>[%bar%]</comment> %currentsize:6s% mo/%totalsize:6s% mo ' . '<info>%percent:3s%%</info> %elapsed:6s%/%estimated:-6s%');
     $progress->start();
     $client->getClient()->setOption(AbstractClient::OPTIONS_TIMEOUT, false)->setOption(AbstractClient::OPTIONS_ACCEPT_ENCODING, false)->setProgress(function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use($progress) {
         if ($download_size > 0) {
             $progress->setMessage(round($downloaded / 1024 / 1024, 3), 'currentsize');
             $progress->setMessage(round($download_size / 1024 / 1024, 3), 'totalsize');
             $progress->setProgress(floor($downloaded * 100 / $download_size));
         }
     });
     $zipFile = $client->get('http://geolite.maxmind.com/download/geoip/database/GeoLite2-City-CSV.zip');
     $progress->finish();
     $this->output->write("\n");
     if (strpos($zipFile->getBody(), "\n") === false) {
         $this->output->writeln(sprintf("<error>Invalid zip with content '%s'</error>", $zipFile->getBody()));
         return false;
     }
     $tmp = tempnam(sys_get_temp_dir(), 'maxmind');
     file_put_contents($tmp, $zipFile->getBody());
     $this->output->writeln(sprintf("Created tmp file with csv at '%s'", $tmp), OutputInterface::VERBOSITY_VERBOSE);
     return $tmp;
 }
 /**
  * @param string $name
  *
  * @return Cache
  */
 private static function httpClient(string $name = null) : Cache
 {
     if ($return = DI::get(__METHOD__, $name)) {
         return $return;
     }
     $config = DI::config()->get('httpclient/' . $name);
     if (is_callable($config)) {
         $item = $config();
     } else {
         $item = new HttpClient();
         $item->setBaseUri($config);
     }
     return DI::set(__METHOD__, $name, $item);
 }
Beispiel #4
0
 /**
  * {@inheritdoc}
  */
 public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = [], $method = 'POST')
 {
     if (!$this->client) {
         $this->client = new BaseHttpClient();
     }
     $request = new Request(new Uri($endpoint->getAbsoluteUri()));
     $request->setMethod($method);
     if ($requestBody && is_array($requestBody)) {
         $request->setPosts($requestBody);
     }
     foreach ($extraHeaders as $name => $value) {
         $request->addHeader($name, $value);
     }
     $response = $this->client->send($request);
     return $response->getBody();
 }