【问题标题】:How to call a service through a menu-driven application in Ballerina如何通过 Ballerina 中的菜单驱动应用程序调用服务
【发布时间】:2018-07-08 05:06:23
【问题描述】:

我有一个文件main.bal,其中包含打印菜单并处理用户输入。 gmail_service.bal 文件包含一个hello 服务,它可以发送电子邮件。

ma​​in.bal

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服务?

【问题讨论】:

    标签: api wso2 ballerina


    【解决方案1】:

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

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

    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;
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答。但现在我遇到了这些错误:error: contacts_db_service:0.0.0/main.bal:32:44: not enough arguments in call to 'post()' error: contacts_db_service:0.0.0/main.bal:32:44: invalid usage of the checked expression operator: no expression type is equivalent to error type compilation contains errors
    • 抱歉,我发布的代码有错误。对post() 的调用应如下所示:post("/", ())。我也更新了答案
    猜你喜欢
    • 1970-01-01
    • 2014-08-14
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    相关资源
    最近更新 更多