Skip to content

Commit

Permalink
Merge pull request #4568 from dilanSachi/http-bbes
Browse files Browse the repository at this point in the history
Update http bbes with latest changes
  • Loading branch information
dilanSachi authored Jun 21, 2023
2 parents 943a19f + 2325a3b commit 7be6b18
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
$ curl -v localhost:9090/artist
* Trying ::1...
* Trying 127.0.0.1:9090...
* TCP_NODELAY set
* Connected to localhost (::1) port 9090 (#0)
* Connected to localhost (127.0.0.1) port 9090 (#0)
> GET /artist HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.64.1
> User-Agent: curl/7.68.0
> Accept: */*
>
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 502 Bad Gateway
< content-type: text/plain
< content-length: 35
< content-type: application/json
< content-length: 210
< server: ballerina
< date: Wed, 21 Dec 2022 13:30:30 +0530
<
< date: Fri, 16 Jun 2023 10:40:52 +0530
<
* Connection #0 to host localhost left intact
Something wrong with the connection
{"timestamp":"2023-06-16T05:10:52.220224Z", "status":502, "reason":"Bad Gateway", "message":"Something wrong with the connection: Connection refused: localhost/127.0.0.1:9091", "path":"/artist", "method":"GET"}
25 changes: 11 additions & 14 deletions examples/http-error-handling/http_error_handling.bal
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@ service class ResponseErrorInterceptor {
}
}

// Creates a new `ResponseErrorInterceptor`.
ResponseErrorInterceptor responseErrorInterceptor = new;

// A `ResponseErrorInterceptor` can be configured at the listener level or
// service level. Listener-level error interceptors can handle any error associated
// with the listener, whereas, service-level error interceptors can only handle
// errors occurred during the service execution.
listener http:Listener interceptorListener = new (9090,
// To handle all of the errors, the `ResponseErrorInterceptor` is added as a first
// interceptor as it has to be executed last.
interceptors = [responseErrorInterceptor]
);

service / on interceptorListener {
// Engage interceptors at the service level using an `http:InterceptableService`. The base path of the
// interceptor services is the same as the target service. Hence, they will be executed only for
// this particular service.
service http:InterceptableService / on new http:Listener(9090) {

// Service-level error interceptors can handle errors occurred during the service execution.
public function createInterceptors() returns ResponseErrorInterceptor {
// To handle all of the errors, the `ResponseErrorInterceptor` is added as a first
// interceptor as it has to be executed last.
return new ResponseErrorInterceptor();
}

// If the request does not have an`x-api-version` header, then an error will be returned
// and the execution will jump to the nearest `ResponseErrorInterceptor`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ service class RequestInterceptor {
}
}

RequestInterceptor requestInterceptor = new;

// A `RequestErrorInterceptor` service class implementation. It allows intercepting
// the error that occurred in the request path and handle it accordingly.
// A `RequestErrorInterceptor` service class can have only one resource function.
Expand All @@ -50,16 +48,13 @@ service class RequestErrorInterceptor {
}
}

// Creates a new `RequestErrorInterceptor`.
RequestErrorInterceptor requestErrorInterceptor = new;

listener http:Listener interceptorListener = new (9090,
// To handle all of the errors in the request path, the `RequestErrorInterceptor`
// is added as the last interceptor as it has to be executed last.
interceptors = [requestInterceptor, requestErrorInterceptor]
);
service http:InterceptableService / on new http:Listener(9090) {

service / on interceptorListener {
public function createInterceptors() returns [RequestInterceptor, RequestErrorInterceptor] {
// To handle all of the errors in the request path, the `RequestErrorInterceptor`
// is added as the last interceptor as it has to be executed last.
return [new RequestInterceptor(), new RequestErrorInterceptor()];
}

resource function get albums() returns Album[] {
return albums.toArray();
Expand Down
16 changes: 7 additions & 9 deletions examples/http-request-interceptor/http_request_interceptor.bal
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@ service class RequestInterceptor {
}
}

// Interceptors can also be engaged at the listener level. In this case, the `RequestInterceptors`
// can have only the default path.
listener http:Listener interceptorListener = new (9090);
// Engage interceptors at the service level using an `http:InterceptableService`. The interceptors
// will inherit the basepath of the service.
service http:InterceptableService / on new http:Listener(9090) {

// Engage interceptors at the service level using `http:InterceptableService`. The base path of the
// interceptor services is the same as the target service. Hence, they will be executed only for
// this particular service.
service http:InterceptableService / on interceptorListener {

// Creates the interceptor pipeline. The function can return a single interceptor or an array of interceptors as the interceptor pipeline. If the interceptor pipeline is an array, then the request interceptor services will be executed from head to tail.
// Creates the interceptor pipeline. The function can return a single interceptor or an array of
// interceptors as the interceptor pipeline. If the interceptor pipeline is an array, then, the
// request interceptor services will be executed from head to tail.
public function createInterceptors() returns RequestInterceptor {
return new RequestInterceptor();
}

resource function get albums() returns Album[] {
return albums.toArray();
}
Expand Down
4 changes: 2 additions & 2 deletions examples/http-request-interceptor/http_request_interceptor.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

The `http:RequestInterceptor` is used to intercept the request and execute custom logic. A `RequestInterceptor` is a service object with only one resource method, which is executed before dispatching the request to the actual resource in the target service. This resource method can have parameters just like a usual resource method in an `http:Service`.

A `RequestInterceptor` can be created from a service class, which includes the `http:RequestInterceptor` service type. Then, this service object can be engaged at the listener level by using the `interceptors` field in the `http:ListenerConfiguration` or at the service level by declaring a `http:InterceptableService` object.
A `RequestInterceptor` can be created from a service class, which includes the `http:RequestInterceptor` service type. Then, this service object can be engaged at the service level by declaring an `http:InterceptableService` object.

These accept an interceptor service object or an array of interceptor service objects as an interceptor pipeline, and the interceptors are executed in the order in which they are placed in the pipeline. Use `RequestInterceptors` to execute common logic such as logging, header manipulation, state publishing, etc., for inbound requests.
This accepts an interceptor service object or an array of interceptor service objects as an interceptor pipeline, and the interceptors are executed in the order in which they are placed in the pipeline. Use `RequestInterceptors` to execute common logic such as logging, header manipulation, state publishing, etc., for inbound requests.

::: code http_request_interceptor.bal :::

Expand Down
17 changes: 10 additions & 7 deletions examples/http-response-interceptor/http_response_interceptor.bal
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ service class ResponseInterceptor {
}
}

// Engage interceptors at the listener level. Response interceptor services will be executed from
// tail to head.
listener http:Listener interceptorListener = new (9090,
// This interceptor pipeline will be executed for all of the services attached to this listener.
interceptors = [new ResponseInterceptor()]
);
// Engage interceptors at the service level using an `http:InterceptableService`. The base path of the
// interceptor services is the same as the target service. Hence, they will be executed only for
// this particular service.
service http:InterceptableService / on new http:Listener(9090) {

service / on interceptorListener {
// Creates the interceptor pipeline. The function can return a single interceptor or an array of
// interceptors as the interceptor pipeline. If the interceptor pipeline is an array, then, the
// request interceptor services will be executed from head to tail.
public function createInterceptors() returns ResponseInterceptor {
return new ResponseInterceptor();
}

resource function get albums() returns Album[] {
return albums.toArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The `http:ResponseInterceptor` is used to intercept the response and execute custom logic. A `ResponseInterceptor` is a service object with a remote method called `interceptResponse`, which is executed before dispatching the response to the client. A `ResponseInterceptor` can be created from a service class, which includes the `http:ResponseInterceptor` service type.

This service object can be engaged at the listener level by using the `interceptors` field in the `http:ListenerConfiguration` or at the service level by declaring a `http:InterceptableService` object. These accept an interceptor service object or an array of interceptor service objects as an interceptor pipeline and the interceptors are executed in the order in which they are placed in the pipeline.
This service object can be engaged at the service level by declaring an `http:InterceptableService` object. This accepts an interceptor service object or an array of interceptor service objects as an interceptor pipeline and the interceptors are executed in the order in which they are placed in the pipeline.

Use `ResponseInterceptors` to execute common logic such as logging, header manipulation, state publishing, etc., for all outbound responses.

Expand Down

0 comments on commit 7be6b18

Please sign in to comment.