Skip to content

Commit

Permalink
wip GoogleChat
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed Jan 30, 2024
1 parent ba55c1a commit 980d80c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
46 changes: 14 additions & 32 deletions src/GoogleChat/Credential.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,24 @@

namespace Guanguans\Notify\GoogleChat;

use Guanguans\Notify\Foundation\Credentials\NullCredential;
use GuzzleHttp\Psr7\HttpFactory;
use GuzzleHttp\UriTemplate\UriTemplate;
use Psr\Http\Message\RequestInterface;
use Guanguans\Notify\Foundation\Credentials\AggregateCredential;
use Guanguans\Notify\Foundation\Credentials\QueryCredential;
use Guanguans\Notify\Foundation\Credentials\UriTemplateCredential;

class Credential extends NullCredential
class Credential extends AggregateCredential
{
public const TEMPLATE_SPACE = '{space}';

private string $space;
private string $key;
private string $token;
private ?string $threadKey;

public function __construct(string $space, string $token, string $key, ?string $threadKey = null)
public function __construct(string $spaceId, string $key, string $token, ?string $threadKey = null)
{
$this->space = $space;
$this->key = $key;
$this->token = $token;
$this->threadKey = $threadKey;
$this->httpFactory = new HttpFactory;
}
$credentials = [
new UriTemplateCredential(['spaceId' => $spaceId]),
new QueryCredential('key', $key),
new QueryCredential('token', $token),
];

public function applyToRequest(RequestInterface $request): RequestInterface
{
return $request->withUri($this->httpFactory->createUri(
UriTemplate::expand(urldecode((string) $request->getUri()), ['space' => $this->space])
));
}

public function applyToOptions(array $options): array
{
$options['query']['token'] = $this->token;
$options['query']['key'] = $this->key;
$options['query']['thread_key'] = $this->threadKey;
if ($threadKey) {
$credentials[] = new QueryCredential('threadKey', $threadKey);
}

return $options;
parent::__construct(...$credentials);
}
}
3 changes: 1 addition & 2 deletions src/GoogleChat/Messages/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use Guanguans\Notify\Foundation\Concerns\AsJson;
use Guanguans\Notify\Foundation\Concerns\AsPost;
use Guanguans\Notify\GoogleChat\Credential;

/**
* @method \Guanguans\Notify\GoogleChat\Messages\Message text($text)
Expand Down Expand Up @@ -69,6 +68,6 @@ class Message extends \Guanguans\Notify\Foundation\Message

public function toHttpUri(): string
{
return sprintf('https://chat.googleapis.com/v1/spaces/%s/messages', Credential::TEMPLATE_SPACE);
return 'https://chat.googleapis.com/v1/spaces/{spaceId}/messages';
}
}

0 comments on commit 980d80c

Please sign in to comment.