Ain’t Nobody Got Time for That…

ANGTFT

Sometimes you do certain tasks that become normal and you kind of go into autopilot, blindly repeating the same thing over and over again. It normally takes someone else or something else to spark something in your mind and give you an idea that there is always a simpler solution.

My spark came in the form of Alan Richardson, I’ve been a huge fan of his work for a while and I love the content that he creates and the weekly YouTube videos! A couple of weeks ago he released a short video about creating a bookmarklet to change some static text URL links on a web page, into actual clickable links, that open up a specific web page, a Twitter profile in this case. I thought that was really cool and looked like something that I could have a go at myself. All of the JavaScript code for doing this was written within the Chrome DevTools console – Perfect, I’m familiar with these already!

Recently, I’ve been using a strategy when testing, that involves creating “Test Queues” within RabbitMQ (The message broker that we use for our microservices) that siphon all the messages from the queues that our microservices are consuming – The Test Queues hold/store them, so that I can investigate deeper into the message data. Unlike the actual queues, these do not have consumers so the messages will stay there until I choose to delete them, adding an extra layer of control. I could just manually stop the service and grab the messages from the service queues but I like to give myself a separate option.

Screenshot from 2017-06-03 11-13-34
RabbitMQ – Publisher > Exchange > Queues > Consumers…

For context – My Test Queues would live alongside the red ones in the image above but will not have consumers, the arrows to the right of the red queues…hopefully, that makes sense. 🙂

My boring repetitive problem…

RabbitMQ is an awesome message broker and like it says on the site “Messaging that just works” what they haven’t concentrated on is the usability of their UI management console, why should they, that’s not their main focus – they currently just have something that’s good for now. As much as having these queues is really useful, clearing them out or purging them is a tedious task, it’s made worse by the fact that I follow the same process several times a day…

Click on the queue name > scroll to the bottom of the page > Click on the “Purge” button > scroll back to the top of the page > Select the Queue tab to get back to the main view…repeat…yawn!

I’m a fan of getting a local instance of the technologies that we use within our feature team and exploring lots of different aspects so I feel more comfortable and more informed about the tools I’m working within. I knew the RabbitMQ has a HTTP Restful API with a limited amount of features, I can use some of these to my advantage!!

RabbitMQ has been around for about 10 years now so I had a hunch that someone has probably had the same problem as me and wanted to use the API to purge the messages within a queue…I headed over to Google…

Basically, the first result that came back was about 90% of the solution that I required…Bingo! I found this from 5 years ago – Like I said, it wasn’t exactly what I wanted so I needed to adapt the code to suit my requirement.

I grabbed the code and pasted it into a new Snippet within the “Sources” tab on the Dev Tools, I love this feature, it’s like a little playground for practising and running basic JavaScript. The code in the gist deletes the messages from every single queue which was the main part that I needed to change, I tend to apply a simple naming convention to my queues and prefix them all with “TestQueue_” – This is just so that they are all grouped together within the management console.

TestQueues_RabbitMQ
My Test Queues with lots of lovely messages…

I only needed to refactor the code slightly and add an IF statement that checks to see if the queue name starts with the “TestQueue_” name if so, purge the queue. All the other queues are unaffected. Job Done!

RabbitManagementConsole
Slightly refactored code with an IF Statement…

During the testing of the code, I added some logging just to sanity check that I was getting the correct queues that I wanted and discarding all the other ones. All the “null” entries below are queues that do not start with “TestQueue_” so are ignored.

Getting_Queue_Names
Iterating over all the queues to get the ones I want…

Once the Snippet was run, I could see that the correct requests are being made…Yay! The messages were deleted from only the queues I wanted.

Console_Output
Console Output showing the DELETE requests…

I have created a gist of the code that I used to Purge the Queues – https://gist.github.com/DannyDainton/2bae06e3ca898440cdc0452a727ee7bf

I could have just settled for that solution and ran this script from the Dev Tools each time but I wanted it to be even easier for me (I’m very lazy) so that’s where Alan’s bookmarklet app comes in handy, you can take any piece of JavaScript code that you have coded to do something on a page and it will create an encoded link that you can add to your browser bookmarks.

Bookmarklet App
Creating a new bookmarklet with my JavaScript code…

So I now have a link on my browser that saves me the pain of going through the tedious repetitive task to purge each queue. It’s the simple things in life that make me happy.

Alan wrote a post to accompany his video that references lots of links about how others have used bookmarklets. One of those is this excellent blog post by Abby Bangser, which I read a while ago before I saw the YouTube video. She explains how she utilizes the bookmarklets to quickly fill in form data – It’s really interesting and I would recommend trying to give it a go yourself.

