Skip to content

Instantly share code, notes, and snippets.

@hindol
Last active October 7, 2022 19:15
Show Gist options
  • Save hindol/78e9aa0147b6ebffe7b82d2c3fe99fc1 to your computer and use it in GitHub Desktop.
Save hindol/78e9aa0147b6ebffe7b82d2c3fe99fc1 to your computer and use it in GitHub Desktop.
Writing Azure Functions in Clojure
(ns com.github.hindol.clj-fn.core
(:import
(com.microsoft.azure.functions ExecutionContext
HttpRequestMessage
HttpResponseMessage
OutputBinding)
(com.microsoft.azure.functions.annotation FunctionName
HttpTrigger
QueueOutput)))
;; Bug: https://clojure.atlassian.net/browse/CLJ-2269
;; Must fully qualify type hints on definterface.
(definterface Functional
(^com.microsoft.azure.functions.HttpResponseMessage
run
[^com.microsoft.azure.functions.HttpRequestMessage
request
^com.microsoft.azure.functions.OutputBinding
message
^com.microsoft.azure.functions.ExecutionContext
context]))
(deftype Function []
Functional
(^{:tag HttpResponseMessage
FunctionName "HttpExample"}
run
[this
^{:tag HttpRequestMessage
HttpTrigger {:name "req"
:methods [HttpMethod/GET HttpMethod/POST]
:authLevel AuthorizationLevel/ANONYMOUS}}
request
^{:tag OutputBinding
QueueOutput {:name "msg"
:queueName "outqueue"
:connection "AzureWebJobsStorage"}}
message
^ExecutionContext
context]
42))
package com.github.hindol.clj_fn.core;
import clojure.lang.*;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.*;
public final class Function implements Functional, IType
{
public static final Object const__0;
public static IPersistentVector getBasis() {
return Tuple.create();
}
@FunctionName("HttpExample")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.GET, HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) final HttpRequestMessage request, @QueueOutput(name = "msg", queueName = "outqueue", connection = "AzureWebJobsStorage") final OutputBinding message, final ExecutionContext context) {
return (HttpResponseMessage)Function.const__0;
}
static {
const__0 = 42L;
}
}
package com.github.hindol.clj_fn.core;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.OutputBinding;
public interface Functional {
HttpResponseMessage run(HttpRequestMessage paramHttpRequestMessage, OutputBinding paramOutputBinding, ExecutionContext paramExecutionContext);
}
@hindol
Copy link
Author

hindol commented Apr 15, 2020

Trying to re-create the example from: https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function-azure-cli?tabs=bash%2Cbrowser&pivots=programming-language-java

package com.function;

import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;

/**
 * Azure Functions with HTTP Trigger.
 */
public class Function {
    /**
     * This function listens at endpoint "/api/HttpExample". Two ways to invoke it using "curl" command in bash:
     * 1. curl -d "HTTP Body" {your host}/api/HttpExample
     * 2. curl "{your host}/api/HttpExample?name=HTTP%20Query"
     */
    @FunctionName("HttpExample")
    public HttpResponseMessage run(
            @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) 
            HttpRequestMessage<Optional<String>> request, 
            @QueueOutput(name = "msg", queueName = "outqueue", 
            connection = "AzureWebJobsStorage") OutputBinding<String> msg, 
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        // Parse query parameter
        String query = request.getQueryParameters().get("name");
        String name = request.getBody().orElse(query);

        if (name == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
            .body("Please pass a name on the query string or in the request body").build();
        } else {
            // Write the name to the message queue. 
            msg.setValue(name);

            return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
        }
    }
}

@hindol
Copy link
Author

hindol commented Apr 16, 2020

Check hindol/clj-fn@d3baaae for an alternative Maven based approach to calling Clojure code from Azure Functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment