Unsure how to retrieve data from a custom endpoint? - heroku

Context to this post is I'm a java developer attempting to teach myself Ember. It isn't going well. I realize this question is pretty vague so I apologize, I'm not even sure what I should be asking...
I need to pull data into a model, i.e. via some sort of query, from a heroku json endpoint. In the application.js file, I have the following:
import DS from ‘ember-data’;
export default DS.JSONAPIAdapter.extend({
host: 'https://cag-brain.herokuapp.com'
});
Ideally I would like to pull this data into a user model, then display that data on a page as a sort of proof of concept. This unfortunately gets me nothing. Nor am I even sure I'm going about this correctly. Should I be doing something different than attempting to use Host Customization? Any guidance would be much appreciated!

There are different things involved for retrieving records via ember-data.
First of all you should define your models:
// app/models/post.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string')
});
You should retrieve records in a model hook of a route.
// app\routes\posts.js
import Route from '#ember/routing/route';
export default Route.extend({
model() {
return this.get('store').findAll('post');
}
});
Then you should configure your api host and maybe a namespace. You included that step in your question:
// app/adapters/application.js
import DS from ‘ember-data’;
export default DS.JSONAPIAdapter.extend({
host: 'https://cag-brain.herokuapp.com'
});
If your api does not implement JSON Api specification you need to customize your serializer and adapter. Ember-data ships with a RESTAdapter/RestSerializer additionally to the default adapter and serializer which implements JSON Api spec. There is also one abstract adapter and serializer If you need to start from scratch. Before that I would definitely have a look if there is any community adapter/serializer fitting your needs.
To decouple api and client development and to speed up tests I would recommend ember-cli-mirage which allows you to mock your api.

Related

Send files via Apollo Client and receive in Graphene Flask server

