This repository has been archived by the owner on Apr 4, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adopt fluent syntax for client calls (#15)
* switch to fluent builder style with dedicated client classes and a shared interface * Apply fixes from StyleCI * rename classes * apply change-requests to README.md * Apply suggestions from code review Add strict types Co-authored-by: Dries Vints <dries@vints.io> * change example for custom client * change argument order * Apply fixes from StyleCI * Cleanup * Move classes * Merge client classes * Fix Facade loading Co-authored-by: Tom Witkowski <Gummibeer@users.noreply.github.com> Co-authored-by: Dries Vints <dries@vints.io>
- Loading branch information
1 parent
cd46065
commit c9f55d7
Showing
13 changed files
with
287 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GitHub\Sponsors; | ||
|
||
use GitHub\Sponsors\Exceptions\BadCredentialsException; | ||
use GitHub\Sponsors\Exceptions\QueryException; | ||
use Illuminate\Http\Client\Factory; | ||
|
||
final class Client | ||
{ | ||
private Factory $http; | ||
|
||
private string $token; | ||
|
||
public function __construct(string $token, Factory $http = null) | ||
{ | ||
$this->http = $http ?? new Factory(); | ||
$this->token = $token; | ||
} | ||
|
||
public function viewer(): Viewer | ||
{ | ||
return new Viewer($this); | ||
} | ||
|
||
public function login(string $login): Login | ||
{ | ||
return new Login($this, $login); | ||
} | ||
|
||
public function send(string $query, array $variables = []): array | ||
{ | ||
$response = $this->http | ||
->withToken($this->token) | ||
->asJson() | ||
->accept('application/vnd.github.v4+json') | ||
->withUserAgent('github-php/sponsors') | ||
->post('https://api.github.com/graphql', [ | ||
'query' => $query, | ||
'variables' => $variables, | ||
]); | ||
|
||
if ($response->status() === 401) { | ||
throw BadCredentialsException::badToken(); | ||
} | ||
|
||
if ($response->clientError()) { | ||
throw QueryException::badQuery(); | ||
} | ||
|
||
return $response->json('data'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GitHub\Sponsors\Contracts; | ||
|
||
interface Sponsorable | ||
{ | ||
public function isSponsoredBy(string $sponsor): bool; | ||
|
||
public function isSponsoring(string $account): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GitHub\Sponsors\Facades; | ||
|
||
use GitHub\Sponsors\Client; | ||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* @method static \GitHub\Sponsors\Viewer viewer() | ||
* @method static \GitHub\Sponsors\Login login(string $login) | ||
* | ||
* @see \GitHub\Sponsors\Client | ||
*/ | ||
final class GitHubSponsors extends Facade | ||
{ | ||
protected static function getFacadeAccessor(): string | ||
{ | ||
return Client::class; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.