Skip to content
Rasmus Lindahl edited this page Jan 10, 2020 · 2 revisions

Using our API

The API has been built to create an easy to use interface to communicate with the repository. At the current moment, it only has read functionalities. These are searching for Snippets and fetching a specific Snippet.

Searching for Snippets

Searching for Snippets is one of the key functionalities in this project. It works by specifying a query and a language. The server will then return a set of potential Snippets as JSON objects. It is perhaps important to note that the query does not only try to match against the Snippet name but also against tags and descriptions. If a developer, for instance, wants to return all Snippets matching with "radius" in Python3 the following URL would be correct:

https://snippetdepot.com/search/python3/radius

For this example. The following JSON object would be returned:

{"results": [
{
      "id": 1, 
      "funcName": "getRadiusOfEarth", 
      "input": "", 
      "example": "Returns radius", 
      "desc": "This function returns the earth radius in kilometers."
}, 
{
      "id": 2, 
      "funcName": "getVolumeOfSphere", 
      "input": "sphereRadius", 
      "example": "3 => 36pi", 
      "desc": "This function takes a radius of a sphere and returns its volume."
}]}

This information can then be used to create an autocomplete item in IDEs. The information returned might be subject to change.

Fetching a Snippet

After finding a set of potential Snippets it is possible to choose the prefered one by using the getSnippet functionality. Accessing a Snippet is done by specifying its ID. The ID is retrieved when using the search functionality. Compared to Snippet searching the fetching functionality will retrieve all the information contained in a Snippet. An example of fetching the Snippet with the ID equal to 2 would be:

https://snippetdepot.com/getSnippet/2

This query would return the following:

{
      "funcName": "convertLightyearsToMeters", 
      "tags": "light year units to meters, change light year to meters, conversion, light-years", 
      "input": "lightyears", 
      "example": "1 => 9460000000000", 
      "deps": "", 
      "author": null, 
      "desc": "This function takes a number of light-years and converts the length into meters.", 
      "lang": "python3", 
      "code": "def convertLightyearsToMeters(lightyears):\n\treturn lightyears*9.46*pow(10, 12)"
}

This information can then be used to incorporate the Snippet into a workspace.

Clone this wiki locally