3

For instance the google-speech-api package is described as a "Google Speech API wrapper for node."

what exactly is a wrapper in this kind of context?

Louis
  • 146,715
  • 28
  • 274
  • 320
MartianMartian
  • 1,753
  • 1
  • 18
  • 26

1 Answers1

9

In this kind of context, a "wrapper" is a library that provides a convenient interface to a service or library that would otherwise be much more inconvenient to use. The wrapper "wraps" the native API into a more convenient form.

For instance, the Google Speech API is used through HTTP requests. This page gives this example:

curl -X POST \
--data-binary @alsalam-alikum.flac \
--user-agent 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7' \
--header 'Content-Type: audio/x-flac; rate=16000;' \
'https://www.google.com/speech-api/v1/recognize?client=chromium&lang=ar-QA&maxresults=10'

(Note the page is based on v1 of the API but the API is now at v2. However, it does not matter for what I'm saying here.)

You absolutely can do the operation by issuing the equivalent HTTP request using stock Node.js functions (i.e. without a wrapper) but:

  1. You'll have to deal with a slew of details you really should not have to deal with. For instance, the query always start with the string https://www.google.com/speech-api/v1/recognize. If you do it manually, you have to always specify it. There may be headers you have to set on every query. There may be an API key you need to pass with every query. With a wrapper, the things are are not dependent on your specific situation are handled by the wrapper and things that depend on you but are constant (like an API key) once set can be set once and only once.

  2. Your code won't clearly reflect what you are trying to do. Someone just reading it without knowledge of the Google Speech API will see you are doing an HTTP query but won't readily figure out what it is you are actually doing. With a wrapper, you can write something that more closely resemble what you are trying to do (from the google-speech-api page linked above):

    var speech = require('google-speech-api');
    
    var opts = {
      file: 'speech.mp3',
      key: '<Google API Key>'
    };
    
    speech(opts, function (err, results) {
      console.log(results);
      // [{result: [{alternative: [{transcript: '...'}]}]}]
    });
    

    Sure, the person reading this will want to read the wrapper's documentation but at least they'll get there quickly rather than go through a bunch of details that do not matter to the big picture.

Louis
  • 146,715
  • 28
  • 274
  • 320