ballerina 学习 三十 扩展开发(一)
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 学习 三十 扩展开发(一)的更多相关文章
- ballerina 学习 三十一 扩展开发(二)
上篇说了使用ballerina 语言开发扩展模块,对于注解类型的我们是需要使用java 语言进行 开发的 官方提供了一个hello 的demo可以参考 https://github.com/balle ...
- ballerina 学习 三十二 编写安全的程序
ballerina编译器已经集成了部分安全检测,在编译时可以帮助我们生成错误提示,同时ballerina 标准库 已经对于常见漏洞高发的地方做了很好的处理,当我们编写了有安全隐患的代码,编译器就已 ...
- Java开发学习(三十六)----SpringBoot三种配置文件解析
一. 配置文件格式 我们现在启动服务器默认的端口号是 8080,访问路径可以书写为 http://localhost:8080/books/1 在线上环境我们还是希望将端口号改为 80,这样在访问的时 ...
- Salesforce LWC学习(三十九) lwc下quick action的recordId的问题和解决方案
本篇参考: https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation ...
- ElasticSearch7.3学习(三十二)----logstash三大插件(input、filter、output)及其综合示例
1. Logstash输入插件 1.1 input介绍 logstash支持很多数据源,比如说file,http,jdbc,s3等等 图片上面只是一少部分.详情见网址:https://www.elas ...
- Java开发学习(三十五)----SpringBoot快速入门及起步依赖解析
一.SpringBoot简介 SpringBoot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化 Spring 应用的初始搭建以及开发过程. 使用了 Spring 框架后已经简化了我 ...
- ballerina 学习二十八 快速grpc 服务开发
ballerina 的grpc 开发模型,对于开发者来说简单了好多,不是schema first 的方式,而是我们 只要编写简单的ballerina service 就可以了,proto 文件是自动帮 ...
- Java开发学习(三十)----Maven聚合和继承解析
一.聚合 分模块开发后,需要将这四个项目都安装到本地仓库,目前我们只能通过项目Maven面板的install来安装,并且需要安装四个,如果我们的项目足够多,那么一个个安装起来还是比较麻烦的 如果四个项 ...
- Java开发学习(三十二)----Maven多环境配置切换与跳过测试的三种方式
一.多环境开发 我们平常都是在自己的开发环境进行开发, 当开发完成后,需要把开发的功能部署到测试环境供测试人员进行测试使用, 等测试人员测试通过后,我们会将项目部署到生成环境上线使用. 这个时候就有一 ...
随机推荐
- Struts2 简介图
Struts2官方提供的,strus2的内部工作机制图解.
- android之视频播放系统VideoView和自定义VideoView控件的应用
Android播放视频,包含系统自带VideoView控件,和自定义VideoView控件,可全屏播放,案例包含了本地视频和网络视频. 1:自定义VideoView控件 2:布局代码 3:Activi ...
- Android程序员眼中世界上最遥远的距离
世界上最遥远的距离,是我在if里你在else里,似乎一直相伴又永远分离: 世界上最痴心的等待,是我当case你是switch,或许永远都选不上自己: 世界上最真情的相依,是你在try我在catch. ...
- Confluence 6 从外部目录中同步数据支持的目录类型
针对一些特定的用户目录类型,Confluence 在系统的数据库中保存了目录的缓存信息(用户和用户组),这样能够让系统更快速的访问用户和用户组数据.一个数据同步的进程将会间歇性的在系统中运行来将远程的 ...
- PHP函数总结 (五)
<?php /** * 回调函数: * 指调用函数时并不是传递一个标准的变量作为参数,而是将另一个函数作为参数传递到调用的函数中 * 使用回调函数可以 将一段自己定义的功能传到函数内部使用 * ...
- UVA-10061 How many zero's and how many digits ? (数论)
题目大意:让求n!在base进制下的位数以及末尾0的连续个数. 题目分析:一个m位的b进制数N,最小是b^(m-1),最大不超过b^m,即b^(m-1)≤N<b^m.解不等式,得log10(N) ...
- Web Service基本概念
Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是:通过SOAP ...
- 如何用xmlspy将xml文档生成xsd文件
所以我们有必要知道如何通过xmlspy这个非常方便的工具进行xml的转换工作.点击“File”-“New”系统会弹出“create new document”的窗口,此时选择“xml XML docu ...
- curl常用功能
<?php //创建一个新cURL资源 $ch = curl_init(); //******************************************************** ...
- c中gets函数使用可能导致缓冲区溢出
头文件:#include <stdio.h> gets()函数用于从缓冲区中读取字符串,其原型如下: char *gets(char *string); gets()函数从流中读取字 ...