1

I envision loopback's datasources & models as a useful tool for consuming an API, not just for automatically creating a REST API.

Pretend I'm using Spotify's API. I want a command line application that looks up user information on Spotify. I could write loopback models to consume say /v1/albums.

I'm hoping I can create an Album model and use it like Album.find('Thriller');.

What I don't want is to create a REST API. I just want a better language to consume other people's APIs such as Facebook or Instagram.

1
  • Based on your update, and the statement that you "don't want ... to create a REST API", then I don't think LoopBack is a good choice. It is entirely dedicated to creating REST APIs. Commented Dec 21, 2015 at 19:11

3 Answers 3

1

A LoopBack model is just a config file and a collection of functions in a model file. All of the endpoints are generated by LoopBack itself, and as such not very helpful by themselves. That said, you can generate a swagger spec for each of your models with the explorer component (installed by default when you use the scaffolding CLI: slc loopback).

Simply start up your LoopBack application, then navigate to:

http://localhost:3000/explorer/resources/MyModels

You could then use the swagger spec in any framework that supports that standard.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm interested in using these models to consume apis not create them.
You may need to provide much more information in order to understand your use case then. LoopBack is the API framework, the model is simply an entry point into the API, it doesn't really make sense to use the "model" outside of LoopBack because the model is simply a config file.
1

Loopback has the concept of non-database connectors, including a REST connector. From the docs:

LoopBack supports a number of connectors to backend systems beyond databases.

These types of connectors often implement specific methods depending on the underlying system. For example, the REST connector delegates calls to REST APIs while the Push connector integrates with iOS and Android push notification services.

This is adapted from the documentation for Spotify (I haven't tried this though):

datasources.json

Album": {
  "name": "spotify",
  "connector": "rest",
  "debug": false,
  "options": {
    "headers": {
      "accept": "application/json",
      "content-type": "application/json"
    },
    "strictSSL": false
  },
  "operations": [
    {
      "template": {
        "method": "GET",
        "url": "https://api.spotify.com/v1/albums/",
        "query": {
          "album": "{album}"
        },
        "options": {
          "strictSSL": true,
          "useQuerystring": true
        }
      },
      "functions": {
        "find": ["album"]
      }
    }
  ]
}

You could then call this api from code with:

app.dataSources.Album.find('thriller', processResponse);

3 Comments

Can you use the data source without attaching it to an app?
@Breedly what do you mean by an 'app'
You can't have a LoopBack datasource without a LoopBack app... I think what you're trying to do is separate the datasource "ORM" layer from LoopBack, and that just doesn't make any sense. There is no such thing. The code above is a configuration for a LoopBack datasource, it doesn't exist on its own.
-1

One good approach is to use their rest-connector which will allow you to template your request and response calls to the external API, use model validation on the data that comes back and then allow you to persist that new content to your database.

1 Comment

This answer does provide a correct solution the loopback rest connector allows to consume external apis. Why was this downvoted ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.