Skip to content

Commit 7026e73

Browse files
Wouter0100m1guelpf
authored andcommitted
Fixed redirects for downloading artifacts
By keeping track of the last request, we know if it is being redirected. The following request will overwrite the last stored state. To be able to keep track of redirects, we need to be after the RedirectPlugin.
1 parent d6e7539 commit 7026e73

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/Gitlab/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ public function __construct(Builder $httpClientBuilder = null)
8282

8383
$this->httpClientBuilder->addPlugin(new GitlabExceptionThrower());
8484
$this->httpClientBuilder->addPlugin(new HistoryPlugin($this->responseHistory));
85-
$this->httpClientBuilder->addPlugin(new ApiVersion());
8685
$this->httpClientBuilder->addPlugin(new HeaderDefaultsPlugin([
8786
'User-Agent' => 'php-gitlab-api (http://github.com/m4tthumphrey/php-gitlab-api)',
8887
]));
8988
$this->httpClientBuilder->addPlugin(new RedirectPlugin());
89+
$this->httpClientBuilder->addPlugin(new ApiVersion());
9090

9191
$this->setUrl('https://gitlab.com');
9292
}

lib/Gitlab/HttpClient/Plugin/ApiVersion.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Http\Client\Common\Plugin;
77
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
89

910
/**
1011
* Prefix requests path with /api/v4/ if required.
@@ -13,17 +14,23 @@
1314
*/
1415
class ApiVersion implements Plugin
1516
{
17+
private $redirected = false;
18+
1619
/**
1720
* {@inheritdoc}
1821
*/
1922
public function handleRequest(RequestInterface $request, callable $next, callable $first)
2023
{
2124
$uri = $request->getUri();
2225

23-
if (substr($uri->getPath(), 0, 8) !== '/api/v4/') {
26+
if (substr($uri->getPath(), 0, 8) !== '/api/v4/' && !$this->redirected) {
2427
$request = $request->withUri($uri->withPath('/api/v4/'.$uri->getPath()));
2528
}
2629

27-
return $next($request);
30+
return $next($request)->then(function (ResponseInterface $response) {
31+
$this->redirected = $response->getStatusCode() === 302;
32+
33+
return $response;
34+
});
2835
}
2936
}

0 commit comments

Comments
 (0)