-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from shahmal1yev/bss-31
[BSS-31] New Request Mechanism
- Loading branch information
Showing
17 changed files
with
1,074 additions
and
2 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
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,10 @@ | ||
<?php | ||
|
||
namespace Atproto\Contracts\HTTP; | ||
|
||
use Atproto\Contracts\RequestContract; | ||
|
||
interface APIRequestContract extends RequestContract | ||
{ | ||
public function build(): RequestContract; | ||
} |
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,93 @@ | ||
<?php | ||
|
||
namespace Atproto\Contracts; | ||
|
||
/** | ||
* Interface RequestContract | ||
* | ||
* This interface defines the contract for an HTTP request. | ||
*/ | ||
interface RequestContract | ||
{ | ||
/** | ||
* Get the URL of the request. | ||
* | ||
* @return string The URL of the request | ||
*/ | ||
public function url(): string; | ||
|
||
/** | ||
* Get or set the host of the request. | ||
* | ||
* @param string|null $origin The host to set, or null to get the current host. | ||
* @return mixed|string The request host or instance for chaining | ||
*/ | ||
public function origin(string $origin = null); | ||
|
||
/** | ||
* Get or set the path of the request. | ||
* | ||
* @param string|null $path The path to set, or null to get the current path. | ||
* @return mixed|string The request path or instance for chaining | ||
*/ | ||
public function path(string $path = null); | ||
|
||
/** | ||
* Get or set the HTTP method of the request. | ||
* | ||
* @param string|null $method The HTTP method to set, or null to get the current method. | ||
* @return mixed|string The request method or instance for chaining | ||
*/ | ||
public function method(string $method = null); | ||
|
||
/** | ||
* Get or set a header in the request. | ||
* | ||
* @param string $name The header name | ||
* @param string|null $value The header value to set, or null to get the current value. | ||
* @return mixed|string|null The header value or instance for chaining | ||
*/ | ||
public function header(string $name, string $value = null); | ||
|
||
/** | ||
* Get or set a parameter in the request. | ||
* | ||
* @param string $name The parameter name | ||
* @param null $value The parameter value to set, or null to get the current value. | ||
* @return mixed|string|null The parameter value or instance for chaining | ||
*/ | ||
public function parameter(string $name, $value = null); | ||
|
||
/** | ||
* Get or set a query parameter in the request. | ||
* | ||
* @param string $name The query parameter name | ||
* @param string|null $value The query parameter value to set, or null to get the current value. | ||
* @return mixed|string|null The query parameter value or instance for chaining | ||
*/ | ||
public function queryParameter(string $name, string $value = null); | ||
|
||
/** | ||
* Get or set multiple headers at once. | ||
* | ||
* @param bool|array|null $headers The headers to set, or null to get the current headers. | ||
* @return array|mixed The headers or instance for chaining | ||
*/ | ||
public function headers($headers = null); | ||
|
||
/** | ||
* Get or set multiple parameters at once. | ||
* | ||
* @param bool|array|null $parameters The parameters to set, or null to get the current parameters. | ||
* @return array|mixed The parameters or instance for chaining | ||
*/ | ||
public function parameters($parameters = null); | ||
|
||
/** | ||
* Get or set multiple query parameters at once. | ||
* | ||
* @param bool|array|null $queryParameters The query parameters to set, or null to get the current query parameters. | ||
* @return array|mixed The query parameters or instance for chaining | ||
*/ | ||
public function queryParameters($queryParameters = null); | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Atproto\Exceptions\Http; | ||
|
||
use Atproto\Exceptions\BlueskyException; | ||
|
||
class MissingProvidedFieldException extends BlueskyException | ||
{ | ||
public function __construct($message = "", $code = 0, $previous = null) | ||
{ | ||
parent::__construct("Missing provided fields: $message", $code, $previous); | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
namespace Atproto\HTTP\API; | ||
|
||
use Atproto\Contracts\HTTP\APIRequestContract; | ||
use Atproto\HTTP\Request; | ||
|
||
abstract class APIRequest extends Request implements APIRequestContract | ||
{ | ||
public function __construct() | ||
{ | ||
$this->origin('https://public.api.bsky.app/xrpc/')->headers([ | ||
'Content-Type' => 'application/json', | ||
'Accept' => 'application/json', | ||
]); | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
|
||
namespace Atproto\HTTP\API\Requests\App\Bsky\Actor; | ||
|
||
use Atproto\Contracts\RequestContract; | ||
use Atproto\Exceptions\Http\MissingProvidedFieldException; | ||
use Atproto\HTTP\API\APIRequest; | ||
use Exception; | ||
|
||
class GetProfile extends APIRequest | ||
{ | ||
protected ?string $actor = null; | ||
protected ?string $token = null; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->path('/xrpc/app.bsky.actor.getProfile'); | ||
} | ||
|
||
/** | ||
* @return RequestContract|string | ||
*/ | ||
public function actor(string $actor = null) | ||
{ | ||
if (is_null($actor)) { | ||
return $this->actor; | ||
} | ||
|
||
$this->actor = $actor; | ||
|
||
$this->queryParameter('actor', $this->actor); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return RequestContract|string | ||
*/ | ||
public function token(string $token = null) | ||
{ | ||
if (is_null($token)) { | ||
return $this->token; | ||
} | ||
|
||
$this->token = $token; | ||
|
||
$this->header('Authorization', "Bearer $this->token"); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function build(): RequestContract | ||
{ | ||
$fields = ['actor', 'token']; | ||
$missing = array_filter($fields, function ($field) { | ||
if (is_null($this->$field)) { | ||
return true; | ||
} | ||
}); | ||
|
||
if (! empty($missing)) { | ||
throw new MissingProvidedFieldException(implode(", ", $missing)); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
src/HTTP/API/Requests/Com/Atproto/Repo/CreateRecord.php
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,108 @@ | ||
<?php | ||
|
||
namespace Atproto\HTTP\API\Requests\Com\Atproto\Repo; | ||
|
||
use Atproto\Contracts\RequestContract; | ||
use Atproto\Exceptions\Http\MissingProvidedFieldException; | ||
use Atproto\HTTP\API\APIRequest; | ||
|
||
class CreateRecord extends APIRequest | ||
{ | ||
protected array $required = [ | ||
'repo', | ||
'collection', | ||
'record' | ||
]; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$this->path('/com.atproto.repo.createRecord'); | ||
} | ||
|
||
public function repo(string $repo = null) | ||
{ | ||
if (is_null($repo)) { | ||
return $this->parameter('repo') ?? null; | ||
} | ||
|
||
$this->parameter('repo', $repo); | ||
|
||
return $this; | ||
} | ||
|
||
public function collection(string $collection = null) | ||
{ | ||
if (is_null($collection)) { | ||
return $this->parameter('collection') ?? null; | ||
} | ||
|
||
$this->parameter('collection', $collection); | ||
|
||
return $this; | ||
} | ||
|
||
public function rkey(string $rkey = null) | ||
{ | ||
if (is_null($rkey)) { | ||
return $this->parameter('rkey') ?? null; | ||
} | ||
|
||
$this->parameter('rkey', $rkey); | ||
|
||
return $this; | ||
} | ||
|
||
public function validate(bool $validate = null) | ||
{ | ||
if (is_null($validate)) { | ||
return $this->parameter('validate') ?? null; | ||
} | ||
|
||
$this->parameter('validate', $validate); | ||
|
||
return $this; | ||
} | ||
|
||
public function record(object $record = null) | ||
{ | ||
if (is_null($record)) { | ||
return $this->parameter('record') ?? null; | ||
} | ||
|
||
$this->parameter('record', $record); | ||
|
||
return $this; | ||
} | ||
|
||
public function swapCommit(string $swapCommit = null) | ||
{ | ||
if (is_null($swapCommit)) { | ||
return $this->parameter('swapCommit') ?? null; | ||
} | ||
|
||
$this->parameter('swapCommit', $swapCommit); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @throws MissingProvidedFieldException | ||
*/ | ||
public function build(): RequestContract | ||
{ | ||
$parameters = array_keys($this->parameters(false)); | ||
$missing = array_diff( | ||
$this->required, | ||
$parameters | ||
|
||
); | ||
|
||
if (count($missing)) { | ||
throw new MissingProvidedFieldException(implode(", ", $missing)); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.