Tuesday, May 26, 2020

F# Records

My previous post F# - Refactoring to Functional - Part 1 garnered some attention for my use of the struct data type.

Tobias Burger pointed me at the correct implementation of F# records for my data passing. He suggested using ordinary records as my data type instead of structs since they are a more idiomatic approach to F#. It's taken me a little while to get back to the code, but I modified my data types to be records. The code is below:

I have to say "Thank you" to Tobias. His criticism helped me better understand F# records, and it made my code shorter and cleaner!

Friday, May 22, 2020

Microsoft's Project Tye

I just read through a blog post about Microsoft's Project Tye. This is an experimental project that helps developers run multiple apps under a single command. It also has some service discovery elements and a deployment capability for Kubernetes.

I'm putting this link here so that I can find it again later. The ability to coordinate the running of multiple projects (either in a microservices world or just with a frontend and backend components) is compelling enough. The service discovery and Kubernetes deployment capabilities make this really worth understanding.

Wednesday, May 13, 2020

F# - Refactoring to a Functional Design - Part 1

Shortly after the coronavirus pandemic started, I started trying to create web-based tools for my wife (who is a preschool teacher) to evaluate the progress her students are making with distance learning. I reached for Azure Functions first since they are quick for implementing a publicly visible API. As I created and worked with this new API, I needed a way to test the end points to make sure they worked the way I needed them to work. I decided to create a tool in F# to do this testing.

(I know tools like Postman exist and are great. As I was working on the application in question, I was working on a brand new laptop, and I didn't want to go nuts installing software. Also, as much as I like Postman, it has a few drawbacks. The most notable two for me are the fact that changes to tests don't appear in a text file until the user exports the tests. This disconnect results in me forgetting to update tests from Postman with my source code. The other drawback is that additional third-party tools are needed to run Postman tests from some popular CI/CD tools. I recently ran into an issue with one of these tools where the wrapper for the CI/CD pipeline was incompatible with an NPM module needed to run Postman tests. This incompatibility forced me to remove the tests from my build pipeline. That shouldn't happen.)

My first pass at the code in F# is like what I have below: This code is alright, but you can see that there is a lot of duplication for each endpoint to call. This code is also more difficult to read and maintain. I would like an easy way to define an API endpoint to exercise, and just have it tested.

The first refactoring I will do is to remove the duplication of the HTTP call. In the code above, the HTTP calls are not terribly onerous. These calls are a single line of code for each endpoint, and that's not bad. What I don't like about this code is that the HTTP dependencies are mixed with the rest of my tests, and I have to include the HTTP method calls with my definition of the end point to test.

The first step for me was to create an abstraction for the endpoint itself. I want to use an F# type for the endpoint, but I don't want this type to be a class or to contain any logic. To achieve this purpose, I used the F# struct type. This element allows me to define value fields in my type, along with a constructor. My Endpoint struct is below: This "type" gives me a Name so that I can identify my test externally, a URL to hit, the HTTP verb to use in the call, and a "body" for sending in data when necessary. (For this post, I won't be using the Body property.)

Now that I have an endpoint abstraction in place, I need a method to make my HTTP call. Since this is a single line of code in my original file, it should be a pretty short and straightforward method. The method looks like:

let GetEndpoint (endPoint: Endpoint) =
    Http.Request(endPoint.Url, httpMethod = endPoint.Verb)
This method takes in an endpoint. I wanted to ensure that this method stuck to my endpoint abstraction, so I specified a type with the ": type" syntax. Since every line of code in F# is an expression, this method returns the result of the Http.Request method, which is an HttpResponse object.

With my endpoint abstraction and my function to make HTTP calls, I can modify my original testing code to look like this:

This code doesn't look a lot better than the original code, but it is a little easier to see the endpoints that are being tested. This code also shows me that I still have a good bit of "boilerplate" stuff with my testing of the response data. The testing code will be the subject of my next post.

Wednesday, May 6, 2020

Quick Intro to Azure Functions

I've been working on a small web application for my wife to help her evaluate her preschool students. I created this application as a single page application (SPA) and backing APIs. For the SPA, I used VueJS. For the backing API, I used Azure Functions.

Azure Functions are Microsoft's serverless computing offering. They allow a developer to create functionality based on several triggers. Triggers can be HTTP, queues, Azure Storage, and so on. One of the most commonly used triggers is HTTP. A function based on an HTTP trigger fires for a web request. I think about this like a Model View Controller (MVC) application with the models and controllers and none of the boilerplate code that goes with a full API application.

An Azure Function application consists of a minimum set of files. Those files are:

host.json
local.settings.json
FunctionApp.csproj
Function.cs
Where "FunctionApp" and "Function" are the names you give your Azure Function.

The host.json file contains runtime information, including the version of Azure Functions being used and things like logging configurations. The local.settings.json file contains application-specific settings including connection strings. The CSPROJ file is a fairly typical C# project file that specifies any package dependencies and things like that. The CS file is the "function" itself. It looks like a typcial static C# class except for an annotation and a specific method signature.

The "function" that serves as a trigger for your function app is annotated with [FunctionName("MyFunction")]. This annotation specifies the publicly visible name that your function will have. The function signature is specific to the type of trigger for your function. Since I used an HTTP trigger, my function signature looked like:

public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
This signature tells the runtime that I am using an HTTP trigger that responds to HTTP GET and POST requests. I don't specify a specific route. The HttpRequest and a default logger are supplied using the dependency injection that is baked into the service.

When you generate your first function, a default "Hello World" app is created. This application illustrates how to work with the HttpRequest object passed into the function. It also shows how to create the IActionResult to pass output from the function.

For my specific Azure Function app, I put just enough code into the function itself to handle the request and return a response. I created service and repository classes for handling the business logic and data movement for the calls. This approach worked very well for me!

This approach to API development allowed me to get something created and deployed very quickly. I was able to utilize some basic tooling from Microsoft to build, test, and deploy my API very quickly.