Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tryAdd to RequestHeaders #81

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/RequestHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ public function add(string $key, string $value): void
}
}

/**
* Try to add a value to the existing values for that specific header if it's not already set.
* @param string $key
* @param string $value
* @return boolean if the value have been added
*/
public function tryAdd(string $key, string $value): bool
{
$lowercaseKey = strtolower($key);
if (array_key_exists($lowercaseKey, $this->headers)) {
return false;
} else {
$this->headers[$lowercaseKey] = [$value => true];
return true;
}
}

/**
* Returns the lowercase version of a string.
* @param string $key
Expand Down
10 changes: 5 additions & 5 deletions src/RequestInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function removeRequestOptions(RequestOption ...$options): void {
*/
public function setStreamContent(StreamInterface $value): void {
$this->content = $value;
$this->headers->add(self::$contentTypeHeader, self::$binaryContentType);
$this->headers->tryAdd(self::$contentTypeHeader, self::$binaryContentType);
}

/**
Expand All @@ -171,7 +171,7 @@ public function setContentFromParsable(RequestAdapter $requestAdapter, string $c
$writer = $requestAdapter->getSerializationWriterFactory()->getSerializationWriter($contentType);
$writer->writeObjectValue(null, $value);
$span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, get_class($value));
$this->headers->add(self::$contentTypeHeader, $contentType);
$this->headers->tryAdd(self::$contentTypeHeader, $contentType);
$this->content = $writer->getSerializedContent();
$span->setStatus(StatusCode::STATUS_OK);
} catch (Exception $exception) {
Expand Down Expand Up @@ -205,7 +205,7 @@ public function setContentFromParsableCollection(RequestAdapter $requestAdapter,
if (!empty($values)) {
$span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, get_class($values[0]));
}
$this->headers->add(self::$contentTypeHeader, $contentType);
$this->headers->tryAdd(self::$contentTypeHeader, $contentType);
$this->content = $writer->getSerializedContent();
} catch (Exception $exception) {
throw new RuntimeException('could not serialize payload.', 1, $exception);
Expand All @@ -232,7 +232,7 @@ public function setContentFromScalar(RequestAdapter $requestAdapter, string $con
$writer->writeAnyValue(null, $value);
$span->setAttribute(self::$contentTypeHeader, $contentType);
$span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, gettype($value));
$this->headers->add(self::$contentTypeHeader, $contentType);
$this->headers->tryAdd(self::$contentTypeHeader, $contentType);
$this->content = $writer->getSerializedContent();
$span->setStatus(StatusCode::STATUS_OK);
} catch (Exception $exception) {
Expand Down Expand Up @@ -265,7 +265,7 @@ public function setContentFromScalarCollection(RequestAdapter $requestAdapter, s
if (!empty($values)) {
$span->setAttribute(ObservabilityOptions::REQUEST_TYPE_KEY, gettype($values[0]));
}
$this->headers->add(self::$contentTypeHeader, $contentType);
$this->headers->tryAdd(self::$contentTypeHeader, $contentType);
$this->content = $writer->getSerializedContent();
$span->setStatus(StatusCode::STATUS_OK);
} catch (Exception $exception) {
Expand Down
11 changes: 11 additions & 0 deletions tests/RequestHeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public function testCanAdd(): void
$this->assertEquals(["value", "value2", "VALUE2"], $headers->get($key));
}

public function testCanTryAdd(): void
{
$headers = new RequestHeaders();
$key = "key";
$this->assertTrue($headers->tryAdd($key, "value"));
$this->assertEquals(["value"], $headers->get($key));

$this->assertFalse($headers->tryAdd($key, "value2"));
$this->assertEquals(["value"], $headers->get($key));
}

public function testCanPutAll(): void
{
$headers = new RequestHeaders();
Expand Down