Skip to content

Commit

Permalink
Merge pull request #9 from shahmal1yev/bss-31
Browse files Browse the repository at this point in the history
[BSS-31] New Request Mechanism
  • Loading branch information
shahmal1yev authored Sep 18, 2024
2 parents 73358db + af2c2ba commit e4f0d12
Show file tree
Hide file tree
Showing 17 changed files with 1,074 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/API/App/Bsky/Actor/GetProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* The GetProfile class represents a request to retrieve a user's profile information from the Bluesky API.
*
* This class implements the RequestContract interface, providing methods to construct and handle the request.
*
* @deprecated This class deprecated and will be removed in a future version.
*/
class GetProfile implements RequestContract
{
Expand Down
8 changes: 8 additions & 0 deletions src/API/Com/Atrproto/Repo/CreateRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Atproto\Resources\Com\Atproto\Repo\CreateRecordResource;
use InvalidArgumentException;

/**
* @deprecated This class deprecated and will be removed in a future version.
*/
class CreateRecord implements RequestContract
{
/** @var object $body The request body */
Expand All @@ -21,6 +24,11 @@ class CreateRecord implements RequestContract

public function __construct()
{
trigger_error(
"This class deprecated and will be removed in a future version.",
E_USER_DEPRECATED
);

$this->body = (object) [
'repo' => '',
'rkey' => '',
Expand Down
9 changes: 8 additions & 1 deletion src/API/Com/Atrproto/Repo/UploadBlob.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* Class UploadBlobRequest
*
* Represents a request to upload a blob.
*
* @deprecated This class deprecated and will be removed in a future version.
*/
class UploadBlob implements RequestContract
{
Expand All @@ -24,7 +26,7 @@ class UploadBlob implements RequestContract
/** @var array The headers for the request. */
private $headers = [
'Content-Type' => '*/*',
'Accept' => 'application/json',
'Accept' => 'application/json',s
];

/**
Expand All @@ -34,6 +36,11 @@ class UploadBlob implements RequestContract
*/
public function __construct()
{
trigger_error(
"This class deprecated and will be removed in a future version.",
E_USER_DEPRECATED
);

$this->body = (object) [];
}

Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/HTTP/APIRequestContract.php
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;
}
93 changes: 93 additions & 0 deletions src/Contracts/RequestContract.php
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);
}
13 changes: 13 additions & 0 deletions src/Exceptions/Http/MissingProvidedFieldException.php
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);
}
}
17 changes: 17 additions & 0 deletions src/HTTP/API/APIRequest.php
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',
]);
}
}
71 changes: 71 additions & 0 deletions src/HTTP/API/Requests/App/Bsky/Actor/GetProfile.php
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 src/HTTP/API/Requests/Com/Atproto/Repo/CreateRecord.php
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;
}
}
Loading

0 comments on commit e4f0d12

Please sign in to comment.