Ads 468x60px

Wednesday, 1 July 2015

Application programming interface

Application programming interface


"API" redirects here. For other uses, see API (disambiguation).
For the MediaWiki (the software used by Wikipedia) API, see mw:API
In computer programming, an application programming interface (API) is a set of routines, protocols, and tools for building software applications. An API expresses a software component in terms of its operations, inputs, outputs, and underlying types. An API defines functionalities that are independent of their respective implementations, which allows definitions and implementations to vary without compromising the interface. A good API makes it easier to develop a program by providing all the building blocks. A programmer then puts the blocks together.
In addition to accessing databases or computer hardware, such as hard disk drives or video cards, an API can ease the work of programming GUI components. For example, an API can facilitate integration of new features into existing applications (a so-called "plug-in API"). An API can also assist otherwise distinct applications with sharing data, which can help to integrate and enhance the functionalities of the applications.
APIs often come in the form of a library that includes specifications for routines, data structures, object classes, and variables. In other cases, notably SOAP and REST services, an API is simply a specification of remote calls exposed to the API consumers.
An API specification can take many forms, including an International Standard, such as POSIX, vendor documentation, such as the Microsoft Windows API, or the libraries of a programming language, e.g., the Standard Template Library in C++ or the Java APIs.
An API differs from an application binary interface (ABI) in that an API is source code-based while an ABI is a binary interface. For instance POSIX is an API, while the Linux Standard Base provides an ABI. 

Uses

API in procedural languages

In most procedural languages, an API specifies a set of functions or routines that accomplish a specific task or are allowed to interact with a specific software component. This specification is presented in a human readable format in paper books or in electronic formats like eBooks or as man pages. For example, the math API on Unix systems is a specification on how to use the mathematical functions included in the math library. Among these functions there is a function, named sqrt(), that can be used to compute the square root of a given number.
The Unix command man 3 sqrt presents the signature of the function sqrt in the form:
SYNOPSIS
            #include <math.h>
            double sqrt(double X);
            float  sqrtf(float X);
DESCRIPTION
       sqrt computes the positive square root of the argument. ...
RETURNS
       On success, the square root is returned. If X is real and positive...
This description means that sqrt() function returns the square root of a positive floating point number (single ordouble precision), as another floating point number.
Hence the API in this case can be interpreted as the collection of the include files used by a program, written in the C language, to reference that library function, and its human readable description provided by the man pages.
Similarly, other languages have procedural libraries; for example, Perl has dedicated APIs for the same mathematical task with built-in documentation available, which is accessible using the perldoc utility:
$ perldoc -f sqrt
       sqrt EXPR
       sqrt    #Return the square root of EXPR.  If EXPR is omitted, returns
               #square root of $_.  Only works on non-negative operands, unless
               #you've loaded the standard Math::Complex module.

API in object-oriented languages

In its simplest form, an object API is a description of how objects work in a given object-oriented language – usually it is expressed as a set of classes with an associated list of class methods.
For example, in the Java languag, if the class Scanner is to be used (a class that reads input from the user in text-based programs), it is required to import the java.util.Scanner library, so objects of type Scanner can be used by invoking some of the class' methods:
import java.util.Scanner;

public class Test {
   public static void main(String[] args) {
       System.out.println("Enter your name:");
       Scanner inputScanner = new Scanner(System.in);
       String name = inputScanner.nextLine();
       System.out.println("Your name is " + name + ".");
       inputScanner.close();
  }
}
In the example above, methods nextLine() and close() are part of the API for the Scanner class, and hence are described in the documentation for that API, e.g.:
public String nextLine()
Advances this scanner past the current line and returns the skipped input...
Returns:
the line that was skipped
Throws:
NoSuchElementException - if no line found
IllegalStateException - if this scanner is closed
More generally, in object-oriented languages, an API usually includes a description of a set of class definitions, with a set of behaviors associated with those classes. This abstract concept is associated with the real functionality exposed, or made available, by the classes that are implemented in terms of class methods (or more generally by all its public components hence all public methods, but also possibly including any internal entity made public like: fields, constants, nested objects, enums, etc.).
The API in this case can be conceived of as the totality of all the methods publicly exposed by the classes (usually called the class interface). This means that the API prescribes the methods by which one interacts with/handles the objects derived from the class definitions.
More generally, one can see the API as the collection of all the kinds of objects one can derive from the class definitions, and their associated possible behaviors. Again: the use is mediated by the public methods, but in this interpretation, the methods are seen as a technical detail of how the behavior is implemented.
For instance: a class representing a Stack can simply expose publicly two methods push() (to add a new item to the stack), and pop() (to extract the last item, ideally placed on top of the stack).
In this case the API can be interpreted as the two methods pop() and push(), or, more generally, as the idea that one can use an item of type Stack that implements the behavior of a stack: a pile exposing its top to add/remove elements. The second interpretation appears more appropriate in the spirit of object orientation.
This concept can be carried to the point where a class interface in an API has no methods at all, but only behaviors associated with it. For instance, the Java and Lisp language APIs include the interface named Serializable, which is amarker interface that requires each class implementing it to behave in a serialized fashion. This does not require implementation of a public method, but rather requires any class that implements this interface to be based on a representation that can be saved (serialized) at any time.
Similarly the behavior of an object in a concurrent (multi-threaded) environment is not necessarily determined by specific methods, belonging to the interface implemented, but still belongs to the API for that Class of objects, and should be described in the documentation. 
In this sense, in object-oriented languages, the API defines a set of object behaviors, possibly mediated by a set of class methods.
In such languages, the API is still distributed as a library. For example, the Java language libraries include a set of APIs that are provided in the form of the JDK used by the developers to build new Java programs. The JDK includes the documentation of the API in JavaDoc notation.
The quality of the documentation associated with an API is often a factor determining its success in terms of ease of use.

API libraries and frameworks

An API is usually related to a software library: the API describes and prescribes the expected behavior while the library is anactual implementation of this set of rules. A single API can have multiple implementation (or none, being abstract) in the form of different libraries that share the same programming interface.
An API can also be related to a software framework: a framework can be based on several libraries implementing several APIs, but unlike the normal use of an API, the access to the behavior built into the framework is mediated by extending its content with new classes plugged into the framework itself. Moreover the overall program flow of control can be out of the control of the caller, and in the hands of the framework via inversion of control or a similar mechanism.

API and protocol

An API can also be an implementation of a protocol.
When an API implements a protocol it can be based on proxy methods for remote invocations that underneath rely on the communication protocol. The role of the API can be exactly to hide the detail of the transport protocol. E.g.: RMI is an API that implements the JRMP protocol or the IIOP as RMI-IIOP.
Protocols are usually shared between different technologies (system based on given computer programming languages in a given operating system) and usually allow the different technologies to exchange information, acting as an abstraction/mediation level between the two different environments. Protocol hence can be considered remote APIs, localAPIs instead are usually specific to a given technology: hence an API for a given language cannot be used in other languages, unless the function calls are wrapped with specific adaptation libraries.
To enable the exchange of information among systems that use different technologies, when an API implements a protocol, it can prescribe a language-neutral message format: e.g. SOAP uses XML as a general container for the messages to be exchanged, similarly REST API can use both XML and JSON.

Object exchange API and protocols

An object API can prescribe a specific object exchange format that a program can use locally within an application, while an object exchange protocol can define a way to transfer the same kind of information in a message sent to a remote system.
When a message is exchanged via a protocol between two different platforms using objects on both sides, the object in a programming language can be transformed (marshalled and unmarshalled[7]) in an object in a remote and different language: so, e.g., a program written in Java invokes a service via SOAP or IIOP written in C# both programs use APIs for remote invocation (each locally to the machine where they are working) to (remotely) exchange information that they both convert from/to an object in local memory.
Instead when a similar object is exchanged via an API local to a single machine the object is effectively exchanged (or areference to it) in memory: e.g. via memory allocated by a single process, or among multiple processes using shared memory, an application server, or other sharing technologies like tuple spaces.

Object remoting API and protocols

An object remoting API is based on a remoting protocol, such as CORBA, that allows remote object method invocation. A method call, executed locally on a proxy object, invokes the corresponding method on the remote object, using the remoting protocol, and acquires the result to be used locally as return value.
When remoting is in place, a modification on the proxy object corresponds to a modification on the remote object. When only an object transfer takes place, the modification to the local copy of the object is not reflected on the original object, unless the object is sent back to the sending system.

API sharing and reuse via virtual machine

Some languages like those running in a virtual machine (e.g. .NET CLI compliant languages in the Common Language Runtime (CLR), and JVM compliant languages in the Java Virtual Machine) can share an API. In this case, a virtual machine enables language interoperability, by abstracting a programming language using an intermediate bytecode and itslanguage bindings. In these languages, the compiler performs just-in-time compilation or ahead-of-time compilationtransforming the source code, possibly written in multiple languages, into its language-independent bytecode representation.
For instance, through the bytecode representation, a program written in Groovy or Scala language can use any standard Java class and hence any Java API. This is possible thanks to the fact both Groovy and Scala have an object model that is a superset of that of the Java language; thus, any API exposed via a Java object is accessible via Groovy or Scala by an equivalent object invocation translated in bytecode.
On the other side, Groovy and Scala introduce first-class entities that are not present in Java, like closures. These entities cannot be natively represented in Java language (Java 8 introduced the concept of lambda expression); thus, to enable interoperation, a closure is encapsulated in a standard Java object. In this case the closure invocation is mediated by a method named call(), which is always present in an closure object as seen by Java, and in Java the closure does not represent a first-class entity.

No comments:

Post a Comment

 
Blogger Templates