2

I've been reading a number of articles on "what is an API" but they're either too high level or two low level for me... I can't seem to quite grasp the entire concept, or have that sneaking feeling that i'm "missing something big"

I understand an API exposes functions which help to "talk" to a more complex backend... i.e. Google API lets you create Google maps on your website, or OpenGL API helps you draw graphics without having to talk directly to your video card driver.

And I understand (i think) the concept of an "abstract API" which provides a kind of guide for developers to build software e.g. a RobotAPI might have functions for MoveForward(Speed), TurnLeft(Degrees) etc and a community of robot builders might all want to use that RobotAPI.

But what I don't quite get is the implementation... Lets say there's an application called "ComplexApplication" and it's written in C

Someone wants to use ComplexApplication and either doesn't have access or can't be bothered learning the source code... and they want to use Python to build "PythonApplication". Then someone else has the same issue but they want to use Java to build "JavaApplication"

I understand (I think) that an API can work as a "go between", but it would seem to me that there would need to be two API's: one for the Python developer to use, and one for the Java developer to use. It would seem to me that the API would be provided as a Python module, or a Java Module, and then each programmer can use the API by sending/recieving data to the methods/members defined by that API. Both the Python and Java implementations of the API would share the same abstract API definition... same functions etc... but the code would be different: is that correct? Then however each function talks to the ComplexApplication, is not important... i.e. black box, designed by the ComplexApplication folk

I would also assume that in some cases (like Google maps) the API could be flexible.. it seems some API's are just XML or JSON data formats, and it's expected that you can send/receive data using common protocols like HTTP etc?

Am I on the right track here? It seems in implementing an API, you would have to either a) built a separate API for each language with (ideally) exactly the same functionality or b) use a common "go between" protocol which all languages have an existing implementation for

From what I know about OpenGL (not much), it's a C specific API implementation: there are Java implementations but I believe they just "wrap" the same C API rather than being something that actually facilitates calls from Java straight to the video card?

I just get the feeling there's something about how different programming languages can interact that removes the need for language specific APIs?

  • 1
    I tried to answer your question, but I am not exactly sure to understand what exactly are you asking. – Basile Starynkevitch Aug 2 '16 at 7:33
  • I think this question is about APIs that are architected as a service (which are consumed with HTTP requests and the like) vs APIs that are architected as libraries or SDKs. – Tulains Córdova Aug 2 '16 at 17:01
  • 2
    I guess it's more a "dont know what I dont know" type question... Just seemed to me that people talking about specific API's didn't really talk much about how to use them... there's often a lot of "assumed" knowledge in documentation which makes it hard to "join the dots". I loved your answer Basile as it helps join the dots as well as provide terms for research (and confirmed i'm not completely on the wrong track!) – Simon Aug 2 '16 at 23:08
4

The wikipage on Application Programming Interface is explaining quite well what APIs are.

Often APIs are a set of declarations (of functions, types, variables, or "names") provided by some library, with the documented knowledge about how to use them (including some of their invariants and pre- & post- conditions).

In web contexts, the API could be a set of HTTP requests specifications (since an HTTP web request is nearly seen as some remote procedure call), also with the documented knowledge about how to use them.

Notice that some languages (e.g. Ocaml) formalize a notion of module (and module type) by giving some "shape" of the set of declarations visible from them (and related types or typing properties); but you generally still need to document (e.g. in comments at least) how to use these. For some theory about that, read Pierce's Types and Programming Languages. For an interesting overview of several programming languages, read Scott's Programming Languages Pragmatics.

For C and C++, an API also provides (in practice) some set of header files (that you would #include from your code) declaring it. You probably need to understand what the linker does (e.g. by reading Levine's Linkers & Loaders), and what calling conventions & ABIs are. Read also more about OSes, e.g. Operating Systems: Three Easy Pieces.

how different programming languages can interact....

It is the programming language implementations which are interacting (since a programming language is not a software, but a specification written in some report, often in English with additional formalizations). Read about foreign function interface. A practically important point is the interaction between the garbage collector used in some language implementation (e.g. Python, Ocaml, Scheme) and C code.

Someone wants to use ComplexApplication and either doesn't have access or can't be bothered learning the source code... and they want to use Python to build "PythonApplication".

Then that someone needs to get (or write) the glue code between Python & C. Read the chapters on extending & embedding the Python Interpreter & on Python C/API Reference Manual.


A compiler textbook like the purple Dragon Book could also help you understand how several programming languages implementations can interact.

PS. I am not sure that APIs are conceptually important, even if they are tremendously useful. They are more an engineering thing than a scientific one.

  • Thanks Basile, I think that's what I need... not only better descriptions for the areas i'm wondering about (so I can research them more effectively) but also links to get that information... Very much appreciated! – Simon Aug 2 '16 at 23:03
1

If you want to build a multi-language API, the easiest approach is likely to be to initially write it in one language - probably C - and then write a series of thin wrappers for the other languages. The wrappers would expose the same set of functions, just in a different language.

C is the obvious choice for the base API as so many other languages have methods of interfacing to it.

  • Thanks Simon, that seems to be inline with the picture I'm forming: I just need to learn more about interfacing with C – Simon Aug 2 '16 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.