Calling the API to get Weather Information for SenecaBlackboardExtension project

Xiaochen Wang
Dec 10, 2018 · 2 min read

1. What is the API?

API (Application Programming Interface) is the interface/channel between the application and the developer, responsible for the communication of a program and other software. The API is essentially a predefined function designed to provide the application and the ability of the developer to access a set of routines based on a piece of software or hardware. E.g:

A function is an API. The function has already pre-packaged some functions. When someone else wants to implement certain functions, it can be easily implemented by calling this function by passing in some parameters. The caller does not need to know how these functions are implemented internally. Just look at the documentation or comments to know the entry parameters/return values ​​of the function/what to do. For the user, these functions are APIs, and the API is to call these functions according to the process specified by the person who wrote the function.

2. The role of API

For software providers, leaving the API allows other applications to invoke the software, so that the software can maximize its value and form an ecosystem; it can also prevent others from seeing the code and not revealing trade secrets. For application developers, with an using API, you can directly call a number of companies API to get value to be returned and you do not need to program about how to get this value.

3. Calling OpenWeatherMap API

3.1 First you need to Apply for API Key:

Before you uisng OpenWeatherMap API, you usually need to apply for an API Key. The example of the OpenWeatherMap API Key is: https://api.openweathermap.org/data/2.5/weather?q=toronto&units=metric&appid=XXX, XXX is the API Key of the application.

I applied an API KEY for this PR

3.2 using HTTPS to call API

API is usually provided in the form of HTTPS.

function getText(){                                       
var ourRequest = new XMLHttpRequest();                                       ourRequest.open('GET', urlStr);                                       ourRequest.onload = function() {                                                var weatherData = JSON.parse(ourRequest.responseText);                                           console.log(weatherData);                                           renderHTML(weatherData);                                       
};                                       
ourRequest.send();                                   
};

Return an instance of json:

3.3 Try to get the data of properties of weather, modify the code