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

Introduce status code response binding in the client side #1915

Merged
merged 36 commits into from
Apr 8, 2024

Conversation

TharmiganK
Copy link
Contributor

@TharmiganK TharmiganK commented Mar 27, 2024

Purpose

$Subject

Fixes: ballerina-platform/ballerina-library#6100

Examples

  • Service sample:
import ballerina/http;

type Album record {|
    readonly string id;
    string name;
    string artist;
    string genre;
|};

table<Album> key(id) albums = table [
    {id: "1", name: "The Dark Side of the Moon", artist: "Pink Floyd", genre: "Progressive Rock"},
    {id: "2", name: "Back in Black", artist: "AC/DC", genre: "Hard Rock"},
    {id: "3", name: "The Wall", artist: "Pink Floyd", genre: "Progressive Rock"}
];

type ErrorMessage record {|
    string albumId;
    string message;
|};

type Headers record {|
    string user\-id;
    int req\-id;
|};

type AlbumNotFound record {|
    *http:NotFound;
    ErrorMessage body;
    Headers headers;
|};

type AlbumFound record {|
    *http:Ok;
    Album body;
    Headers headers;
|};

service /api on new http:Listener(9090) {

    resource function get albums/[string id]() returns AlbumFound|AlbumNotFound {
        if albums.hasKey(id) {
            return {
                body: albums.get(id),
                headers: {user\-id: "user-1", req\-id: 1}
            };
        }
        return {
            body: {albumId: id, message: "Album not found"},
            headers: {user\-id: "user-1", req\-id: 1}
        };
    }
}
  • Client sample
import ballerina/http;

type Album record {|
    readonly string id;
    string name;
    string artist;
    string genre;
|};

type ErrorMessage record {|
    string albumId;
    string message;
|};

type Headers record {|
    string user\-id;
    int req\-id;
|};

type AlbumNotFound record {|
    *http:NotFound;
    ErrorMessage body;
    Headers headers;
|};

type AlbumFound record {|
    *http:Ok;
    Album body;
    Headers headers;
|};

final http:Client albumClient = check new("http://localhost:9090/api");

public function main() returns error? {
    Album _ = check albumClient->/albums/'1;

    AlbumFound _ = check albumClient->/albums/'1;

    Album|AlbumNotFound _ = check albumClient->/albums/'1;

    AlbumFound|AlbumNotFound _ = check albumClient->/albums/'1;

    Album|http:Response _ = check albumClient->/albums/'1;
}

Checklist

Copy link

codecov bot commented Mar 27, 2024

Codecov Report

Attention: Patch coverage is 91.34328% with 29 lines in your changes are missing coverage. Please review.

Project coverage is 81.56%. Comparing base (ba6e691) to head (4e49fc3).
Report is 1 commits behind head on master.

Files Patch % Lines
...b/http/api/nativeimpl/ExternResponseProcessor.java 90.20% 21 Missing and 7 partials ⚠️
...ib/http/api/service/signature/AllHeaderParams.java 85.71% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1915      +/-   ##
============================================
- Coverage     81.78%   81.56%   -0.22%     
  Complexity      562      562              
============================================
  Files           393      387       -6     
  Lines         21516    21401     -115     
  Branches       4802     4855      +53     
============================================
- Hits          17597    17456     -141     
- Misses         2925     2942      +17     
- Partials        994     1003       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

…e-binding

# Conflicts:
#	ballerina-tests/http-advanced-tests/Ballerina.toml
#	ballerina-tests/http-advanced-tests/Dependencies.toml
#	ballerina-tests/http-client-tests/Ballerina.toml
#	ballerina-tests/http-client-tests/Dependencies.toml
#	ballerina-tests/http-dispatching-tests/Ballerina.toml
#	ballerina-tests/http-dispatching-tests/Dependencies.toml
#	ballerina-tests/http-interceptor-tests/Ballerina.toml
#	ballerina-tests/http-interceptor-tests/Dependencies.toml
#	ballerina-tests/http-misc-tests/Ballerina.toml
#	ballerina-tests/http-misc-tests/Dependencies.toml
#	ballerina-tests/http-resiliency-tests/Ballerina.toml
#	ballerina-tests/http-resiliency-tests/Dependencies.toml
#	ballerina-tests/http-security-tests/Ballerina.toml
#	ballerina-tests/http-security-tests/Dependencies.toml
#	ballerina-tests/http-service-tests/Ballerina.toml
#	ballerina-tests/http-service-tests/Dependencies.toml
#	ballerina-tests/http-test-common/Ballerina.toml
#	ballerina-tests/http-test-common/Dependencies.toml
#	ballerina-tests/http2-tests/Ballerina.toml
#	ballerina-tests/http2-tests/Dependencies.toml
#	ballerina/Ballerina.toml
#	ballerina/CompilerPlugin.toml
#	ballerina/Dependencies.toml
@TharmiganK TharmiganK added the Skip GraalVM Check This will skip the GraalVM compatibility check label Apr 3, 2024
@TharmiganK TharmiganK force-pushed the status-code-response-binding branch from f475b87 to 01bf7f3 Compare April 4, 2024 02:15
@TharmiganK TharmiganK marked this pull request as ready for review April 4, 2024 03:50
@TharmiganK TharmiganK removed the Skip GraalVM Check This will skip the GraalVM compatibility check label Apr 4, 2024
Copy link

sonarcloud bot commented Apr 8, 2024

Quality Gate Passed Quality Gate passed

Issues
2 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@TharmiganK TharmiganK added the Skip GraalVM Check This will skip the GraalVM compatibility check label Apr 8, 2024
@TharmiganK TharmiganK merged commit 4ee81da into master Apr 8, 2024
7 of 8 checks passed
@TharmiganK TharmiganK deleted the status-code-response-binding branch April 8, 2024 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Skip GraalVM Check This will skip the GraalVM compatibility check
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: HTTP status code response binding support in the HTTP client
3 participants