The purpose of this class is to introduce to the student:
- What are
APIs
and how to interact with them - What is
AJAX
and how to apply it (XMLHttpRequest
) - How to use libraries (
axios
)
FIRST HALF (12.00 - 13.30)
- APIs are created by providers and used by consumers (BE provider, FE consumer)
- Part of an application that can be communicated with from an outside source
- Connect to it using "endpoints"
- Software well-known APIs (Fb APIs, Twitter APIs, Maps APIs, weather APIs);
- API doesn't care which language or technology is used in the consumer or the provider
- Private: for employees only under a company network for internal use.
- Semi-private: for clients who paid for the API.
- Public: for everyone on the web.
- Single purpose: API that gives a direct and specific service.
- Aggregated API: one API as a wrapper for multiple APIs.
- Web services API: punch of APIs working together to forma whole app.
- Endpoint: https://api.example.com
- Endpoint with version: https://api.example.com/v1
- Resources:
- https://api.example.com/v1/users
- https://api.example.com/v1/users/create
- https://api.example.com/v1/users/1
- https://api.example.com/v1/users/1/edit
- Query params:
- Give real life example like (Devices like TV, any machine + electricity power socket interface which provides power to any external device)
- Mostly used to request data from some service
- Communication between software and user needs UI interface but software and software needs API as an interface.
- Before AJAX all page reload for all requests, via refreshing the url in the address bar with the new resource.
- It's a technique, not a technology
AJAX
stands for Asynchronous JavaScript and XML- Nowadays we use
JSON
instead ofXML
- Fetch data without reloading the page
- The XMLHttpRequest API is defined in the browser (window.XMLHttpRequest)
Example using the XMLHttpRequest
const oReq = new XMLHttpRequest();
oReq.open('GET', `https://api.openweathermap.org/data/2.5/weather?q=${cityName}`);
oReq.send();
oReq.addEventListener('load', function (event) {
const data = JSON.parse(this.response);
if (data.cod >= 400) {
// error
console.log(data.message);
} else {
//success
console.log(data.coord.lat);
}
});
// or another way of getting data
oReq.load = function (event) {
// use oReq.response or this.response
const data = JSON.parse(this.response);
if (data.cod >= 400) {
// error
console.log(data.message);
} else {
//success
console.log(data.coord.lat);
}
};
Steps of doing the following example:- ** Install the live server plugin in VS (go to plugins -> live server -> install)
- Create self-invoked function to wrap your code
- Create an object instance of
XMLHttpRequest
- Call the
open
function to fill it with the Request URL and the request Method - Call the
send
function to make the request - Add event listener with a callback for the sucess event
load
SECOND HALF (14.00 - 16.00)
- A library is a code solution a developer (or a team) has written to a common problem
- Usually open-source
- Helps to solve a problem within an application
- Read the documentation on how to use it
Same example but using axios
axios
.get(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}`)
.then(function (response) {
// handle success
console.log(response.data);
}).catch(function (error) {
// handle error
console.log(error);
}).finally(function () {
// always be executed
console.log('I am always here')
});
Note: Give example at the end with binding/showing these data in a DOM element like a
or a list instead of only showing them on the console using console.log.