Skip to content

Commit

Permalink
Merge pull request #10 from shahmal1yev/bss-14
Browse files Browse the repository at this point in the history
[BSS-14] New Client Mechanism
  • Loading branch information
shahmal1yev authored Sep 18, 2024
2 parents e4f0d12 + cdbe66a commit a8d6f83
Show file tree
Hide file tree
Showing 58 changed files with 1,141 additions and 386 deletions.
225 changes: 108 additions & 117 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,166 +1,157 @@
# BlueskySDK
<p align="center">
<img src="art/logo-small.webp" alt="Logo" />
</p>

# BlueSky SDK

![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/shahmal1yev/blueskysdk?label=latest&style=flat)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
![GitHub last commit](https://img.shields.io/github/last-commit/shahmal1yev/blueskysdk)
![GitHub issues](https://img.shields.io/github/issues/shahmal1yev/blueskysdk)
![GitHub stars](https://img.shields.io/github/stars/shahmal1yev/blueskysdk)
![GitHub forks](https://img.shields.io/github/forks/shahmal1yev/blueskysdk)
![GitHub contributors](https://img.shields.io/github/contributors/shahmal1yev/blueskysdk)

## Project Description

BlueskySDK is a PHP library used to interact with the Bluesky API. This library allows you to perform file uploads, create records, and other operations using the Bluesky API.
BlueSky SDK is a PHP library used for interacting with the [BlueSky API](https://docs.bsky.app/docs/get-started). This library allows you to perform various
operations using the BlueSky API.

## Requirements

- PHP 5.6 or newer
- Composer
- **PHP**: 7.4 or newer
- **Composer**: [Dependency management tool](https://getcomposer.org/) for PHP

## Installation

```shell
To install BlueSky SDK via Composer, use the following command:

```bash
composer require shahmal1yev/blueskysdk
```

## Usage

After including the library in your project, you can refer to the following examples:
Once installed, you can start using the SDK to interact with the BlueSky API. Below are examples of how to
authenticate and perform various operations using the library.

### Authentication and Basic Usage

### Get Profile
First, instantiate the `Client` class and authenticate using your BlueSky credentials:

```php
use Atproto\Clients\BlueskyClient;
use Atproto\API\App\Bsky\Actor\GetProfile;
use Atproto\Resources\App\Bsky\Actor\GetProfileResource;
use Atproto\Resources\Assets\LabelsAsset;
use Atproto\Resources\Assets\LabelAsset;
use Atproto\Resources\Assets\FollowersAsset;
use Atproto\Resources\Assets\FollowerAsset;
use Atproto\Client;
use Atproto\Resources\Com\Atproto\Server\CreateSessionResource;

$client = new BlueskyClient(new GetProfile());
$client = new Client();

$client->authenticate([
'identifier' => 'user@example.com',
'password' => 'password'
]);
// Authenticate using your identifier (e.g., email) and password
$client->authenticate($identifier, $password);

/** @var GetProfileResource $user */
$user = $client->send();

/** @var Carbon\Carbon $created */
$created = $user->createdAt();
// Once authenticated, you can retrieve the user's session resource
/** @var CreateSessionResource $session */
$session = $client->authenticated();
```

/** @var LabelsAsset<LabelAsset> $labels */
$labels = $user->labels();
### Making Requests

/** @var FollowersAsset $knownFollowers */
$knownFollowers = $user->viewer()
->knownFollowers()
->followers();
BlueSky SDK provides a fluent interface to construct API requests. Use chained method calls to navigate through the
API lexicons and forge the request:

foreach($knownFollowers as $follower) {
/** @var FollowerAsset $follower */

$name = $follower->displayName();
$createdAt = $follower->createdAt()->format(DATE_ATOM);

echo "$name's account created at $createdAt";
}
```php
use Atproto\Contracts\ResourceContract;

// Example: Fetching a profile
$profile = $client->app()
->bsky()
->actor()
->getProfile()
->forge()
->actor('some-actor-handle') // Specify the actor handle
->send();
```

### File Upload
### Handling Responses

BlueSky SDK supports both Resource and Castable interfaces, providing flexibility in handling API responses and
enabling smooth data manipulation and casting for a more streamlined development experience.

Responses are returned as resource instances that implement the `ResourceContract`. These resources provide methods
for accessing data returned by the API.

```php
use Atproto\API\Com\Atrproto\Repo\UploadBlobRequest;
use Atproto\Clients\BlueskyClient;
use Atproto\Auth\Strategies\PasswordAuthentication;
// Retrieve properties from the profile
/** @var string $displayName */
$displayName = $profile->displayName();

$client = new BlueskyClient(new UploadBlobRequest);
/** @var Carbon\Carbon $createdAt */
$createdAt = $profile->createdAt();
```

$client->setStrategy(new PasswordAuthentication)
->authenticate([
'identifier' => 'user@example.com',
'password' => 'password'
]);
### Working with Assets and Relationships

$client->getRequest()
->setBlob('/var/www/blueskysdk/assets/file.png');
BlueSky SDK allows you to access complex assets like followers and labels directly through the resource instances.

$response = $client->execute();
```php
use Atproto\Resources\Assets\FollowersAsset;
use Atproto\Resources\Assets\FollowerAsset;

// Fetch the user's followers
/** @var FollowersAsset<FollowerAsset> $followers */
$followers = $profile->viewer()
->knownFollowers()
->followers();

echo "Blob uploaded successfully. CID: {$response->cid}";
foreach ($followers as $follower) {
/** @var FollowerAsset $follower */
echo $follower->displayName() . " - Created at: " . $follower->createdAt()->format(DATE_ATOM) . "\n";
}
```

### Record Creation
### Example: Fetching Profile Information

Here is a more complete example of fetching and displaying profile information, including created dates and labels:

```php
use Atproto\API\Com\Atrproto\Repo\CreateRecordRequest;
use Atproto\Clients\BlueskyClient;
use Atproto\Auth\Strategies\PasswordAuthentication;
use Atproto\API\App\Bsky\Actor\GetProfile;
use Atproto\Resources\App\Bsky\Actor\GetProfileResource;

$client = new BlueskyClient(new CreateRecordRequest);
$client = new BlueskyClient(new GetProfile());

$client->authenticate([
'identifier' => 'user@example.com',
'password' => 'password'
]);

$client->setStrategy(new PasswordAuthentication)
->authenticate([
'identifier' => 'user@example.com',
'password' => 'password'
]);

$record = new \Atproto\Builders\Bluesky\RecordBuilder();
/** @var GetProfileResource $user */
$user = $client->send();

$record->addText("Hello World!")
->addText("")
->addText("I was sent via BlueskySDK: https://github.com/shahmal1yev/blueskysdk")
->addCreatedAt(date_format(date_create_from_format("d/m/Y", "08/11/2020"), "c"))
->addType();
// Output profile details
echo "Display Name: " . $user->displayName() . "\n";
echo "Created At: " . $user->createdAt()->toDateTimeString() . "\n";
echo "Labels: " . implode(', ', $user->labels()->pluck('name')->toArray()) . "\n";

$client->getRequest()
->setRecord($record);
// Accessing and iterating over followers
$followers = $user->viewer()->knownFollowers()->followers();

echo "Record created successfully. URI: {$response->uri}";
foreach ($followers as $follower) {
echo $follower->displayName() . " followed on " . $follower->createdAt()->format(DATE_ATOM) . "\n";
}
```
### Create Record (with blob)

```php
use Atproto\API\Com\Atrproto\Repo\UploadBlobRequest;
use Atproto\Auth\Strategies\PasswordAuthentication;
use Atproto\Clients\BlueskyClient;
use Atproto\API\Com\Atrproto\Repo\CreateRecordRequest;

$client = new BlueskyClient(new UploadBlobRequest);

$client->setStrategy(new PasswordAuthentication)
->authenticate([
'identifier' => 'user@example.com',
'password' => 'password'
]);

$client->getRequest()
->setBlob('/var/www/blueskysdk/assets/file.png')
->setHeaders([
'Content-Type' => $client->getRequest()
->getBlob()
->getMimeType()
]);

$image = $client->execute();

$client->setRequest(new CreateRecordRequest);

$record = (new \Atproto\Builders\Bluesky\RecordBuilder)
->addText("Hello World!")
->addText("")
->addText("I was sent from 'test BlueskyClient execute method with both UploadBlob and CreateRecord'")
->addText("")
->addText("Here are the pictures: ")
->addImage($image->blob, "Image 1: Alt text")
->addImage($image->blob, "Image 2: Alt text")
->addType()
->addCreatedAt();

$client->getRequest()
->setRecord($record);

$response = $client->execute();
```
### Extending the SDK

BlueSky SDK is built with extensibility in mind. You can add custom functionality by extending existing classes and
creating your own request and resource types. Follow the structure used in the SDK to maintain consistency.

## Contribution
- If you find any bug or issue, please open an issue.
- If you want to contribute to the code, feel free to submit a pull request.

We welcome contributions from the community! If you find any bugs or would like to add new features, feel free to:

- **Open an issue**: Report bugs, request features, or suggest improvements.
- **Submit a pull request**: Contributions to the codebase are welcome. Please follow best practices and ensure that your code adheres to the existing architecture and coding standards.

## License

This project is licensed under the MIT License. For more information, see the LICENSE file.
BlueSky SDK is licensed under the MIT License. See the LICENSE file for full details.
File renamed without changes
Binary file added art/logo-small.webp
Binary file not shown.
Binary file added art/logo.webp
Binary file not shown.
22 changes: 20 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
{
"name": "shahmal1yev/blueskysdk",
"description": "BlueskySDK is a PHP library for easy integration with the Bluesky API, enabling effortless file uploads, record creation, and authentication in PHP applications.",
"description": "BlueSky SDK is a PHP library used for interacting with the BlueSky API. This library allows you to perform various operations using the BlueSky API.",
"keywords": [
"bluesky",
"sdk",
"api",
"social-network",
"atproto",
"decentralized",
"php",
"client",
"library",
"wrapper",
"federation",
"microblogs",
"web3"
],
"minimum-stability": "stable",
"license": "mit",
"autoload": {
"psr-4": {
"Atproto\\": "src/",
"Tests\\": "tests/"
}
},
"files": [
"src/helpers.php"
]
},
"require-dev": {
"phpunit/phpunit": "9.6.20",
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/API/Com/Atrproto/Repo/UploadBlob.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class UploadBlob implements RequestContract
/** @var array The headers for the request. */
private $headers = [
'Content-Type' => '*/*',
'Accept' => 'application/json',s
'Accept' => 'application/json',
];

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Atproto;

use Atproto\Traits\Authentication;
use Atproto\Traits\Smith;

class Client
{
use Smith;
use Authentication;
protected static string $prefix = "Atproto\\HTTP\\API\\Requests\\";

public function prefix(): string
{
return self::$prefix;
}
}
6 changes: 6 additions & 0 deletions src/Contracts/HTTP/APIRequestContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@

interface APIRequestContract extends RequestContract
{
const API_BASE_URL = 'https://bsky.social';
const API_BASE_HEADERS = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
];

public function build(): RequestContract;
}
2 changes: 2 additions & 0 deletions src/Contracts/RequestContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ public function parameters($parameters = null);
* @return array|mixed The query parameters or instance for chaining
*/
public function queryParameters($queryParameters = null);

public function send();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Atproto\Exceptions\BlueskyException;

class MissingProvidedFieldException extends BlueskyException
class MissingFieldProvidedException extends BlueskyException
{
public function __construct($message = "", $code = 0, $previous = null)
{
Expand Down
Loading

0 comments on commit a8d6f83

Please sign in to comment.