ballerina 主要是分为两大类

  • 基于ballerina 语言开发的,一般是客户端的connector
  • 使用java语言开发的(类似的基于jvm的都可以),一般是注解以及进行构件生成

ballerina 语言开发说明

  • 是一个ballerian 的包
  • 创建一个对象包含了init 、getCallerActions 函数
  • 实现init 函数,次函数是在用户实例化一个endpoint的时候
  • 实现getCallerActions 函数,次函数是在connect 必须别返回的时候进行调用
  • 构建模块并发布

一个twilio connector 使用说明

官方提供了单文件以及多文件的代码组织方式,各有优缺点,结合实际使用

  • 使用demo(注意官方文档的版本有问题,最好参考github 学习)
import ballerina/config;
import ballerina/io;
import wso2/twilio; public function main(string... args) {
endpoint twilio:Client twilioClient {
accountSId:config:getAsString("ACCOUNT_SID"),
authToken:config:getAsString("AUTH_TOKEN"),
xAuthyKey:config:getAsString("AUTHY_API_KEY")
}; var details = twilioClient->getAccountDetails();
match details {
twilio:Account account => io:println(account);
twilio:TwilioError twilioError => io:println(twilioError);
}
}

twilio connector 开发细节说明

按照流程开发即可

  • 是一个模块
我们可以使用ballerina init 创建
  • 一个对象包含了init 、getCallerActions 函数

    因为其他地方需要调用,我们的可见性,使用public 修饰

endpoint:
public type TwilioClient object {
// 配置参数
public TwilioConfiguration twilioConfig;
public TwilioConnector twilioConnector = new;
// init 函数
public function init(TwilioConfiguration config);
documentation { Initialize Twilio endpoint
R{{}} The Twilio connector object
}
// getCallerActions 函数
public function getCallerActions() returns TwilioConnector;
};
配置说明:
public type TwilioConfiguration record {
http:ClientEndpointConfig clientConfig;
};
  • connector 对象,实际上就是具体干活的对象
public type TwilioConnector object {
public string accountSId;
public http:Client client;
public function getAccountDetails() returns Account|error;
};
  • 常量以及record 定义
public type Account record {
string sid;
string name;
string status;
string ^"type";
string createdDate;
string updatedDate;
}; // Constants
@final string BASE_URL = "https://api.twilio.com/2010-04-01";
@final string ACCOUNTS_API = "/Accounts/";
@final string RESPONSE_TYPE_JSON = ".json";
@final string EMPTY_STRING = "";
  • 实现endpoint 的init 、getCallerActions 函数
function TwilioClient::getCallerActions() returns TwilioConnector {
return self.twilioConnector;
}
function TwilioClient::init(TwilioConfiguration config) {
config.clientConfig.url = BASE_URL;
string username;
string password;
var usernameOrEmpty = config.clientConfig.auth.username;
match usernameOrEmpty {
string usernameString => username = usernameString;
() => {
error err = { message: "Username cannot be empty" };
throw err;
}
}
var passwordOrEmpty = config.clientConfig.auth.password;
match passwordOrEmpty {
string passwordString => password = passwordString;
() => {
error err = { message: "Password cannot be empty" };
throw err;
}
}
self.twilioConnector.accountSId = username;
self.twilioConnector.client.init(config.clientConfig);
}
  • 实现connector 函数
function TwilioConnector::getAccountDetails() returns Account|error {
endpoint http:Client httpClient = self.client;
string requestPath = ACCOUNTS_API + self.accountSId + RESPONSE_TYPE_JSON;
var response = httpClient->get(requestPath);
json jsonResponse = check parseResponseToJson(response);
return mapJsonToAccount(jsonResponse);
}

参考资料

https://ballerina.io/learn/how-to-extend-ballerina/(文档与项目不一致)
https://github.com/wso2-ballerina/module-twilio

 
 
 
 

