function showRepoTags(GithubService $api, AuthToken $authToken) { $username = '******'; $repo = 'GithubArtaxService'; $command = $api->listRepoTags($authToken, $username, $repo); $command->setPerPage(100); $repoTags = $command->execute(); $repoTagsContext = ShowRepoTagsContext::fromTags($repoTags); return JigExecutable::createWithSharedObjects("pages/showRepoTags", [$repoTagsContext]); }
use GithubService\Hydrator\HydratorException; require_once "testBootstrap.php"; $injector = createProvider(); $reactor = \Amp\reactor(); $cache = new NullResponseCache(); $client = new ArtaxClient(); $client->setOption(ArtaxClient::OP_MS_CONNECT_TIMEOUT, 5000); $client->setOption(ArtaxClient::OP_MS_KEEP_ALIVE_TIMEOUT, 1000); $githubAPI = new GithubService($client, $reactor, $cache, "Danack/test"); $token = @file_get_contents("../../GithubToken.txt"); $oauthToken = null; if ($token) { $oauthToken = new Oauth2Token($token); } try { $tagListRequest = $githubAPI->listRepoTags(null, "Danack", "GithubArtaxService"); $tagList = $tagListRequest->execute(); foreach ($tagList as $tag) { /** @var $tag \GithubService\Model\Tag */ printf("tag name %s, commmit %s \n", $tag->name, $tag->commit->sha); } $emojiResult = $githubAPI->listEmojis(null)->execute(); foreach ($emojiResult->emojis as $emoji) { echo $emoji->name . " \n"; } } catch (BadResponseException $bre) { echo "Bad response: " . $bre->getResponse()->getBody() . "\n"; echo "Uri was: " . $bre->getResponse()->getOriginalRequest()->getUri() . "\n"; } catch (HydratorException $dme) { echo $dme->displayProblem(); }
}; $noteURL = 'http://www.github.com/danack/GithubArtaxService'; $note = "Testing Oauth creation: " . time(); //This must be unique to create a new Oaut key // List of the scopes required. An empty list is used to get a token // with no access, just to avoid the 50 reqs/hour limit for unsigned // api calls. $scopes = [\GithubService\GithubArtaxService\GithubService::SCOPE_PUBLIC_REPO]; try { //Attempt to either create or retrieve an Oauth token $authResult = $github->createOrRetrieveAuth($username, $password, $enterPasswordCallback, $scopes, $note, $maxAttempts = 3); echo "Token is: " . $authResult->token . ", keep it secret.\n"; file_put_contents($tokenFileLocation, $authResult->token); echo "Token stored in " . $tokenFileLocation . "\n"; } catch (ArtaxServiceBuilder\BadResponseException $badResponseException) { echo "Something went wrong trying to retrieve an oauth token:\n"; echo $badResponseException->getMessage(); echo "Body is:\n"; var_dump($badResponseException->getResponse()->getBody()); echo "\n"; exit(-1); } //$token = $authResult; $token = new Oauth2Token($authResult->token); } // Okay we have an authorization, lets test it. // This api call will be signed and so able to do 5000 requests per hour $repoTags = $github->listRepoTags($token, 'Danack', 'GithubArtaxService')->execute(); foreach ($repoTags as $nextRepoTag) { echo "Found tag: " . $nextRepoTag->name . "\n"; }
<?php $autoloader = (require __DIR__ . '/../vendor/autoload.php'); use GithubService\GithubArtaxService\GithubService; use Amp\Artax\Client as ArtaxClient; use ArtaxServiceBuilder\ResponseCache\FileResponseCache; use GithubService\AuthToken\NullToken; $github = new GithubService(new ArtaxClient(), \Amp\reactor(), new FileResponseCache(__DIR__ . "/fileCache"), 'Danack/GithubArtaxService'); $command = $github->listRepoTags(new NullToken(), 'php', 'php-src'); $listRepoTags = $command->execute(); $statusCode = $command->getOriginalResponse()->getStatus(); if ($statusCode == 304) { echo "YAY! 304 response so data read from cache. This request did not count against the rate limit.\n"; } else { echo "Cache miss.\n"; } $rateLimit = $github->getRateLimit(); //Alternatively you can create the rate limit object from the original response //$rateLimit = \GithubService\RateLimit::createFromResponse($command->getOriginalResponse()); if ($rateLimit) { echo sprintf("Rate limit info:\n Remaining: %d\n Reset time: %d\n", $rateLimit->remaining, $rateLimit->resetTime); } else { echo "No rate limit information was in the response.\n"; }
/** * @param GithubService $api * @param AccessResponse $accessResponse * @param $username * @param $reponame */ function showAllTagsForRepo(GithubService $api, AccessResponse $accessResponse, $username, $reponame, $maxPages = 20) { $results = []; echo "Sorry, this needs updating. <br/>"; return; // What to do if an individual request in the batch fails $onError = function ($requestKey, Exception $error) { echo 'Error: (', $requestKey, ') ', get_class($error) . ": ", $error->getMessage(), "\n"; }; $command = $api->listRepoTags(new Oauth2Token($accessResponse->accessToken), $username, $reponame); $repoTags = $command->execute(); $results[] = $repoTags; $pages = []; if ($repoTags->pager) { $newPages = $repoTags->pager->getAllKnownPages(); foreach ($newPages as $newPage) { $pages[$newPage] = false; } } $finished = false; $count = 0; while ($finished == false) { $maxPages++; //TODO - this is in the wrong place, it's currently max iterations. if ($count > $maxPages) { break; } $requestsAndCallbacks = []; foreach ($pages as $uri => $alreadyFetched) { if ($alreadyFetched == false) { $pages[$uri] = true; $command = $api->listRepoTagsPaginate(new Oauth2Token(new Oauth2Token($accessResponse->accessToken)), $uri); $onResponse = function ($requestKey, Response $response) use($command, &$pages, &$results) { $newRepoTags = $command->processResponse($response); $results[] = $newRepoTags; if ($newRepoTags->pager) { $newPages = $newRepoTags->pager->getAllKnownPages(); foreach ($newPages as $newPage) { if (isset($pages[$newPage]) == false) { $pages[$newPage] = false; } } } }; $requestsAndCallbacks[] = [$command->createRequest(), $onResponse, $onError]; } } if (count($requestsAndCallbacks)) { $client = new ArtaxClient(); //$client->requestMultiVerse($requestsAndCallbacks); //$client->requestMulti($requestsAndCallbacks); } else { $finished = true; } } $mergedRepoTags = []; foreach ($results as $result) { foreach ($result->repoTags as $repoTag) { $mergedRepoTags[$repoTag->name] = $repoTag; } } return $mergedRepoTags; }