Skip to content

Instantly share code, notes, and snippets.

View ldclakmal's full-sized avatar
🎯
Focusing

Chanaka Lakmal ldclakmal

🎯
Focusing
View GitHub Profile
import ballerina/http;
http:Client webAppClient = check new("https://localhost:9090",
auth = {
tokenUrl: "https://localhost:9443/oauth2/token",
clientId: "FlfJYKBD2c925h4lkycqNZlC2l4a",
clientSecret: "PJz0UhTJMrHOo68QQNpvnqAY_3Aa",
scopes: ["customer"],
clientConfig: {
secureSocket: {
import ballerina/grpc;
import ballerina/jwt;
listener grpc:Listener paymentsEP = new(9191,
secureSocket = {
key: {
certFile: "/path/to/public.crt",
keyFile: "/path/to/private.key"
}
}
import ballerina/grpc;
GrpcClient clientEP = check new("https://localhost:9191",
auth = {
issuer: "order-service",
audience: ["payment-service", "delivery-service"],
keyId: "5a0b754-895f-4279-8843-b745e11a57e9",
jwtId: "JlbmMiOiJBMTI4Q0JDLUhTMjU2In",
customClaims: { "scp": "admin" },
expTime: 3600,
import ballerina/http;
listener http:Listener listenerEP = new(9091,
secureSocket = {
key: {
certFile: "/path/to/server-public.crt",
keyFile: "/path/to/server-private.key"
},
mutualSsl: {
verifyClient: http:REQUIRE,
import ballerina/http;
http:Client clientEP = check new("https://localhost:9092",
secureSocket = {
cert: "/path/to/client-public.crt",
key: {
certFile: "/path/to/server-public.crt",
keyFile: "/path/to/server-private.key"
}
}
import ballerina/http;
import ballerina/graphql;
listener http:Listener apiGateway = new(9090,
secureSocket = {
key: {
certFile: "/path/to/public.crt",
keyFile: "/path/to/private.key"
}
}
@ldclakmal
ldclakmal / custom-eviction-policy.bal
Created June 6, 2020 15:06
Custom Eviction Policy of Ballerina
public type CustomEvictionPolicy object {
*cache:AbstractEvictionPolicy;
public function get(LinkedList list, Node node) {
// custom implementation related to updating the
// `LinkedList` based on the get operation.
}
}
@ldclakmal
ldclakmal / custom-cache.bal
Created June 6, 2020 15:05
Custom Cache of Ballerina
public type CustomCache object {
*AbstractCache;
private AbstractEvictionPolicy evictionPolicy;
private LinkedList list;
public function __init(AbstractEvictionPolicy evictionPolicy) {
self.evictionPolicy = evictionPolicy;
self.list = {
@ldclakmal
ldclakmal / cache-sample-2.bal
Created June 6, 2020 15:05
Cache Usage with Error Handling
cache:Error? result = cache.put("key1", "value1");
if (result is cache:Error) {
// implement what to do, if any error happen when inserting
// item to the cache
}
any|cache:Error result = cache.get("key1");
if (result is cache:Error) {
// implement what to do, if any error happen when retrieving
// item from the cache
}
@ldclakmal
ldclakmal / cache-sample-1.bal
Created June 6, 2020 15:04
Cache Usage without Error Handling
_ = check cache.put("key1", "value1");
string value = <string> check cache.get("key1");
_ = check cache.invalidate("key1");
_ = check cache.invalidateAll();
boolean hasKey = cache.hasKey("key1");
string[] keys = cache.keys();
int size = cache.size();
int capacity = cache.capacity();