19

Postman collection to send coronavirus stats on slack

 4 years ago
source link: https://medium.com/@shreyaskumar/postman-collections-and-monitors-a68758e5b507
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

niMN73Y.png!web

www.postman.com

Though this is not a very optimum time for making a job switch , Last week I joined Postman as Software Engineer .

There is an awesome onboarding process that everyone goes through whenever he/she joins the company, as a part of which you are required to make a collection that aims to solve a basic real-world problem. The fact that this is awesome because I feel as an employee it is necessary to know the capabilities of the product that the company owns.

I have used postman before for testing end-points but there is lot more to explore than just that. In this article I have tried to demonstrate how collections and monitors can be used to solve a basic real-world problem , spoon-feeding is a bad practice when it comes to web-development so little effort would be required from your end :wink:.

Collections, as the name says, are a collection of related endpoints. It is like a folder that can be used to group together similar requests and they can be shared as well. Instead of running individual requests we can run collections as well, which will basically run all the requests in that collection.
Monitors can be setup for tracking the performance of API over regular intervals. Adding a monitor for a collection will run the entire collection at a regular interval provided during the setup.

What I did?

https://quickchart.io/

How to do that?

  • Find a public api that is Free and create a new collection on Postman app.
    Rapid apis has a lot of free apis available. I used https://rapidapi.com/api-sports/api/covid-193 for getting the statistics of Covid-19 cases of a particular country. End-points can be tested on the website itself. Generate an api-key by logging in.
    Go to collections tab on postman app and create a new collection. Add a request to get data for a particular country ( Request endpoint ) and save the response received in environment variables , don’t forget to add request headers and api-key.
  • Generate chart using chart.io API

    Add another request to collection using one of the apis provided by quickchart.io .This will return the generated graph in image format.

zEVRZf2.png!web

In the pre-request script tab fetch the data received in 1st request from the environment variable and format it in a way required to generate a chart. I have used bar chart format. We need the encoded chart data in our next request so save that in one of the environment variable. Use the same variable in the request URL and everything goes well you will be able to see the chart image in response.

  • Setup Slack for sending message using Webhooks

A Slack app will be required to send message , create one if needed. Webhooks are one of the easiest ways of posting a message on slack.

Reference the above link to create a slack app in your slack workspace and enable Incoming Webhook URL, which will be used to post message on slack you. It will requires a channel to post to as an app in that you can select your own id so that message will be sent to yourself.

Add one more request to the collection with webhooks url obtained from slack. We need to format the data again in a format supported by slack.

Add the following code in pre-request tab of this new request. Make sure that the required environment variables are present, in this case its encoded_chart and CHART_URL .

const encodedChart = pm.environment.get("encoded_chart");const chart_url = pm.environment.get("CHART_URL");// chart payload for slack
const chart_payload = [{
 type: "image",
 title: {
  type: "plain_text",
  text: "bar chart"
 },
 block_id: "quickchart-image",
 image_url: `${chart_url}?bkg=white&c=${encodedChart}`,
 alt_text: "Chart showing latest data"
}];
pm.environment.set("chart_payload", JSON.stringify(chart_payload));

U3iiyyY.png!web

Add the chart payload saved during pre-request script chart_payload to the request body as shown in image. Go ahead and send the request and I hope you get a slack notification.

  • Adding a Monitor to the collection

    This is easy just click on 3-dots on collections tab in Postman app and find Monitor collection option. Steps are self explanatory and options are there for monitor to run at different intervals(Weekly, hourly or minutes).

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK