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. Spring Cloud常用组件介绍

    一.Eureka (Netfix下) 云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. 二.Spring Cloud Config (Spring下) 配置 ...

  2. Oracle 11g 物理Dataguard日常操作维护(二)

    Oracle 11g 物理Dataguard日常操作维护(二) 2017年8月25日 14:34 3.3 3.3.1 查看备库进程状态 SYS(125_7)@fpyj123> select pr ...

  3. dp练习(4)——过河卒

    1010 过河卒 2002年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 如图,A ...

  4. Vue--Vue.nextTick()的使用

    Vue.nextTick()是比较常用到的API Vue官网对它的解释是:在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 首先要明白Vue的响应式 ...

  5. dup的使用(二)

    转自:http://blog.csdn.net/yeyuangen/article/details/6852682 一个进程在此存在期间,会有一些文件被打开,从而会返回一些文件描述符,从shell中运 ...

  6. Python笔记初识

    Python笔记初识

  7. Phython笔记初识

    Phython笔记初识   Python 1898 第一版本 1991 荷兰人 Guido  协议 Gpl                     动态语音类型  

  8. BZOJ1670 [Usaco2006 Oct]Building the Moat护城河的挖掘

    裸的凸包...(和旋转卡壳有什么关系吗...蒟蒻求教T T) 话说忘了怎么写了...(我以前都是先做上凸壳再做下凸壳的说) 于是看了下hzwer的写法,用了向量的点积,方便多了,于是果断学习(Orz) ...

  9. 小程序中的bindtap和catchtap的区别(交流QQ群:604788754)

    bindtap绑定的节点,如果他的父节点也有绑定事件,点击之后就会出现冒泡. catchtap绑定的节点,如果他的父节点也有绑定事件,点击之后不会出现冒泡.

  10. Awk 从入门到放弃(4) — Aws 格式化

    转:http://www.zsythink.net/archives/1421 print & printf的区别:printf不带\r\n 在awk当中,格式替换符的数量必须与传入的参数的数 ...