3

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.

Riyafa Abdul Hameed
  • 7,417
  • 6
  • 40
  • 55
Nipuna Dilhara
  • 424
  • 2
  • 6
  • 18

1 Answers1

4

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);  
}
Sameera Jayasoma
  • 1,545
  • 8
  • 12
  • Hi @Sameera, I'm using a similar code as you have suggested and it works fine within a single .bal file. What I want to do is to pass an endpoint to a public function in a second .bal file and do some related operations there. However, I'm getting 'invalid action invocation, expected an endpoint' errors from the lines I have used the endpoint in second .bal filie. This occurs when I'm trying to build the source code folder using 'ballerina build Code/' command. – Nipuna Dilhara Jul 16 '18 at 02:22
  • 1
    I think I found the cause. It's working when I assigned the endpoint to a local endpoint declaration in every function it is being passed. Can't use straight away from parameters. – Nipuna Dilhara Jul 16 '18 at 03:14