8

Github GitHub - cloudlayerio/cloudlayerio-fsharp: cloudlayerio (https://cloudlay...

 3 years ago
source link: https://github.com/cloudlayerio/cloudlayerio-fsharp
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

CloudLayer for F#

This is the CloudLayer API for easy access to our REST based API services using F#.

To read about how to get started using CloudLayer, see documentation here.

Installation

You can reference it directly from Nuget or Paket.

PS> Install-Package CloudLayer.FSharp

The assembly targets NetStandard 2.0.

Usage

To begin, create an API key from the dashboard.

Basics

All API calls take in a Connection:

let connection = 
	{ Connection.Defaults with ApiKey = "ca-644907a519df4f84b0db24b822b37c5e" }

If you are using this from an Asp.Net Core app, you can specify a IHttpClientFactory to be used (this is usually available through Dependency Injection),

let connection' = { connection with ClientFactory = factory }

IHttpClientFactory avoids socket exhaustion problems and maintains a pool of HttpClient instances for reuse. See this article for more.

API calls have, as the last argument a Connection, and they take the shape:

connection |> CloudLayerApi.apiCall : Async<Result<ReturnValue, FailureReason>>

All API calls return the Result type, and they follow the railway-oriented approach.

Account Status

You can check the status of your account with

let status =
    connection |> CloudLayerApi.accountStatus |> Async.RunSynchronously

The results can be pattern matched.

match status with
| Ok status ->
    $"{status.Remaining} of {status.Limit} remaining. " +
    $"Limit resets at {status.ResetsAt.LocalDateTime}"
| Error err ->
    match err with
    | FailureReason.InvalidApiKey -> "Check your api key"
    | FailureReason.InsufficientCredit -> "Buy more credit pls"
    | FailureReason.SubscriptionInactive -> "Please activate your account"
    | FailureReason.Unauthorized -> "Please check your credentials or proxy"
    | other -> $"There was an error: {other}"
|> printfn "%s"

Creating Images

CloudLayer can create images of public URLs:

let image = 
    connection |> CloudLayerApi.fetchImage (Url "https://google.com")

and raw html:

let image = 
    connection |> CloudLayerApi.fetchImage (Html "<h1>Hello World!</h1>")

and returns either a System.IO.Stream or a FailureReason.

match image with
| Ok (stream, status) ->
    //do something with stream
| Error err -> 
    failwithf "Something went wrong: %A" err

You can save the result to a file with saveToFile, or read directly to memory with toByteArray.

connection 
|> CloudLayerApi.fetchImage (Url "https://google.com") 
|> CloudLayerApi.saveToFile "google.jpg"
|> Async.RunSynchronously

To use more configuration options, use fetchImageWith. Options are specified by the ImageOptions record.

connection 
|> CloudLayerApi.fetchImageWith 
    { ImageOptions.Defaults with 
        Source = Url "https://www.openstreetmap.org#map=13/-6.1918/71.2976" 
        Timeout = TimeSpan.FromSeconds 60. 
        Inline = false }
|> CloudLayerApi.saveToFile "eagle-island.jpg"
|> Async.RunSynchronously

Creating PDFs

Creating PDFs is similar to the API for creating images.

connection |> CloudLayerApi.fetchPdf (Url "https://en.wikipedia.org/wiki/Marine_snow")
connection |> CloudLayerApi.fetchPdf (Html "<h1>Hello from PDF!</h1>")

For more options, use fetchPdfWith. Options are specified by the PdfOptions record.

connection 
|> CloudLayerApi.fetchPdfWith 
    { PdfOptions.Defaults with
        Source = (Url "https://en.wikipedia.org/wiki/Marine_snow") 
        PrintBackground = false
        Format = "A4" }
|> CloudLayerApi.saveToFile "snow.pdf"
|> Async.RunSynchronously

This library is specifically for F#, if you are using C# you should use our C# library. We did this because we wanted to give F# developers first class support instead of wrapping a C# library.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK