抄一遍果然比读一遍收获大。
In computer programming, an Application Programming Interface (API) is a set of subroutine definition, protocols, and tools for building application software. In general terms, it is a set of clearly defined methods of communication between various software components.
A good API makes it easier to develop a computer program by providing all the building blocks, which are then put together by the programmer.
An API may be for a web-based system, operating system, database system, computer hardware or software library.
An API specification can take many forms, but often includes specificaton for routines, datastructure, object classes, variables or remote calls.
POSIX. Microsoft Windows API, the C++ Standard Template Library and Java APIs are examples of different forms of APIs. Documentation for the API is usually provided to facilitate usage.
Purpose
Just as a graphical user interface (GUI) makes it easier for people to use programs, appllication programming interfaces make it easier for developers to use certain technologies in building applications.
By abstracting the underlying implementation and only exposing objects or actions the developer needs, an API simplifies programming.
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 occuring behind the scenes.
Uses
Libraries and frameworks
An API is usually related to a software library. The API describes and prescribes the expected behavior (a specification) while the library is an actual implementation of this set of rules.
A single API can have multiple implementations (or none, being abstract) in the form of defferent libraries that share the same programming interface.
The separation of the API from its implementation can allow programs written in one language to use a library written in another. For example, because Scala and Java compile to compatible bytecode, Scala developers can take advantage of any Java API.
API use can very depending on the type of programming language involved. An API for a procedural language such as Lua could primarily consist of basic routines to execute code, manipulate data or handle errors, while an API for an object-oriented language such as Java would provide a specification of classes and their class methods.
Language bindings are also APIs. By mapping the features and capabilities of one language to an interface implemented in another language, a language binding allows a library or service written in one language to be used when developing in another language. Tools such as SWIG and F2PY, a Foetran-to-Python interface generator, facilitate the creation of such interfaces.
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 hand of the framework via inversion of control or a similar mechanism.
Operating systems
An API can specify the interface between an application and the operating system. POSIX, for example, specifies a set of common APIs that aim to enable an application written for a POSIX conformant operating system to be compiled for another POSIX conformant operating system. Linux and Berkeley Software Distribution are examples of operating systems that implement the POSIX APIs.
Microsoft has shown a strong commitment to a backward-compatible API, particularly within their Windows API (Win32) library, so older applications may run on newer versions of Windows using an executable-specific setting called “Compatibility Mode”.
An API differ from an application binary interface (ABI) in that an ,API is source code based while an ABI is binary based. For instance, POSIX provides APIs, while the Linux Standard Base provides an ABI.
Remote APIs
Remote APIs allow developers to manipulate remote resources through protocols, specific standards for communication that allow different technologies to work together, regardless of language or platform. For example, the Java Database Connectivity API allows developers to query many different types of databases with the same set of functions, while the Java remote method invocation API uses the Java Remote Method Protocol to allow invocation of functions that operate remotely, but appear local to the developer. Therefore, remote APIs are useful in maintaining the object abstraction in object-oriented programming; a method call, executed locally on proxy object, invokes the corresponding method on the remote object, using the remote protocol, and acquires the result to be used locally as return value. A modification on the proxy object will also result in a corresponding modification on the remote object.
Web APIs
Web APIs are the defined interfaces through which interactions happen between an enterprise and applications that use its assets. An API approach is an architetural approach that revolves around providing programmable interfaces to a set of services to different applications serving different types of consumers.
When used in the context of web development, an API is typically defined as a set of Hypertext Transfer Protocol (HTTP) request message, along with a definition of the structure of response messages, which is usually in an Extensible Markup Language (XML) or JavaScript Object Notation (JSON) format.
An example might be a shipping company API that can be added to an eCommerce-focused web site, to facilitate ordering shipping services and automatically include current shipping rates, without the site developer having to enter the shipper’s rate table into a web database. While “web API” historically has been virtually synonymous for web service, the recent trend (so called Web 2.0) has been moving away from Simple Object Access Protocol (SOAP) based web services and service-oriented architecture (SOA) towards more direct representational state transfer (REST) style web resources and resource-oriented architecture (ROA). Part of this trend is related to the Semantic Web movement toward Resource Description Framework (PDF), a concept to promote web-based ontology engineering technologies. Web APIs allow the conbination of multiple APIs into new applications known as mashups. In the social media space, web APIs have allowed web comminities to facilitate sharing content and data between communities and applications. In this way, content that is created in one place can be dynamically posted and updated in multiple locations on the web.
Design
The design of an API has significant impact on its usability.
The principle of information hiding describes the role of programming interfaces as enabling modular programming by hiding the implementation details of the modules so that users of modules need not understand the complexities inside the modules.
Thus, the design of an API attempts to provide only the tools a user would expect. The design of programming interfaces represents an important part of software architecture, the organization of a complex piece of software. Several authors have created recommendations for how to design APIs, such as Joshua Bloch, Kin Lane, and Michi Henning.
Release policies
APIs are one of the most common ways technology companies integrate with each other. Those that provide and use APIs are considered as being members of a business ecosystem.
The main pollicies for releasing an API are:
- Private: The API is for internal company use only.
- Partner: Only specific business partners can use the API.
For example, car service companies such as Uber and Lyft allow approved third party developers to directly order rides from within their apps. This allows the companies to exercise quality control by curating which apps have access to the API, and provides them with an additional revenue stream.
- Public: The API is available for use by the public.
For example, Microsoft makes the Microsoft Windows API public, and Apple releases it APIs Carbon and Cocoa, so that software can be written for their platforms.
Public API implications
An important factor when an API becomes public is its interface stability. Changes by a developer to a part of it — for example adding new parameters to a function call — could break compatibility with clients that depend on that API.
When parts of a publicly presented API are subject to change and thus not stable, such parts of a particular API should be explicitly documented as unstable. For example, in the Google Guava library the parts that are considered unstable, and that might change in the near future, are marked with the Java annotation @Beta.
A public API can sometimes declare parts of itself as deprecated. This usually means that such part of an API should be considered candidates for being removed, or modified in a backward incompatible way. Therefore, deprecation allows developer to transition away from parts of the API that will be removed or unsupported in the future.
Documentation
API documentation describes what services an API offers and how to use those services, aiming to cover everything a client would need to know to use the API. Documentation is crucial for the development and maintenance of applications that use the API.
API documentation is traditionally found in documentation files, but can also be found in social media such as blogs, forums, and Q&A websites. Traditional documentation files are often presented via a documentation system, such as Javadoc or Pydoc, that has a consistent appearance and structure. However, the types of content included in the documentation differs from API to API.
To facilitate understanding, API documentation can include description of classes and methods in the API as well as “typical usage scenarios, code snippets, design rationales, performance discussions, and contracts”, but implementation details of the API services themselves are usually omitted.
Restrictions and limitations on how the API can be used are also covered by the documentation. For example, documentation for an API function could note that its parameters cannot be null, or that the function could note that its parameters cannot be null, or that the function itself is not thread safe. Because API documentation is so comprehensive, it can be difficult for the writers to keep the documentation updated and for the users to read it carefully, potentially resulting in bugs.
API documentation can be enriched with metadata information like Java annotations. This metadata can be used by the compiler, tools and by the run-time environment to implement custom behaviors or custom handling.