Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158<?php
declare(strict_types=1);
namespace Wotnak\Atproto;
use Psr\Http\Client\ClientInterface;use Psr\Http\Message\RequestFactoryInterface;use Psr\Http\Message\StreamFactoryInterface;use Psr\SimpleCache\CacheInterface;use Wotnak\Atproto\Client\AuthenticatedSessionInterface;use Wotnak\Atproto\Client\AuthenticatedXrpcRequestSender;use Wotnak\Atproto\Client\Psr18XrpcRequestSender;use Wotnak\Atproto\Client\XrpcClient;use Wotnak\Atproto\Client\XrpcRequestSenderInterface;use Wotnak\Atproto\Identity\DidResolver;use Wotnak\Atproto\Identity\DnsTxtResolverInterface;use Wotnak\Atproto\Identity\HandleResolver;use Wotnak\Atproto\Identity\NativeDnsTxtResolver;use Wotnak\Atproto\Security\OutboundUrlPolicy;use Wotnak\Atproto\Security\OutboundUrlPolicyInterface;
/** * Builds AT Protocol clients from framework-provided dependencies. * * @api */final readonly class AtprotoClientFactory {
public function __construct( private ClientInterface $httpClient, private RequestFactoryInterface $requestFactory, private StreamFactoryInterface $streamFactory, private ?CacheInterface $cache = NULL, private DnsTxtResolverInterface $dnsTxtResolver = new NativeDnsTxtResolver(), private int|\DateInterval|null $identityCacheTtl = 3600, private OutboundUrlPolicyInterface $urlPolicy = new OutboundUrlPolicy(), ) {}
/** * Creates an anonymous client for an AT Protocol service. */ public function create( ?string $defaultServiceUrl = NULL, int|\DateInterval|null $identityCacheTtl = NULL, ?OutboundUrlPolicyInterface $urlPolicy = NULL, ): AtprotoClient { return $this->createWithSender( new Psr18XrpcRequestSender($this->httpClient), $defaultServiceUrl, $identityCacheTtl, $urlPolicy, ); }
/** * Creates a client bound to an authenticated session. */ public function createAuthenticated( AuthenticatedSessionInterface $session, ?string $defaultServiceUrl = NULL, int|\DateInterval|null $identityCacheTtl = NULL, ?OutboundUrlPolicyInterface $urlPolicy = NULL, ): AtprotoClient { $serviceUrl = $defaultServiceUrl ?? $session->getPdsUrl(); return $this->createWithSender( new AuthenticatedXrpcRequestSender(new Psr18XrpcRequestSender($this->httpClient), $session, $serviceUrl), $serviceUrl, $identityCacheTtl, $urlPolicy, ); }
/** * Creates a DID resolver with the factory dependencies. */ public function createDidResolver( int|\DateInterval|null $identityCacheTtl = NULL, ?OutboundUrlPolicyInterface $urlPolicy = NULL, string $cacheKeyPrefix = 'atproto.did_document', ): DidResolver { $identityCacheTtl ??= $this->identityCacheTtl; return new DidResolver( $this->httpClient, $this->requestFactory, $this->cache, $identityCacheTtl, $urlPolicy ?? $this->urlPolicy, cacheKeyPrefix: $cacheKeyPrefix, ); }
/** * Creates a handle resolver with the factory dependencies. */ public function createHandleResolver( int|\DateInterval|null $identityCacheTtl = NULL, ?OutboundUrlPolicyInterface $urlPolicy = NULL, string $cacheKeyPrefix = 'atproto.handle_did', ): HandleResolver { $identityCacheTtl ??= $this->identityCacheTtl; return new HandleResolver( $this->httpClient, $this->requestFactory, $this->dnsTxtResolver, cache: $this->cache, cacheTtl: $identityCacheTtl, urlPolicy: $urlPolicy ?? $this->urlPolicy, cacheKeyPrefix: $cacheKeyPrefix, ); }
/** * Creates an authoritative Lexicon repository. */ public function createLexiconRepository( int|\DateInterval|null $cacheTtl = 86_400, ?OutboundUrlPolicyInterface $urlPolicy = NULL, ): AuthoritativeLexiconRepository { $policy = $urlPolicy ?? $this->urlPolicy; return new AuthoritativeLexiconRepository( $this->dnsTxtResolver, $this->createDidResolver($cacheTtl, $policy, 'atproto.lexicon_publisher_did'), new XrpcClient( new Psr18XrpcRequestSender($this->httpClient), $this->requestFactory, $this->streamFactory, urlPolicy: $policy, ), $this->cache, $cacheTtl, ); }
private function createWithSender( XrpcRequestSenderInterface $requestSender, ?string $defaultServiceUrl = NULL, int|\DateInterval|null $identityCacheTtl = NULL, ?OutboundUrlPolicyInterface $urlPolicy = NULL, ): AtprotoClient { $identityCacheTtl ??= $this->identityCacheTtl; $xrpc = new XrpcClient( $requestSender, $this->requestFactory, $this->streamFactory, $defaultServiceUrl, $urlPolicy ?? $this->urlPolicy, );
return new AtprotoClient( $xrpc, $this->createHandleResolver($identityCacheTtl, $urlPolicy), $this->createDidResolver($identityCacheTtl, $urlPolicy), ); }
}