ballerina 学习 三十 扩展开发(一)的更多相关文章

  1. ballerina 学习 三十一 扩展开发(二)

    上篇说了使用ballerina 语言开发扩展模块,对于注解类型的我们是需要使用java 语言进行 开发的 官方提供了一个hello 的demo可以参考 https://github.com/balle ...

  2. ballerina 学习 三十二 编写安全的程序

      ballerina编译器已经集成了部分安全检测,在编译时可以帮助我们生成错误提示,同时ballerina 标准库 已经对于常见漏洞高发的地方做了很好的处理,当我们编写了有安全隐患的代码,编译器就已 ...

  3. Java开发学习(三十六)----SpringBoot三种配置文件解析

    一. 配置文件格式 我们现在启动服务器默认的端口号是 8080,访问路径可以书写为 http://localhost:8080/books/1 在线上环境我们还是希望将端口号改为 80,这样在访问的时 ...

  4. Salesforce LWC学习(三十九) lwc下quick action的recordId的问题和解决方案

    本篇参考: https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation ...

  5. ElasticSearch7.3学习(三十二)----logstash三大插件(input、filter、output)及其综合示例

    1. Logstash输入插件 1.1 input介绍 logstash支持很多数据源,比如说file,http,jdbc,s3等等 图片上面只是一少部分.详情见网址:https://www.elas ...

  6. Java开发学习(三十五)----SpringBoot快速入门及起步依赖解析

    一.SpringBoot简介 SpringBoot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化 Spring 应用的初始搭建以及开发过程. 使用了 Spring 框架后已经简化了我 ...

  7. ballerina 学习二十八 快速grpc 服务开发

    ballerina 的grpc 开发模型,对于开发者来说简单了好多,不是schema first 的方式,而是我们 只要编写简单的ballerina service 就可以了,proto 文件是自动帮 ...

  8. Java开发学习(三十)----Maven聚合和继承解析

    一.聚合 分模块开发后,需要将这四个项目都安装到本地仓库,目前我们只能通过项目Maven面板的install来安装,并且需要安装四个,如果我们的项目足够多,那么一个个安装起来还是比较麻烦的 如果四个项 ...

  9. Java开发学习(三十二)----Maven多环境配置切换与跳过测试的三种方式

    一.多环境开发 我们平常都是在自己的开发环境进行开发, 当开发完成后,需要把开发的功能部署到测试环境供测试人员进行测试使用, 等测试人员测试通过后,我们会将项目部署到生成环境上线使用. 这个时候就有一 ...

随机推荐

  1. Confluence 6 为空间赋予公共访问

    希望为一个 Confluence 空间赋予公共访问权限,你必须为匿名用户赋予下面的权限: 在全站启用 可以使用(can use)权限,如上面描述的的. 相关的 空间权限.如果你希望你的一个空间可以公共 ...

  2. 『PyTorch』第五弹_深入理解Tensor对象_中上:索引

    一.普通索引 示例 a = t.Tensor(4,5) print(a) print(a[0:1,:2]) print(a[0,:2]) # 注意和前一种索引出来的值相同,shape不同 print( ...

  3. Java网络编程和NIO详解8:浅析mmap和Direct Buffer

    Java网络编程与NIO详解8:浅析mmap和Direct Buffer 本系列文章首发于我的个人博客:https://h2pl.github.io/ 欢迎阅览我的CSDN专栏:Java网络编程和NI ...

  4. 二分求LIS并打印结果

    1275: God's ladder [DP] 时间限制: 1 Sec 内存限制: 128 MB  Special Judge 题目描述 天明来到神之宫殿,在他眼前出现了若干个石柱,每个石柱上有1枚金 ...

  5. http 请求和格式

    get 请求:从指定的资源请求数据. post请求:向指定的资源提交要被处理的数据. head请求:与 GET 相同,但只返回 HTTP 报头,不返回资源实体. option请求:返回服务器支持的 H ...

  6. POJ服务器不能启动问题

    问题1: 启动tomcat时出现错误:tomcat Address already in use: JVM_Bind:80 按照网上的方法,查找占用80端口的进程:netstat -ano 任务管理器 ...

  7. windows创建窗口、关闭窗口流程

    NC,即 non-client 区域,包括标题栏.窗口边框.最大.最小按钮.滚动条等. 一.在调用Windows的::CreateWindowEx函数创建窗口时,一般会先发出 WM_NCCREATE消 ...

  8. js组件的写法

    工作之中的不足,报了js培训班,因为工作加班原因缺了几天课(js组件开发),现在拾起来补补 <!doctype html> <html> <head> <me ...

  9. .net大型分布式电子商务架构说明

    背景 构建具备高可用,高扩展性,高性能,能承载高并发,大流量的分布式电子商务平台,支持用户,订单,采购,物流,配送,财务等多个项目的协作,便于后续运营报表,分析,便于运维及监控. 架构演变 基础框架剥 ...

  10. Flask初级(十一)flash与APScheduler 实现定时任务

    from flask import Flask from flask_apscheduler import APScheduler # 引入APScheduler class Config(objec ...