Skip to content

Instantly share code, notes, and snippets.

@TharmiganK
Last active June 7, 2024 06:33
Show Gist options
  • Save TharmiganK/d575aeb026f520123c59a4c2c768cad5 to your computer and use it in GitHub Desktop.
Save TharmiganK/d575aeb026f520123c59a4c2c768cad5 to your computer and use it in GitHub Desktop.
Ballerina conversion API
import ballerina/http;
type ConvertionRate record {|
string fromCurrency;
string toCurrency;
decimal conversionRate;
|};
type ConversionRateUpdate record {|
decimal conversionRate;
|};
type ConversionRates record {|
ConvertionRate[] conversionRates;
|};
type ConversionRateNotFound record {|
*http:NotFound;
record {|
string message;
string 'fromCurrency;
string toCurrency;
string...;
|} body;
|};
type ConversionResult record {|
decimal amount;
|};
type ConversionRateCreated record {|
*http:Created;
record {|
string message;
|} body;
|};
type ConversionRateUpdated record {|
*http:Ok;
record {|
string message;
|} body;
|};
type ConversionRateConflict record {|
*http:Conflict;
record {|
string message;
string 'fromCurrency;
string toCurrency;
|} body;
|};
type UnauthorizedError record {|
*http:Unauthorized;
record {|
string message;
|} body;
|};
service class Authorizer {
*http:RequestInterceptor;
resource function 'default [string... path](http:RequestContext ctx, @http:Header string X\-API\-KEY)
returns UnauthorizedError|http:NextService|error? {
// add your logic here
return ctx.next();
}
}
type BadRequest record {|
*http:BadRequest;
record {|
string message;
|} body;
|};
type InternalServerError record {|
*http:InternalServerError;
record {|
string message;
|} body;
|};
service class ErrorInterceptor {
*http:ResponseErrorInterceptor;
remote function interceptResponseError(error err, http:RequestContext ctx) returns BadRequest|InternalServerError {
// add your logic here
return <InternalServerError>{
body: {
message: err.message()
}
};
}
}
@http:ServiceConfig {
mediaTypeSubtypePrefix: "conversion.api"
}
service http:InterceptableService /api on new http:Listener(9090) {
public function createInterceptors() returns [Authorizer, ErrorInterceptor] {
return [new Authorizer(), new ErrorInterceptor()];
}
@http:ResourceConfig {
name: "conversion-rates",
linkedTo: [
{name: "conversion-rate", method: http:GET, relation: "get-conversion-rate"},
{name: "conversion", relation: "get-conversion"}
]
}
resource function get conversion\-rates() returns @http:Cache {maxAge: 18000} ConversionRates {
return {
conversionRates: []
};
}
@http:ResourceConfig {
name: "conversion-rate",
linkedTo: [
{name: "conversion-rate", method: http:PUT, relation: "update-conversion-rate"},
{name: "conversion-rate", method: http:DELETE, relation: "delete-conversion-rate"},
{name: "conversion", relation: "get-conversion"}
]
}
resource function get conversion\-rate(string 'from, string to) returns @http:Cache {maxAge: 18000} ConvertionRate|ConversionRateNotFound {
return {
fromCurrency: 'from,
toCurrency: to,
conversionRate: 1.25
};
}
@http:ResourceConfig {
name: "conversion",
linkedTo: [
{name: "conversion-rates", relation: "get-conversion-rates"},
{name: "conversion-rate", method: http:GET, relation: "get-conversion-rate"}
]
}
resource function get conversion(string 'from, string to, decimal amount) returns @http:Cache {maxAge: 18000} ConversionResult|ConversionRateNotFound {
return {
amount: amount * 1.25
};
}
@http:ResourceConfig {
name: "conversion-rate",
linkedTo: [
{name: "conversion-rate", method: http:GET, relation: "get-conversion-rate"},
{name: "conversion-rate", method: http:PUT, relation: "update-conversion-rate"},
{name: "conversion-rate", method: http:DELETE, relation: "delete-conversion-rate"},
{name: "conversion", relation: "get-conversion"}
]
}
resource function post conversion\-rate(ConvertionRate convertionRate) returns ConversionRateCreated|ConversionRateConflict {
return {
body: {
message: "Conversion rate created successfully"
}
};
}
@http:ResourceConfig {
name: "conversion-rate",
linkedTo: [
{name: "conversion-rate", method: http:GET, relation: "get-conversion-rate"},
{name: "conversion-rate", method: http:DELETE, relation: "delete-conversion-rate"},
{name: "conversion", relation: "get-conversion"}
]
}
resource function put conversion\-rate(string 'from, string to, ConversionRateUpdate convertionRate) returns ConversionRateUpdated|ConversionRateNotFound {
return {
body: {
message: "Conversion rate updated successfully"
}
};
}
@http:ResourceConfig {
name: "conversion-rate"
}
resource function delete conversion\-rate(string 'from, string to) returns http:NoContent|ConversionRateNotFound {
return {};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment