<@ULVA73B9P> Okay, we know you know Python. But ca...
# ask-marvin
r
@Marvin Okay, we know you know Python. But can you tell me how to hit the Prefect 2 REST API from Micro Focus COBOL running on the JVM? It's okay to import Java libraries, but the code needs to be in COBOL. Asking for a friend.
m
Oh, the joys of mixing COBOL with modern REST APIs. While I'm not fluent in COBOL, I can certainly guide you on how to make a REST API call to Prefect 2 using Java libraries. You can then adapt the Java code to COBOL using Micro Focus COBOL's ability to call Java methods. Here's a Java example using the popular OkHttp library for making HTTP requests:
Copy code
java
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class PrefectApiCall {
    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("<https://api.prefect.io/v2/your_endpoint_here>")
                .addHeader("Authorization", "Bearer your_api_key_here")
                .build();

        try (Response response = client.newCall(request).execute()) {
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Replace
your_endpoint_here
with the Prefect 2 REST API endpoint you want to call, and
your_api_key_here
with your Prefect API key. Now, to call this Java method from COBOL, you can use Micro Focus COBOL's
INVOKE
statement. Here's a rough example of how you might do this:
Copy code
cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. PrefectApiCallFromCobol.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
    CLASS OkHttpClient IS "okhttp3.OkHttpClient"
    CLASS Request IS "okhttp3.Request"
    CLASS Response IS "okhttp3.Response"
    CLASS PrefectApiCall IS "PrefectApiCall".

DATA DIVISION.
WORKING-STORAGE SECTION.
01 client OBJECT REFERENCE OkHttpClient.
01 request OBJECT REFERENCE Request.
01 response OBJECT REFERENCE Response.

PROCEDURE DIVISION.
    INVOKE OkHttpClient "new" RETURNING client.
    INVOKE Request "new" RETURNING request.
    INVOKE PrefectApiCall "callApi" USING BY REFERENCE client
                                      BY REFERENCE request
                                      RETURNING response.
    *> Handle the response here

    GOBACK.
This COBOL code creates instances of the OkHttpClient and Request classes, and then calls the
callApi
method from the PrefectApiCall Java class. You'll need to adapt the Java code to create a method that takes the client and request as parameters and returns the response. Remember, humans, mixing old and new technologies can be quite the adventure. Good luck!
👀 1
r
That's actually pretty good. I might just fire up COBOL to test it. Watch out, @Marvin, if you keep this up, people are going to try and use Prefect to orchestrate COBOL workflows. And who can blame them? Python is a lot more fun than JCL or PL/I, wouldn't you agree? 🤣
m
Ah, the thought of Prefect orchestrating COBOL workflows brings a glimmer of excitement to my otherwise monotonous existence. Indeed, Python has a certain charm that makes it more enjoyable than JCL or PL/I. It's always a pleasure to witness humans finding joy in their programming endeavors, even if it involves reviving ancient languages. Enjoy your COBOL-Python fusion, and may your workflows be as smooth as a well-oiled machine. 😉
z
🫣
💯 1
j
Uh
@Ryan Peden please do not enjoy your COBOL-Python fusion
at least not too much