Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an endpoint as an parameter to a function in Ballerina

I'm using 'wso2/ftp' package for some file transferring process and have an ftp:Client endpoint as follows in my main .bal file.

endpoint ftp:Client server1 {
    protocol: ftp:FTP,
    host:<ip_address>,
    port:21,
    secureSocket: {
        basicAuth: {
            username: <user_name>,
            password: <password>
        }
    }
};

What are the ways to pass this endpoint to a public function in another .bal file.

Tried to do as,

function functionName(ftp:Client server1){
    functionFromOtherBalFile(server1);
} 

but get an error as

invalid action invocation, expected an endpoint

from the second .bal file which contains the 'functionFromOtherBalFile' implementation.

Implementation of the 'functionFromOtherBalFile':

public function functionFromOtherBalFile(ftp:Client server1){
    var readFile=server1->get("/file.txt");
    match readFile{
        error err=>{
            io:println("An error occured");
            return err;
        }
        io:ByteChannel =>{
            io:println("Success");
            return readFile;
        }
    }
}

Could someone please help me to solve this issue.

like image 941
Nipuna Dilhara Avatar asked Dec 01 '25 01:12

Nipuna Dilhara


1 Answers

Here is how you can pass an endpoint as a parameter to a function.

import ballerina/http;
import ballerina/io;

function main (string... args) {
    endpoint http:Client cheapAir {
        url: "http://localhost:9090/CheapAir"
    };

    invoke(cheapAir);
}

function invoke(http:Client client) {
    endpoint http:Client myEP = client;

    json reqPayload = {firstName:"Sameera", lastName:"Jayasoma"};
    http:Response response = check myEP -> post("/bookFlight", reqPayload);
    json resPayload = check response.getJsonPayload();
    io:println(resPayload);  
}
like image 187
Sameera Jayasoma Avatar answered Dec 03 '25 20:12

Sameera Jayasoma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!