A thought experiment on writing a strange API for Dart.

Overview

Operator Frog 🐸

What is Operator Frog

Operator Frog is a < 24h thought experiment that I did. I wanted to know if we could write a strange looking API in Dart for HTTP communication without needing any kind of code generation.

How does it work?

Operator Frog overwrites operators to create an API that does not depend on methods. This doesn't make it readable (unless you are me) but it is a nice showcase of how you can overwrite operators in Dart and that certain kind of operators (bit-wise vs comparators) have a certain logic when it comes to their return type.

For instance any bit-wise operator (&, ^, |, >>, <<<) in Dart will allow to directly chain the next bit-wise operator without having to put the statement in parentheses. While the comparator operators (>=, >) will assume that the next operator is called on the left side of the previous operator call.

This mean there is a certain restriction in how you can define chained API logic. For instance with the client from Operator Frog you can do the following:

final client = http > 'http://localhost:8080';

await (client.post / 'todos' &
  header >> {'Content-Type': 'application/json'} &
  json >> {'title': 'A new Todo'});

Notice how we are using >> and & to setup our request object? If instead of >> we used > it would complain that The operator '>' isn't defined for the type 'HttpRequest'. because the > is being called on the result of client.post / 'todos & header and not on header.

Readability

I tried to remain some form of readability on both the client and server side by using specific operators for specific actions. For instance the action to create a request (or define a route on the server) I used the / operator, indicating the start of a path: /some/path.

If something needs to be added to an object, like adding a header or a query to HttpRequest I used the & operator, indicating this with that or this and that.

And then if we wanted to set the final value, like the route executor or the final value of a body I used the >> operator, which should be read as this uses that.

On the server side I decided to use the | to chain routes or middleware. As only route can ever be triggered for a single request it should be read as this or that.

To facilitate these operators the server and client packages both provide certain constant values that have these operators defined:

  • Server
    • middleware > (Handler) => Handler: to define a new middleware handler
    • METHOD / 'path' >> (RequestContext) => Response: where METHOD is one of the provided HTTP method constants, get, post, put, patch, delete, head, options
  • Client
    • http > 'http://host:port': to create a new client
    • client.METHOD / 'path': to create a new http request that is also future-able.
    • request & RequestAttribution: to add an attribution like body, json, query or header.
    • RequestAttribution >> dynamic: to set the value of a specific attribution.

What now?

Nothing, honestly this shouldn't ever be used in any form or sense as it is not readable and there are probably a lot of caveats and issues with this approach that I didn't even think off.

If anything this should serve as a reminder that it is okay to stray of the "correct" path and do some exploration yourself, at best you will find a cool concept and at it's worse you might learn something new.

Credits & Gratitude

I would like to give gratitude for the existence of Dart Frog, which was used to build this API.

And I would like to give a shout-out to Felix Angelov for tolerating (and debugging) this thought experiment.

You might also like...

Flashcard and Kanji writing Flutter App. Stopped working on this Project. Currently just a graveyard.

Flashcard and Kanji writing Flutter App. Stopped working on this Project. Currently just a graveyard.

KanjiMaru Not in development anymore for reasons I shall not state here. Still loved the design I created, so it will stay up. Flashcard and Japanese

Jan 1, 2023

Flutter Plugin for Requesting and Writing Reviews in Google Play and the App Store

Flutter Plugin for Requesting and Writing Reviews in Google Play and the App Store

Flutter Plugin for Requesting and Writing Reviews in Google Play and the App Store. Apps have to be published for the app to be found correctly.

Jan 4, 2023

Glass-Design - Glass Design on one page writing in Flutter

Glass-Design - Glass Design on one page writing in Flutter

glassdesign writing in Flutter framework / GlassDesign fait en Flutter framework

Jun 15, 2022

A collection of bricks to be more productive when writing code.

A collection of bricks to be more productive when writing code.

A collection of bricks that enable developers to be more productive when writing code. Bricks 🧱 Brick Description Version clean_architecture_global_s

Nov 12, 2022

