Invoking HTTP REST endpoints with one line of C# code

I have just created a small addition to Magic, which is the ability to invoke HTTP REST endpoints, with a single line of C# code. The thing relies upon intelligent use of generics, automatically transforming your DTO objects to JSON as you invoke REST endpoints. In addition, the class also intelligently looks to see if what you supply it as a generic argument is a Stream, and allows you to also supply a delegate taking a Stream. This allows you to download and upload huge files, without exhausting your client’s memory resources. This reduces your HTTP REST invocations to a single line of C# code.

To understand how to use it, take a look at my unit tests for the class. Notice, normally you’d simply inject a constructor argument of type IHttpClient to your classes, and retrieve an instance to the HttpClient through dependency injection – And not directly instantiate it as I do in my unit tests. To see the class, and how it is implemented, you can check out its GitHub repository. The class is “super DRY”, implying it doesn’t contain any repeating code, allowing you to easily change it according to your needs. The class also allows you to return anything that’s implementing IConvertible, allowing you to use it in combination with endpoints returning integer values, string, etc – Which is useful for endpoint returning simple atomic values, and not complex JSON types.

Internally the class consumes HttpClient, and hence is also extremely “async in nature”, allowing for invoking HTTP REST API endpoints in an extremely optimised way, utilising your client’s resources in the best possible way.

Advertisements

Resistance marks the target

If you have worked with some extreme form of innovation, such as for instance Nikola Tesla, avoiding noticing the amount of resistance you are given becomes impossible. This is because all forms of extreme innovation carries with them the promise of disruption and change. Changing the status quo of course, implies that those having a vested interest in the current state of affairs, will lose something due to the change your innovation proposes. Nikola Tesla himself being the primary example of this, where Thomas Edison even murdered an elephant in public, in an attempt at trying to scare followers of Tesla away from the ideas of alternating current as a solution to distribute electricity. Another and more recent example is how Tesla, the cars, and its entrepreneur, seems to be demonised in public on a daily basis today. It doesn’t require a rocket scientist (pun!) to understand where this resistance is coming from, who is paying for it, and whom are taking the money to paint things black. The paradox is that once you move beyond this resistance, you realise it can be beneficial to you, since it helps you identify your target – As in; No resistance, no innovation!

Hence, resistance towards your ideas, becomes to the entrepreneur a shiny beacon of light, helping him or her to navigate the ideas of his mind, such that he can easily discard everything not generating resistance. In fact, we can even define the amount of innovation an idea have, using the equations of electricity, by measuring Ohm (electrical resistance) towards our ideas – And the higher the Ohm (resistance), the better the idea usually is.

This is because nobody are afraid of things not containing an element of disruptance. While the existing powers always fears that which promises true change. And the existing power structures in whatever area you are innovating in, often have humongously large research departments, capable of easily measuring an idea towards status quo, and quantify its potential for change. And once they have identified some idea, that is beyond some threshold in regards to change, they will protect themselves, in order to keep the status quo, paying any price necessary to make sure things don’t change – At which point they will facilitate for you as the entrepreneur experiencing resistance, in an attempt to prevent you from changing the status quo. Hence, by intelligently observing and analysing the resistance your ideas as an entrepreneur generates, you can literally hire billion dollar research departments, to investigate if your idea is good or bad, and you can make them do your work for free!

The Black Swan and its Blue Ocean

Generating resistance every time you open your mouth becomes exhausting after a while though. So after a while, the (wise) entrepreneur starts looking for ways to innovate, without generating resistance. Instead of competing in a “red ocean”, he or she realise they can create their own “blue ocean”, where there are no sharks, and they can be the black swan – Whom the white swans cannot see, due to a lack of ability to recognise the black swan as one of their own. However, before you can reach that point, you’ll have to experience resistance, to make sure you have correctly identified your target, such that you know where to navigate, and what to create, and which ideas to simply discard.

The same way existing power structures uses “the occult” (that which is hidden) to generate resistance, the wise entrepreneur uses “that which is hidden” to avoid resistance, while still being capable of generating music in the ears of those having ears, and rainbows in the eyes of those having eyes …

However, this time he is invisible …

A scientific definition of the word ‘Magic’

The word “Magic” tends to scare scientists and left hemisphere humans. It is a word loaded with prejudice and assumptions. However, if you neutrally observe it, it is nothing but a word. We have millions of similar words in our languages, all attempting to help us understand the world around us, and give us the ability to communicate with each other, and such understand each others intentions. In order to understand this particular word though, a little bit of context is required. So let us look at the word “wizard” first.

