Skip to content

Commit

Permalink
Merge pull request #636 from ayeshLK/master
Browse files Browse the repository at this point in the history
[Master] Add custom headers and parameters to the unsubscription request
  • Loading branch information
ayeshLK authored Sep 15, 2023
2 parents cb5b3ab + f78bace commit 1b17fd1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
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

0 comments on commit 1b17fd1

Please sign in to comment.