Bhagavad Gita app using flutter & Bhagavad-Gita-API is A lightweight Node.js based Bhagavad Gita API [An open source rest api on indian Vedic Scripture Shrimad Bhagavad Gita].

Bhagavad Gita app using flutter & Bhagavad-Gita-API is A lightweight Node.js based Bhagavad Gita API [An open source rest api on indian Vedic Scripture Shrimad Bhagavad Gita].

Gita Bhagavad Gita flutter app. Download App - Playstore Web Application About Bhagavad Gita app using flutter & Bhagavad-Gita-API is A lightweight No

Apr 5, 2022

Beautiful Weather App using API with support for dark mode. Created by Jakub Sobański ( API ) and Martin Gogołowicz (UI, API help)

Beautiful Weather App using API with support for dark mode. Created by Jakub Sobański ( API ) and Martin Gogołowicz (UI, API help)

Flutter Weather App using API with darkmode support Flutter 2.8.1 Null Safety Beautiful Weather App using https://github.com/MonsieurZbanowanYY/Weathe

Nov 29, 2022

Dart API Client which wraps the QvaPay API

qvapay_api_client Dart API Client which wraps the QvaPay API The client needs Dio to perform the requests, you must inject an instance in the construc

Nov 2, 2022

A most easily usable RESAS API wrapper in Dart. With this library, you can easily integrate your application with the RESAS API.

A most easily usable RESAS API wrapper library in Dart! 1. About 1.1. What Is RESAS? 1.2. Introduction 1.2.1. Install Library 1.2.2. Import It 1.2.3.

Apr 7, 2022

An Imgur API Client Library that uses Imgur's v3 API for Dart

An Imgur API Client Library that uses Imgur's v3 API for Dart

imgur.dart An Imgur API Client Library that uses Imgur's v3 API for Dart. Usage

Dec 2, 2022
Owner
Jochum van der Ploeg
Jochum van der Ploeg
An experiment in building a better XMPP client. using Flutter

moxxy An experimental XMPP client that tries to be as easy, modern and beautiful

null 28 Dec 15, 2022
What it be - A flutter app to experiment with locally executed mobile AI.

what_it_be A flutter app to experiment with locally executed mobile AI. Getting Started This project is a starting point for a Flutter application. A

S. OZDEMIR 1 Jan 4, 2022
Experiment with dark themes based on popular apps.

Fast Dark Theme This project is a Flutter web experiment. It allows you to prototype dark mode colors based on popular apps: WhatsApp, Twitter and Sha

Bernardo Ferrari 47 Dec 10, 2021
An experiment in building a better XMPP client. This time using Flutter.

Moxxy An experimental XMPP client that tries to be as easy, modern and beautiful as possible. The code is also available on codeberg. Screenshots Deve

null 28 Dec 15, 2022
Create dart data classes easily, fast and without writing boilerplate or running code generation.

Dart Data Class Generator Create dart data classes easily, fast and without writing boilerplate or running code generation. Features The generator can

null 186 Feb 28, 2022
Sibyl App written with Dart/Flutter. (My first experience in writing a real android app in flutter).

sibyl_app A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if thi

ALi.w 3 Feb 17, 2022
A zero-dependency web framework for writing web apps in plain Dart.

Rad Rad is a frontend framework for creating fast and interactive web apps using Dart. It's inspired from Flutter and shares same programming paradigm

null 70 Dec 13, 2022
Cross-platform flutter plugin for reading and writing NFC tags. Not maintained anymore - not looking for new maintainer, fork instead.

nfc_in_flutter NFC in Flutter is a plugin for reading and writing NFC tags in Flutter. It works on both Android and iOS with a simple stream interface

Andi Semler 113 Sep 28, 2022
Flutter-writting-prompt - Flutter example app used to show Writing Prompts to the user

Writing Prompt A writing prompt application designed to showcase an approach to a "Clean"er architecture in Flutter with Bloc and RxDart, including Un

Gonçalo Palma 42 Dec 11, 2022
null 1 Jan 8, 2022