A “wizard” is historically thought of as a person whom is able to “cast spells”, that creates “change” to his surroundings. To understand “magic”, we must first understand what a “wizard” is. And to understand what a “wizard” is, we must understand what it implies to “cast a spell”. Don’t worry though, it’ll get clear as we proceed, and as we understand what it implies to “cast a spell”, all other parts of our equation will naturally fall in place.

The spell caster

To cast a spell, is actually easily understood, even to a 5 year old child, at least from its conceptual point of view. However, it has been consciously turned into an occult, vicious and dangerous thing, by history, due to the power it yields to those able to successfully perform this art.

In order to understand what spell casting implies, I have a confession to come with. You see, I am casting a spell at this very moment. I am spelling words and sentences, and I am casting them out into the world, for others to read. That is literally what the word “spell casting” implies. Of course, 500 years ago, and some would argue still today, simply being literate was dangerous to the existing power structures, whom wanted to have a monopoly on “spell casting” – Which is why we still refer to the Bible as the word” today, and all other sources of words are still today in some circles considered “dangerous knowledge”. The power structures 500 years ago, such as the Vatican and our Kings, wanted to keep a monopoly on “spell casting”, because by allowing everyone to cast spells, their existing powers would vanish, due to people being able to freely share ideas, and such free themselves from vicious powers, seeking to subdue them and enslave them. Hence the Bible teaches us the following …

In the beginning there was the word, the word was with God, the word was God

And history teaches us that the only holy source of words, can be found in the Bible. This is of course by design, to scare the masses away from people with ideas, making sure enlightened human beings are able to enlighten as few other humans as possible.

Old powers simply wanted to enslave “God”, subdue God (the “word”), and prevent everybody else from taking part in creation, through sharing new ideas, such as democracy, etc, which obviously was a source of fear for Kings, Priests, and other men with power. The literate was dangerous, unless he was thoroughly in alignment with the existing powers of course. Which is why literacy was a task solely given to monks and our priesthood. In fact, it took enormous strength to shed the most horrific parts of these constructs, and put these ideas on the scrapyard of history. However, as we finally managed to put this behind us, we saw beautiful positive consequences, such as the Enlightenment, medicine, science, etc. So in the end, we all benefited, even the Kings, Emperors and priesthood arguably too.

So a “wizard” is basically nothing but a person who chooses his words carefully, throws his words into the world, in order to apply change to other people’s perception, to achieve some sort of greater goal. The words of a wizard becomes his “proxy” or “Djinn” if you like. There are good wizards, and there are bad wizards. An example of the former would be William Shakespeare. An example of the latter would be Joseph Goebbels. They were both “wizards”, carefully choosing their words, in order to change the hearts and minds of those willing to listen.

Defining Magic

With the above definition of “wizardry” and “spell casting”, we now have the foundation required to understand “magic”. If spell casting is the ability to change the world, using the word, and never applying the change directly yourself – Then Magic is the ability to change the world, using some other type of “proxy”. In fact, “magic” is the “base class” or “generalised principle” for “spell casting”, if you wish to speak about it scientifically. It’s basically the same thing, except “spell casting” is a specialisation of “magic”. Spell casting changes our physical world, using “the word” as a proxy. Magic also changes our physical world, still without directly interfering with it, but rather by using another type of “proxy”. Magic hence, is the ability to create a “proxy” that acts on your behalf, in order to apply change. Hence, arguably, all software developers are also magicians, since their software applies change unto the world, without the developer necessary needing to be directly involved in the process. In such a way, software becomes the developer’s “Djinn”.

My reasons for talking about this subject at this particular time, is because I’ve got a new MSDN article coming up, and I anticipate it’ll create resistance, due to that its header contains the word “magic”. Hence, I wanted to be a “good wizard”, and explain my intentions, creating a common vocabulary first, such that I could simply point to this article, and such easily explain my intentions.

You see, according to the above definition of the word “magic”, magic, which is the subject I am writing about in the upcoming MSDN article, truly is magical – Because it gives you a “proxy”, that you can use to your advantage, to create code. Hence, it becomes the art of coding, without coding. Or, it describes a recipe for creating a “Djinn”, that allows you to apply change, create code without effort, or interacting with the code yourself. It hence becomes a “proxy” that creates your code. Hence, it’s “Magic”.

Nothing magical about that … 😉

Sim Sala Bim, Abra Kadabra, cheerio peeples, and have a nice day 🙂