首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Ballerina中通过菜单驱动的应用程序调用服务

如何在Ballerina中通过菜单驱动的应用程序调用服务
EN

Stack Overflow用户
提问于 2018-07-08 13:06:23
回答 1查看 175关注 0票数 0

我有一个main.bal文件,其中包含打印菜单和处理用户输入的内容。gmail_service.bal文件包含一个能够发送电子邮件的hello服务。

main.bal

代码语言:javascript
复制
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

代码语言:javascript
复制
// 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服务?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-08 14:49:07

在Ballerina中,我们通过端点与网络可访问的点进行交互。例如,在您的Gmail服务源中,您已经使用了两个端点:一个侦听器端点和一个客户端端点。侦听器端点用于将hello服务绑定到端口,客户端端点用于调用第三方API ( Gmail API)。

类似地,要从main()函数调用hello服务,您需要为该服务创建一个HTTP client端点。您将通过此端点与您的服务进行交互。修改后的main.bal源代码如下所示。请注意,还没有为POST请求设置有效负载,因为hello服务中的任何地方都没有使用请求正文。

代码语言:javascript
复制
import ballerina/http;
import ballerina/io;

endpoint http:Client emailClient {
    url: "http://localhost:9090"
};

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) {
            http:Response response = check emailClient->post("/", ());
            // Do whatever you want with the response
        }

        if (c == 2) {
            break;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51229003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档