Browse Source

Refactoring pour fournir une query string a l'appel de l'API et recuperer les headers pour pouvoir ensuite gerer la pagination

pull/5/head
Cerdic 10 months ago
parent
commit
11b4c699f9
  1. 106
      mirror.php

106
mirror.php

@ -38,6 +38,7 @@ $method = reset($args);
foreach ($url_orgs as $url_org) {
$repos = debardeur_connecteur_gitea_lister_repositories($url_org, time() - 10 * 60);
var_dump($url_org, count($repos));die();
$total = count($repos);
$nb_repositories += $total;
@ -112,7 +113,7 @@ function debardeur_connecteur_gitea_lister_repositories($url_organisation, $last
$org = gitea_organisation_from_url($url_organisation);
$method = "orgs/{$org}/repos";
$res = debardeur_json_api_call('gitea', $endpoint, $method, $last_modified_time);
$res = debardeur_json_api_call('gitea', $endpoint, $method, ['limit' => 50], $last_modified_time);
$repositories = [];
if ($res) {
@ -136,46 +137,103 @@ function debardeur_connecteur_gitea_lister_repositories($url_organisation, $last
return $repositories;
}
/**
* Appel d'une l'API JSON en get (type gitea/github) avec cache si possible
* (mais par defaut pas de cache)
*
* @param $endpoint
* @param $method
* @param string $endpoint
* @param string $method
* @param string|array $query
* @param null $last_modified_time
* @return false|mixed|string
*/
function debardeur_json_api_call($type, $endpoint, $method, $last_modified_time = null) {
function debardeur_json_api_call($type, $endpoint, $method, $query, $last_modified_time = null){
$res = debardeur_json_api_call_raw($type, $endpoint, $method, $query, $last_modified_time);
if ($res){
return $res['content'];
}
return false;
}
/**
* Appel d'une l'API JSON en get (type gitea/github) avec cache si possible
* (mais par defaut pas de cache)
*
* @param string $endpoint
* @param string $method
* @param string|array $query
* @param null $last_modified_time
* @return array
*/
function debardeur_json_api_call_raw($type, $endpoint, $method, $query, $last_modified_time = null){
$dir_cache = sous_repertoire(_DIR_DEBARDEUR_TMP, 'cache');
$dir_cache = sous_repertoire($dir_cache, $type);
$file_cache = $dir_cache . "api-" . md5("debardeur_json_api_call:$endpoint:$method") . ".json";
if (!is_null($last_modified_time)
and file_exists($file_cache)
and filemtime($file_cache) > $last_modified_time
and $json = file_get_contents($file_cache)
and ($json = json_decode($json, true)) !== false) {
return $json;
if (is_array($query)){
$query = http_build_query($query);
}
if ($query){
$query = '?' . ltrim($query, '?');
} else {
$query = '';
}
$url = $endpoint . $method;
$command = "curl --silent -L '$url' -H 'accept: application/json'";
$file_cache = $dir_cache . "api-" . md5("debardeur_json_api_call_raw:$endpoint:$method:$query") . ".json";
$output = [];
exec($command, $output);
$output = trim(implode("\n", $output));
$json = json_decode($output, true);
if (!is_null($last_modified_time)
and file_exists($file_cache)
and filemtime($file_cache)>$last_modified_time
and $res = file_get_contents($file_cache)
and ($res = json_decode($res, true))!==false){
return $res;
}
if ($json !== false and !is_null($json)) {
file_put_contents($file_cache, json_encode($json));
return $json;
$url = $endpoint . $method . $query;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-Debardeur');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json'));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$output = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
list($header, $content) = explode("\r\n\r\n", $output, 2);
$headers = [];
$header_lines = explode("\n", $header);
foreach ($header_lines as $header_line){
$header_line = explode(':', $header_line, 2);
if (count($header_line)==2){
list($k, $v) = $header_line;
$headers[$k] = trim($v);
}
}
else {
spip_log("Echec call API $url", 'debardeur_call_api' . _LOG_ERREUR);
$json = json_decode($content, true);
$res = [
'status' => false,
'header' => $headers,
'content' => $json,
];
if ($httpCode==200 and $json!==false and !is_null($json)){
$res['status'] = true;
file_put_contents($file_cache, json_encode($res));
} else {
spip_log("Echec call API $url:" . json_encode($res), 'debardeur_call_api' . _LOG_ERREUR);
}
return false;
return $res;
}
function gitea_endpoint_from_url($url){

Loading…
Cancel
Save