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

[Master] Add custom headers and parameters to the unsubscription request #636

Merged
merged 3 commits into from
Sep 15, 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
4 changes: 2 additions & 2 deletions ballerina/annotation.bal
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
# + unsubscribeOnShutdown - This flag notifies whether or not to initiate unsubscription when the service is shutting down
# + httpConfig - The configuration for the subscriber client used to interact with the discovered/specified hub
# + discoveryConfig - HTTP client configurations for resource discovery
# + customParams - Additional parameters, which need to be sent with the subscription request
# + customHeaders - Additional HTTP headers, which need to be sent with the subscription request
# + customParams - Additional parameters, which need to be sent with the subscription/unsubscription request
# + customHeaders - Additional HTTP headers, which need to be sent with the subscription/unsubscription request
# + servicePath - The generated service-path if the service-path is not provided. This is auto-generated at the compile-time.
public type SubscriberServiceConfiguration record {|
string|[string, string] target?;
Expand Down
13 changes: 7 additions & 6 deletions ballerina/subscriber_client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public isolated client class SubscriptionClient {
returns SubscriptionChangeResponse|SubscriptionInitiationError {
http:Client httpClient = self.httpClient;
SubscriptionPayload payload = buildSubscriptionPayload(MODE_UNSUBSCRIBE, unsubscriptionRequest);
http:Response|error response = httpClient->post("", payload, mediaType = mime:APPLICATION_FORM_URLENCODED);
http:Response|error response = httpClient->post("", payload,
headers = unsubscriptionRequest.customHeaders, mediaType = mime:APPLICATION_FORM_URLENCODED);
return processHubResponse(self.url, MODE_UNSUBSCRIBE, unsubscriptionRequest.topic, response);
}

Expand All @@ -91,11 +92,11 @@ isolated function buildSubscriptionPayload(string mode, SubscriptionChangeReques
if subscriptionReq.leaseSeconds != 0 {
payload.hub\.lease_seconds = subscriptionReq.leaseSeconds.toString();
}
map<string>? customParams = subscriptionReq.customParams;
if customParams is map<string> {
foreach var ['key, value] in customParams.entries() {
payload['key] = value;
}
}
map<string>? customParams = subscriptionReq.customParams;
if customParams is map<string> {
foreach var ['key, value] in customParams.entries() {
payload['key] = value;
}
}
return payload;
Expand Down
13 changes: 1 addition & 12 deletions ballerina/tests/subscription_initiation_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,6 @@ service /common on new http:Listener(9192) {
}
check caller->respond();
}

isolated resource function post hub/additional/unsub(http:Caller caller, http:Request request) returns error? {
map<string> params = check request.getFormParams();
foreach var ['key, _] in self.expectedAdditionalParams {
test:assertTrue(!params.hasKey('key), "unexpected additional parameter present");
}
foreach var [header, _] in self.expectedAdditionalHeaders {
test:assertTrue(!request.hasHeader(header), "unexpected additional header present");
}
check caller->respond();
}
}

isolated function getServiceAnnotationConfig(string|[string, string] target) returns SubscriberServiceConfiguration {
Expand Down Expand Up @@ -239,7 +228,7 @@ isolated function testUnSubscriptionInitiationDisable() returns error? {
}
isolated function testUnSubscriptionInitiationSuccessWithAdditionalParams() returns error? {
SubscriberServiceConfiguration config = {
target: ["http://127.0.0.1:9192/common/hub/additional/unsub", COMMON_TOPIC],
target: ["http://127.0.0.1:9192/common/hub/additional", COMMON_TOPIC],
leaseSeconds: 36000,
callback: CALLBACK,
unsubscribeOnShutdown: true
Expand Down
4 changes: 2 additions & 2 deletions docs/spec/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ should be `websub:SubscriberServiceConfig` a service-level-annotation for `websu
# + unsubscribeOnShutdown - This flag notifies whether or not to initiate unsubscription when the service is shutting down
# + httpConfig - The configuration for the subscriber client used to interact with the discovered/specified hub
# + discoveryConfig - HTTP client configurations for resource discovery
# + customParams - Additional parameters, which need to be sent with the subscription request
# + customHeaders - Additional HTTP headers, which need to be sent with the subscription request
# + customParams - Additional parameters, which need to be sent with the subscription/unsubscription request
# + customHeaders - Additional HTTP headers, which need to be sent with the subscription/unsubscription request
# + servicePath - The generated service-path if the service-path is not provided. This is auto-generated at the compile-time
public type SubscriberServiceConfiguration record {|
string|[string, string] target?;
Expand Down