I'm using Graphene package in my Flask app (https://github.com/graphql-python/graphene) and graphene-file-upload (https://pypi.org/project/graphene-file-upload/). I need to receive a file through a mutation as bellow:
class Picture(graphene.Mutation):
class Arguments:
file = Upload(required=True)
numerical_id = graphene.Int()
def mutate(self, info, file):
... continues
class Mutations(graphene.ObjectType):
picture = Picture.Field()
But when I send the mutation via an Apollo client (https://www.apollographql.com/docs/react/) I'm receving this errors:
Unknown type "Upload". Perhaps you meant "Float"?
Cannot query field "picture" on type "Mutations".
Is there any workaround for it? Thank you in advance !
Did you import Upload from graphene_file_upload.scalars? You also need to make sure you fix the view function you hook up to your /graphql view
I spent a couple of hours trying to figure this out since the mutation worked fine via Postman but not when sent from the Frontend.
The problem was that the parent component was using a different ApolloProvider from the one I was expecting.

Connecting Spring Boot and Ember

I'm new to web development stuff and have been looking at some ember, I wanted to try running this with Spring boot. I got spring boot working and was able to run the sample blog example created with ember.
Now I want to some data read from the spring server, for example I can go to http://localhost:8080/greeting?name=User and get the following displayed onto the page
{"id":12,"content":"Hello, User!"}
What I want to do is get ember to read make that call instead of me manually adding it in the url and getting ember to then display that data.
So in ember my app.js looks like this
App.IndexRoute = Ember.Route.extend({
model: function() {
return $.getJSON("http://localhost:8080/greeting?name=User");
}
});
This I believe makes the call. I was trying to re-display the data onto the onto the index.html like this:
<script type="text/x-handlebars" data-template-name="index">
<Not sure what to put here>
</script>
I may be missing something obvious or not getting something. I apologize if thats the case. Any help will be welcome
The model hook in your route should resolve to {"id":12,"content":"Hello, User!"}. Once that is resolved, Ember will populate the model property of the corresponding controller (in this case, IndexController, which is auto-generated if you don't provide one).
As the index template you are using is backed by that controller, you can refer to any of its properties, including model:
<script type="text/x-handlebars" data-template-name="index">
{{model.content}}
</script>
I hope you can make it work!

How to mimic a grafana backend?

My application producers counters. I'd like to use grafana to view them. It seems grafana depends on influxDB or elasticsearch.
Is there a way to make grafana read data from my own app so I don't need to store them in another backend?
You can include an OpenTSDB datasource in between your App and Grafana.
Like this:
datasources: {
'OpenTSDB-TEST': {
default: true,
type: 'opentsdb',
url: "http://my_opentsdb_server:4242"
}
}
See more OpenTSDB configuration details here
You can use this plugin: https://github.com/grafana/datasource-plugin-genericdatasource
To configure it with version 2.6 do the following:
Put the plugin's files to folder, let's say, "genericdatasource".
Then copy this folder to /public/app/plugins/datasource/.
In directives.js change templateUrl of query.editor.html to:
'public/app/plugins/datasource/genericdatasource/partials/query.editor.html'
also change templateUrl of query.options.html to:
'public/app/plugins/datasource/genericdatasource/partials/query.options.html'
In plugin.json change module to:
'app/plugins/datasource/genericdatasource/datasource',
and change config to:
'public/app/plugins/datasource/genericdatasource/partials/config.html'
Then restart grafana-server. The new data source should now be avalilable in the data source type dropdown in the Add Data Source View.
Use option "proxy" there (not "direct") to work correctly with cross-domain requests.
You just need to implement 3 methods in your backend: /,search, query.
Look at examples here:
https://gist.github.com/bergquist/bc4aa5baface3cffa109
https://gist.github.com/tral/1fe649455fe2de9fb8fe

Sails.js 0.10 and passport

I was fiddling with sails.js and passport. Seems they are mend to be used together.
I have made the user models available in de db. And I am able to use the authentication by bcryptjs. This works, but dearly want to like to automatically authenticate every call, and mostly socket.io blueprint calls.
Still, I am searching for an elegant method to enable the sails.js way of integrating passport.
There are many examples, but seem a bit out-dated, not sure.
Important is the sockets. They need to be authenticated.
For e.g. every io.socket CRUD method, would be nice.
Kind regards
I found a link (http://www.bearfruit.org/2014/07/21/tutorial-easy-authentication-for-sails-js-apps/) that's pointing me in the right direction, but still not clear how this is reflected in for instance controllers
What I am trying to manage, is to have the custom api calls authenticated before they are called (configured in routes.js or by means of blueprints).
e.g:
OrderController:
module.exports = {
placeOrder: function (req, res) {
if (true === req.isSocket) {
// Pseudo code:
if (passport.authenticated(['user','admin'])) {
Order.save();
}
// end pseudo code
}
}
}
How should/is the above method secured, and I am I able to use for instance user-roles here?
I know two good solution for you question:
sails-auth
sails-generate-auth
Both implements passport, I recommend the first one, because it creates a layer to handle all authentication difficulties in model and services

Retrieving JSON from an external API with backbone

I'm new to backbone.js and I've read other solutions to similar problems but still can't get my example to work. I have a basic rails api that is returning some JSON from the url below and I am trying to access in through a backbone.js front end. Since they are one different servers I think I need to use a 'jsonp' request. I'm currently doing this by overriding the sync function in my backbone collection.
Api url:
http://guarded-wave-4073.herokuapp.com/api/v1/plans.json
sync: function(method, model, options) {
options.timeout = 10000;
options.dataType = 'jsonp';
options.url = 'http://guarded-wave-4073.herokuapp.com/api/v1/plans.json'
return Backbone.sync(method, model, options);
}
To test this I create a new 'plans' collection in my chrome console using "plans = new Plans()" and then "plans.fetch()" to try and get the JSON.
When I call plans.models afterwards I still have an empty array and the object that returns from plans.fetch() doesn't seem to have any json data included.
Any ideas where I'm going wrong?
I have had the same problem before. You should not have to override your sync method.
Taken from Stackoverflow Answer
"The JSONP technique uses a completely different mechanism for issuing HTTP requests to a server and acting on the response. It requires cooperating code in the client page and on the server. The server must have a URL that responds to HTTP "GET" requests with a block of JSON wrapped in a function call. Thus, you can't just do JSONP transactions to any old server; it must be a server that explicitly provides the functionality."
Are you sure your server abides to the above? Test with another compatible jsonp service (Twitter) to see if you receive results?
Have you tried overriding the fetch method as well?
You should add ?callback=? to your api url in order to enable jsonp

Resources