我有一个文件main.bal
,其中包含打印菜单并处理用户输入。gmail_service.bal
文件包含hello
能够发送电子邮件的服务。
主文件
function main(string... args) {
int c = 0;
while ( c != 2) {
// print options menu to choose from
io:println("-------------------------------------------------------------------------");
io:println("1. Email");
io:println("2. Exit");
io:println("-------------------------------------------------------------------------");
// read user's choice
string choice = io:readln("Enter choice 1 - 2: ");
c = check <int>choice;
if (c == 1) {
//code to send email
}
if (c == 2) {
break;
}
}
}
gmail_service.bal
// A system package containing protocol access constructs
// Package objects referenced with 'http:' in code
import ballerina/http;
import ballerina/io;
import wso2/gmail;
import ballerina/config;
endpoint gmail:Client gmailEP {
clientConfig:{
auth:{
accessToken:config:getAsString("accessToken"),
clientId:config:getAsString("clientId"),
clientSecret:config:getAsString("clientSecret"),
refreshToken:config:getAsString("refreshToken")
}
}
};
documentation {
A service endpoint represents a listener.
}
endpoint http:Listener listener {
port:9090
};
documentation {
A service is a network-accessible API
Advertised on '/hello', port comes from listener endpoint
}
@http:ServiceConfig {
basePath: "/"
}
service<http:Service> hello bind listener {
@http:ResourceConfig {
methods: ["POST"],
path: "/"
}
documentation {
A resource is an invokable API method
Accessible at '/hello/sayHello
'caller' is the client invoking this resource
P{{caller}} Server Connector
P{{request}} Request
}
sayHello (endpoint caller, http:Request request) {
gmail:MessageRequest messageRequest;
messageRequest.recipient = "abc@gmail.com";
messageRequest.sender = "efg@gmail.com";
messageRequest.cc = "";
messageRequest.subject = "Email-Subject";
messageRequest.messageBody = "Email Message Body Text";
//Set the content type of the mail as TEXT_PLAIN or TEXT_HTML.
messageRequest.contentType = gmail:TEXT_PLAIN;
//Send the message.
var sendMessageResponse = gmailEP -> sendMessage("efg@gmail.com", messageRequest);
}
}
当用户输入“1”时如何调用gmail服务?