Hopefully, that has been interesting enough to spark something in your own mind and attempt to give this a go – I would love to hear from anyone who has created a bookmarklet to solve a problem.

Cheers!

30 Second APIs…

In the UK we have a chef called Jamie Oliver and over the years he has created lots of TV episodes and also a book on how to make a complete family meal in 30 Minutes – The premise of this, is that time doesn’t have to be an excuse not to make healthy food for the family.

30_Min_Meals

Now, if you’ve ever seen these episodes or tried to follow this, it takes a lot longer than 30 minutes!! It’s more about a “mindset”, which is a lie, it’s about having everything prepped and ready to go so all you’re doing is cooking the food and not chopping things up and getting the pots and pans ready.

So what does this have to do with APIs?!

In my current context, making requests to a RESTful API and analysing the response data is something I’m perfectly comfortable with doing but that’s only because I’ve been working within that space for a couple of years now. I truly take for granted many of the skills that I’ve learnt over the last couple of years and I forget, that you need to start somewhere, in order to build up your knowledge of a certain area.

There are loads of public APIs out there that you can use but these can be tricky to set up. In my experience, most of the documentation that comes with these is confusing and sometimes it’s lacking vital information. Another way forward is to create your own basic API but this can be a daunting task coding it yourself, although a very rewarding once you get it working!

What if there was an even easier way that would allow you to have control over the requests that you make and the data that you want to see in the response?!

I’ve found this node module called json-server that is very handy and super quick to get you started. It’s only a couple of commands from a terminal and you are up and running and making requests within seconds….seriously!

The 30 second API mindset…

So following Jamie’s lead, before starting we need to do our Mise en place…We require a couple of things in place before we get cooking.

Prep Work:

  • Download and Install Node.js
  • Download and Install Postman
  • Create a new Folder on your machine
  • Open a Terminal in the new directory

Your starting point should look something like this – depending on your terminal/Postman colour preferences…
Starting_Point

First of all, we need to globally install the json-server module. In the open terminal, type the following command:

npm install -g json-server

Once this has been installed you’re basically ready to make requests. Quick right?! As you have installed the module globally (using the -g flag), the server can be started from any directory but we will be using our newly created one.

In the terminal, type:

 json-server db.json

json_server_db_file
As you do not have a db.json file in that directory, it will conveniently create one for you and populate this with some default values. These are good for now, just to get you started and making basic requests. Later in the post, we will talk through how to create data files containing the data that is more relevant to your context.

We’ll be using Postman to request the data from the db.json file but it’s entirely up to you, use something that you feel comfortable using. You can now send a request and get a response containing data from the file. The available routes are displayed in the terminal, you can use either of these routes in your first request.

Copy one of the routes below and paste this straight into Postman, you will get the data returned from that Endpoint.

 

GET_Request

Sending a GET request is easy right, the best thing about the json-server module is that it gives you the ability to also practice sending POSTs, PUTs and DELETEs. I would attempt go through each one of these methods but the documentation on the json-server GitHub page explains these methods better than I ever could. This also covers a number of awesome features that you can make use of when using the application.

I don’t want to use the Default data file…

Fear not, as this is essentially just a JSON file that you’re using, you can have any data you desire. You’ll need to stay within the applications format for the way it parses the routes/endpoints from the .json file but apart from that, it’s up to you!

I’ve used mockaroo to create some random data. You can grab this from here and save the .json file to the same directory. Stop the current server instance (Ctrl+C) and spin up a new one using the new dummyUsers.json file:

json-server dummyUsers.json

Requests can now be made to access the new data – Try a GET request to http://localhost:3000/users or how about selecting just one user http://localhost:7000/users/{id}

Request_Single_User

Handy Extras…

If like me, you have applications running on certain ports on your local machine, you will need to either stop that process or give json-server a different port to use. This can be done from the command line before you start the server:

json-server <file.json> -p <port number>

When making changes to the .json file manually in a text editor you will need to stop and start the server instance that you have running, for the changes to take effect, which obviously sucks because you want to be able to see your changes straight away. Luckily, there’s a command line flag to get over this:

json-server –watch <file.json>

Any manual file changes made will be automatically detected and the server will be restarted for you and you’ll be able to continue making those lovely requests.

To Finish Off…

So that’s it, you now have a platform to practice making requests to an API that you have control over and all without writing any code. I’ve only covered a small part of what json-server offers during this post, I would advise you to have a look through the GitHub page once you’re comfortable using the basic function of the application because it offers so much more.

Good luck and let me know if you find this useful. If you have any questions just drop me a message!!

An Extra Helping…

I’ve added some more information about using a collection within Postman to make different types of requests and interact with the dummyUsers.json data. Hope it helps!!

https://github.com/DannyDainton/API-Postman-Practice