Accessing headers to see rate limits #49
Replies: 2 comments 8 replies
-
Hi @PaulWebster, Thanks for reaching out and starting this discussion! If you want the truth, this can be handled quite easily. I had anticipated this might be necessary and implemented a method to handle headers: RequestHandler.php#L68 With just a few small adjustments, this functionality can be added. Diffdiff --git a/src/Lexicons/Traits/RequestHandler.php b/src/Lexicons/Traits/RequestHandler.php
index dd0b6f6..03ea58b 100644
--- a/src/Lexicons/Traits/RequestHandler.php
+++ b/src/Lexicons/Traits/RequestHandler.php
@@ -26,7 +26,10 @@ trait RequestHandler
$this->request();
$this->handle();
- return $this->content;
+ return [
+ $this->content,
+ $this->responseHeaders,
+ ];
}
/**
diff --git a/src/Responses/BaseResponse.php b/src/Responses/BaseResponse.php
index da5afdd..67aadcc 100644
--- a/src/Responses/BaseResponse.php
+++ b/src/Responses/BaseResponse.php
@@ -10,10 +10,11 @@ use Atproto\Traits\Castable;
trait BaseResponse
{
protected array $content = [];
+ protected array $headers = [];
- public function __construct(array $content)
+ public function __construct(array $response)
{
- $this->content = $content;
+ [$this->content, $this->headers] = $response;
}
/**
@@ -58,6 +59,29 @@ trait BaseResponse
return $this->parse($offset);
}
+ public function headers(): array
+ {
+ return $this->headers;
+ }
+
+ /**
+ * @param string $name
+ * @param mixed $defaultValue
+ * @return mixed
+ */
+ public function header(string $name, $defaultValue = null)
+ {
+ if (array_key_exists($name, $this->headers)) {
+ return $this->headers[$name];
+ }
+
+ if (is_callable($defaultValue)) {
+ return $defaultValue($this);
+ }
+
+ return $defaultValue;
+ }
+
public function resolve($name)
{
return @$this->get($name);
diff --git a/tests/Unit/Lexicons/App/Bsky/Actor/GetProfilesTest.php b/tests/Unit/Lexicons/App/Bsky/Actor/GetProfilesTest.php
index 3812c8d..983024a 100644
--- a/tests/Unit/Lexicons/App/Bsky/Actor/GetProfilesTest.php
+++ b/tests/Unit/Lexicons/App/Bsky/Actor/GetProfilesTest.php
@@ -87,7 +87,7 @@ class GetProfilesTest extends TestCase
public function testResourceMethodReturnsCorrectInstance(): void
{
- $data = ['actors' => []];
+ $data = [['actors' => []], []];
$resource = $this->request->response($data);
$this->assertInstanceOf(GetProfilesResponse::class, $resource, 'Resource method should return an instance of GetProfilesResource.');
}
diff --git a/tests/Unit/Responses/BaseResponseTest.php b/tests/Unit/Responses/BaseResponseTest.php
index f6bb926..94382b4 100644
--- a/tests/Unit/Responses/BaseResponseTest.php
+++ b/tests/Unit/Responses/BaseResponseTest.php
@@ -19,7 +19,8 @@ class BaseResponseTest extends TestCase
parent::setUp();
$this->resource = new TestableResponse([
- 'example' => 'some value',
+ ['example' => 'some value',],
+ ['foo' => 'bar']
]);
}
@@ -50,6 +51,35 @@ class BaseResponseTest extends TestCase
$this->assertInstanceOf(ExampleObject::class, $result);
}
+ public function testHeaderIsAccessible(): void
+ {
+ $this->assertSame('bar', $this->resource->header('foo'));
+ }
+
+ public function testHeadersMethodReturnsArray(): void
+ {
+ $this->assertIsArray($headers = $this->resource->headers());
+ $this->assertArrayHasKey('foo', $headers);
+ $this->assertNotEmpty($headers);
+ }
+
+ public function testHeaderMethodReturnsNullWhenHeaderNotFound(): void
+ {
+ $this->assertNull($this->resource->header('nonexistent'));
+ }
+
+ public function testHeaderMethodReturnsDefaultValueWhenHeaderNotFound(): void
+ {
+ $this->assertSame('default value', $this->resource->header(
+ 'nonexistent',
+ 'default value'
+ ));
+
+ $this->assertInstanceOf(ExampleObject::class, $this->resource->header(
+ 'nonexistent',
+ static fn (ResponseContract $response): ExampleObject => $response->resolve('example')
+ ));
+ }
}
class TestableResponse implements ResponseContract That said, I also want to try a few things that came to mind to:
I’d also love to hear your thoughts on this topic—it could be really helpful. Give me a little time for these checks, and I’ll send you a notification as soon as the release is ready. |
Beta Was this translation helpful? Give feedback.
-
Here it is, I think it's done now: https://github.com/shahmal1yev/blueskysdk/blob/36c02cdf6992f788c902c1041645b12689c83b9f/tests/Feature/SessionManagementTest.php I will publish a blog post about this after the release. Thanks for your contribution, @PaulWebster. I will note this in the release notes. |
Beta Was this translation helpful? Give feedback.
-
I am experimenting with blueskysdk - thanks for building it.
While testing I have been hit with a rate limit error from Bluesky. I had not posted much the content was mostly the same and I was deleting them by hand ... so perhaps I triggered some sort of bot protection at their end.
However, I cannot see what rate limits are being applied.
Their documentation suggest that there is more information in the HTTP headers.
Is there a way I can get the extra information via your API or do I have to edit your source to put more diagnostics in there?
Beta Was this translation helpful? Give feedback.
All reactions