RestAssured#1 - Introduction To API
What is an API? | Examples of API Usage | How does API work? | Types of API | What is webservice and its type |Difference between API and Web service
What is an API?
API stands for an application programming interface. It is a set of functions that allows applications to access data and interact with external software components, and operating systems. An API is a software-to-software interface that defines the contract for applications to talk to each other over a network without user interaction.
As per Wikipedia - An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offers a service to other pieces of software. In contrast to a user interface, which connects a computer to a person, an application programming interface connects computers or pieces of software to each other.
Purpose In general, APIs define the rules that programmers must follow in order to interact with a programming language, a software library, or any other software tool.
In building applications, an API (application programming interface) simplifies programming by abstracting the underlying implementation and only exposing objects or actions the developer needs. While a graphical interface for an email client might provide a user with a button that performs all the steps for fetching and highlighting new emails, an API for file input/output might give the developer a function that copies a file from one location to another without requiring that the developer understand the file system operations occurring behind the scenes.
Examples of API usage
Weather Snippets - One common API usage example we come across on a daily basis is weather data. This is one of the common features of smartphones and can also be found on other platforms like Google's search, and Apple's weather search. All of these applications use API to fetch weather details.
Travel/Hotel Booking - On travel websites we can find multiple flight details based on our search criteria. These travel sites use APIs to find relevant flights from multiple airlines.
As we can see that APIs provides an interface to consume service from other application by abstracting the underlying implementation of that application.
How does API work?
An API is a set of rules that define how computers, applications, or machines can talk to each other. Most web APIs sits between the application and the web server. The user initiates an API call that tells the application to do something, then the application will use an API to ask the webserver to do something. The API is the middleman between the application and the web server, and the API call is the request. And every time you use software to communicate with other software or online web servers, you’re using APIs to request the information you need.
It’s important to note that while web APIs are the most common, APIs aren’t limited to the web. There are APIs for virtually every machine or system that expects to interact with other machines or systems.
Let’s use a metaphor to explain how an API works.
Imagine you’re a customer at a restaurant. The waiter (the API) functions as an intermediary between customers like you (the user) and the kitchen (webserver). You tell the waiter your order (API call), and the waiter requests it from the kitchen. Finally, the waiter will provide you with what you ordered.
The waiter is the intermediary between you and the kitchen. In this metaphor, the waiter is effectively an abstraction of the API. In software engineering, abstraction is a fundamental concept that aims to simplify a complex mechanism by only focusing on the details of higher importance. In this example, as a customer, you don’t need to know how the kitchen or the restaurant operates in order to get what you want: the food. You just need to know how to order it.
An API is also an abstraction of the web server. The application (such as a website or a mobile app) will make an API call for a set of data to display for the end user to consume. The request is made via the API that accesses the web server to retrieve the requested data, which is populated in the user interface.
Types of API
- Public APIs - A public API is open and available for use by any outside developer or business.
- Partner APIs - A partner API, only available to specifically selected and authorized outside developers or API consumers, is a means to facilitate business-to-business activities.
- Internal APIs - An internal (or private) API is intended only for use within the enterprise, to connect systems and data within the business.
- Composite APIs - Composite APIs generally combine two or more APIs to craft a sequence of related or interdependent operations. Composite APIs can be beneficial to address complex or tightly-related API behaviors, and can sometimes improve speed and performance over individual APIs.
What is web service and its type?
A Web service is a software application with a standardized way of providing interoperability between disparate applications. It does so over HTTP using technologies such as XML, SOAP, WSDL, and UDDI. Web services have the advantage of allowing programs developed in different languages to connect with one another by exchanging data over a web service between clients and servers.
A Web Service can be defined in the following ways:
- It is a client-server application or application component for communication.
- The method of communication between two devices over the network.
- It is a software system for interoperable machine-to-machine communication.
- It is a collection of standards or protocols for exchanging information between two devices or applications.
Types of Webservice
SOAP - s an XML-based Web service protocol to exchange data and documents over HTTP or SMTP (Simple Mail Transfer Protocol). SOAP web services typically use HTTP as a transport protocol, although this is not mandatory. SOAP can be over JMS/FTP/SMTP or any layer 7 protocol. The SOAP message structure consists of a SOAP envelope, inside of which are the SOAP headers and the SOAP body. The SOAP body contains the actual information we want to send. It is based on the standard XML format, designed especially to transport and store structured data. SOAP is a mature standard and is heavily used in many systems, but it does not use many of the functionalities built into HTTP.
REST - RESTful APIs do not need XML-based web service protocols like SOAP and WSDL to support their interfaces. REST is another architectural pattern (resource-oriented), an alternative to SOAP. Unlike SOAP, RESTful applications use the HTTP built-in headers (with a variety of media types) to carry meta-information and use the GET, POST, PUT, and DELETE verbs to perform CRUD operations. REST is resource-oriented and uses clean URLs (or RESTful URLs). The body can be JSON or XML, the former being preferred more due to its simple structure. Later in this book, we look into the principles of RESTful APIs.
Difference between API and Web service?
- Web Services provide interaction between two machines over a network while APIs act as an interface between two different applications for interoperability.
- Web Services require a network connection while APIs may or may not require a network for their operability.
- While APIs can use any protocols or design patterns, web services mostly use REST, SOAP, XML-RPC, or UDDI.
- Many public APIs are transparent with open documentation and self-service portals for rapid developer onboarding. However, web services offer specific data or functionality to certain patterns only.
- Moreover, each web service is an API but all APIs are not web services because a web service might not perform all the operations that an API would do.
- APIs feature efficiency, wide reach, personalization, data ownership, time-effectiveness, programming language independence, and easy integration with GUI; Web Services offer loose coupling, synchronous and asynchronous functionality, and document exchange support while supporting remote procedure calls.
- Web Services provide support for HTTP only while APIs support HTTP/s, URL Requests/Response Headers, and so on.