Skip to content

firesharkstudios/butterfly-util

Repository files navigation

Butterfly.Util Butterfly Logo

Collection of utility methods used in the Butterfly Server

Install from Nuget

Name Package Install
Butterfly.Web nuget nuget install Butterfly.Web
Butterfly.Web.EmbedIO nuget nuget install Butterfly.Web.EmbedIO
Butterfly.Web.RedHttpServer nuget nuget install Butterfly.Web.RedHttpServer

Install from Source Code

git clone https://github.com/firesharkstudios/butterfly-twilio

Working with Dictionaries

Since Dictionary<string, object> is used so extensively, you'll likely find it useful to declare an alias with your other using statements...

using Dict = System.Collections.Generic.Dictionary<string, object>;

Butterfly.Core.Util contains a GetAs extension method for Dict that makes it easier to convert values...

Here are a few common scenarios related to database records...

// Retrieve from the todo table using the primary key value
Dict row = await database.SelectRowAsync("todo", "123");

// Retrieve as string
var id = row.GetAs("id", "");

// Retrieve as integer
var count = row.GetAs("count", -1);

// Retrieve as float
var amount = row.GetAs("id", 0.0f);

// Retrieve as DateTime instance (auto converts UNIX timestamp)
var createdAt = row.GetAs("created_at", DateTime.MinValue);

Here are a couple common scenarios related to the Web API...

webApi.OnPost("/api/todo/insert", async (req, res) => {
    var todo = await req.ParseAsJsonAsync<Dict>();

    // Retrieve as array
    var tags = todo.GetAs<string[]>("tags", null);

    // Retrieve as dictionary
    var options = todo.GetAs<Dict>("options", null);
});

Contributing

If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.

Licensing

The code is licensed under the Mozilla Public License 2.0.

About

Collection of utility methods used in the Butterfly Server

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages