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 的高级 Crowd 设置

    启用嵌套用户组(Enable Nested Groups) 为嵌套组启用或禁用支持. 在启用嵌套用户组之前,你需要检查你在 Crowd 中定义的目录能够支持嵌套用户组.当嵌套用户组启用成功后,你可以将 ...

  2. Many Easy Problem

    转自hwk0518,不胜感谢,侵删.

  3. php fpm深度解析

    摘自:https://www.cnblogs.com/wanghetao/p/3934350.html 当我们在谈到cgi的时候,我们在讨论什么 最早的Web服务器简单地响应浏览器发来的HTTP请求, ...

  4. nyoj860(01变形)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=860 又见01背包 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 ...

  5. shell里的/dev/null 2>&1详解

    shell中可能经常能看到: >/dev/null 2>&1 命令意思是:标准输出stdout 和标准错误输出stderr 也重定向到空设备文件,即不显示输出信息 分解这个组合:“ ...

  6. quartz---的Cron表达式

    quartz---的Cron表达式 CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔, ...

  7. spark RDD底层原理

    RDD底层实现原理 RDD是一个分布式数据集,顾名思义,其数据应该分部存储于多台机器上.事实上,每个RDD的数据都以Block的形式存储于多台机器上,下图是Spark的RDD存储架构图,其中每个Exe ...

  8. Unicode与UTF-8,UTF-16

    Unicode(UTF-8, UTF-16)令人混淆的概念 为啥需要Unicode 我们知道计算机其实挺笨的,它只认识0101这样的字符串,当然了我们看这样的01串时肯定会比较头晕的,所以很多时候为了 ...

  9. SQL Server 调优系列基础篇 - 并行运算总结(二)

    前言 上一篇文章我们介绍了查看查询计划的并行运行方式. 本篇我们接着分析SQL Server的并行运算. 闲言少叙,直接进入本篇的正题. 技术准备 同前几篇一样,基于SQL Server2008R2版 ...

  10. Response.ContentType都有哪些?

    Response.ContentType 名称 类型ai application/postscriptaif audio/x-aiffaifc audio/x-aiffaiff audio/x-aif ...