call multiple APIs synchronously and show results immediately - json

Am working in jQueryMobile and PhoneGap.
Currently am facing a Big issue. When I click a button and at that time its calls multiple APIs one by one and get results one by one. But in result page its shows only after getting all the Results of all API.
In My Application When I Submit the Button and It return a JSON which contains the list of Different APIs to Call
Then We call each API asynchronously.
We get all the result one by one. And Result is showing only after get all the results.
Then My Question is is there any way to shows the result page when I get First API Result and call all other API at that same time when Data shown in Result page in background?
Currently for shows the result Page it takes more than 30 seconds time. But We need to shows the result page when we get first API result. We get result as JSON Format.
Anybody please can help Me :(
i lost my 1 week for resolve this issue !!

There are a number of HTML template solutions are available jQuery Templates, Underscore, and Mustache etc, better to use Mustache i think that solves your problem. its better to use any templates if you are working on so many DOM manipulation in your projects.
Eg:
HTML Template
<h1>{{header}}</h1>
{{#bug}}
{{/bug}}
{{#items}}
{{#first}}
<li><strong>{{name}}</strong></li>
{{/first}}
{{#link}}
<li>{{name}}</li>
{{/link}}
{{/items}}
{{#empty}}
<p>The list is empty.</p>
{{/empty}}
JSON DATA
{
"header": "Colors",
"items": [
{"name": "red", "first": true, "url": "#Red"},
{"name": "green", "link": true, "url": "#Green"},
{"name": "blue", "link": true, "url": "#Blue"}
],
"empty": false
}
Out put HTML
<h1>Colors</h1>
<li><strong>red</strong></li>
<li>green</li>
<li>blue</li>

Related

Logic App designer removes foreach loop without error or exception

I have a logic app that uses a webhook trigger on a service bus message queue to post selected messages to an API app using a HTTP+Swagger action.
Azure Logic App Designer
Here’s the json array (DBChanges) from the triggerBody that the foreach is supposed to iterate over
"DBChanges":[{"Key":"ItemID","Value":"101"},{"Key":"Description","Value":"Decript the message"},{"Key":"Owner","Value":"Samuel"}]
This is the logic app code for the DBChanges POST. The foreach loop is supposed to iterate over all the elements of the DBChanges array which is a key, value pair in the swagger metadata.
Azure Logic App Code View
When I switch to design mode after adding the foreach loop the designer strips the foreach code out even though it appears syntactically correct.
Does anyone know why the logic app designer strips out the foreach when switching between Design and Code views?
Try update your condition to "expression": "#equals(triggerBody()['Description'], 'Create')",.
Within the condition, you want to have a for-each first, then HTTP action inside it, not the other way around (I'm using Compose as an example, but you can substitute it to HTTP+Swagger in your case), note how I use #item() to reference each value inside the DBChanges array.
"actions": {
"For_each": {
"actions": {
"Compose": {
"inputs": "#item()",
"runAfter": {},
"type": "Compose"
}
},
"foreach": "#triggerBody()['DBChanges']",
"runAfter": {},
"type": "Foreach"
}
}
To help you reference the right value in a JSON blob, we recently added action Parse JSON, you can add it after your HTTP Webhook trigger, provide the body as input, and include a schema of the JSON it will be returning. You will be able to use friendly tokens from the picker instead of having to handcraft them. :)
Hope this helps, feel free to reach out to me at Derek.Li (at) Microsoft dot com.

Should the server respond one json for all content data on the page in SPA or is better to split it?

I'm building API for SinglePageApplication, which handle by Angular in frontend. One thing is not clear to me.
Supose the web applcation has delati journal paige wich display journal,some articles which belongs to this journal and some cool authors which can be not connected to this journal.
Should I build my api urls based on each need page content, for example:
from url /api/journal/<journal_id>
send json:
{
"journal": {
"id": 10,
"name": "new_journal",
"articles": [
{
"name": "cool_article",
"id": 42
},
{
"name": "another_cool_article",
"id": 43
}
]
},
"authors": [
{
"name": "some_name",
"id": 42
},
{
"name": "another_name",
"id": 43
}
]
}
Or I should build my api based on concrete objects and related objects of them.
With urls like this:
/api/journals/<journal_id>
/api/authors/
And frontend side build this page with two GET requests for fetching content.
Sory if my question too broad, I just want to find best bractice for building API to SinglePageApplications.
Does it have any difference of building API enpoints for external web-apps and what I should do if page need to display more objects, which not belong together? Which of the options above is better?
There isn't really a universal right answer for this. It largely depends on the use case for that data you're fetching. I would say to err on the side of splitting this into multiple requests as it grants you flexibility and efficiency in terms of partial updates to the page. That approach also makes exposing an API to the public much easier in terms of being able to just expose what you already have.
If you're dealing with a potentially large (an intentionally relative term) number of concurrent requests though, you may build some composites of related data to mitigate that.
Of course, you could also do a combination of the two as well (first load makes 1 large request, subsequent updates are segmented).

All metafields fetched via Shopify API got improper owner and all belong to "shop"

I got problem with shopify API and with data it provides. When i try to get list of all meta-fields with my app I end up with invalid data that tell all meta-fields are owned by shop (owner_id, owner_resource in JSON result). In shop I work with there is over 2000 meta-fields, created mostly by installed apps, that most certainly belong directly to resources like page, blog or product and most of them are most certain not associated directly with shop.
What is more strange when i use chrome plugins or free shopify app to edit meta-fields, that in data i recieve present as associated with shop, they are displayed as belonging to particular aspects of shop as it should be. Adding or deleting meta-field does not reflect on the data or count of meta-fields I get in way explained below.
So what I do first is getting count of metafields with following request, to later know how split data requests into pages.
/admin/metafields/count.json
then to get complete set of data i make series of requests like this
/admin/metafields.json?limit=200&page=1
...
As result i get (example data)
{ "id": 2488777, "namespace": "content_value", "key": "[default]3883205", "value": "There are no items in your cart. ", "value_type": "string", "description": "", "owner_id": 2174481, "created_at": "2014-12-30T20:23:56+01:00", "updated_at": "2014-12-30T20:33:56+01:00", "owner_resource": "shop" }
So my question is what wrong I do here making these requests and why i get invalid data, or it is maybe another "shopify thing" to get such behaviour. Can someone direct me how to get full valid list of meta-fields with rightfully associated owners? Is there any caching involved with metafields that could cause lack of update?
When you make a call to /admin/metafields.json without any query parameters then you will see the metafields that belong to the shop.
You can retrieve metafields that belong to another resource by making an authenticated GET request to a URL like this one: /admin/orders/3250063171/metafields.json
An alternate format for the URL is this: /admin/metafields.json?metafield[owner_id]=3250063171&metafield[owner_resource]=order

Slow responce whie parsing large JSON responce

I have developed a web application based on RESTFul design where the application takes JSON responce from JAVA based web service and displays in UI and it refreshes the data in every 5 seconds.
The application uses Bootstrap for UI design, Backbone and require.js for implementing an MVC stucture where JSON response is parsed as Backbone collection.
When an admin is using this application the JSON response size is too large(from 800 to 1100 objects).
This is where things get messy. As per my analysis the browser is taking up too much resource.So rest of the application is very slow. For eg if I try to open a modal, system freezes for some time and opens slowly thus giving a very poor user experience.
As per my analysis time is being taken in parsing the data
As a remedy I am removing all comments in code and trying to implement Gzip compression for JSON files/html/css/js.
Sample of the JSON object is pasted below
{
"name": "TEST",
"state": "Lunch",
"time": "00:00:09",
"manager": "TEST",
"site": "C",
"skill": "TEST",
"center": "TEST",
"teamLead": "TEST",
"workGroup": "TEST",
"lanId": "TEST",
"dbID": "TETS",
"loginId": "TEST",
"avgAcwTime": "nn",
"avgHandleTime": "nn",
"avgTalkTime": "nn",
"callsAnswered": "nn",
"dispSkill": "-",
"errCode": null,
"errDesc": null,
"avgAcwTimeth": "medium",
"avgHandleTimeth": "high",
"avgTalkTimeTh": "medium",
"callsAnsweredTh": "medium",
"stateTh": "high"
}
Pagenation can't be done due to some requirements.
Can any one suggest something to improve the perfomance
Also I am fetching data using Backbone.Collection.fetch()
getAgentMetric(){
this.metrices.fetch({
url : (isLocal) ? ('http://localhost:8080/jsons/agent.json') : (prev_this.url + '/agentstat'),
data: JSON.stringify(param),
type: "POST",
dataType: "JSON",
contentType: "application/json",
})
.done(function() {
// passing the datasource from ajax call
prev_this.agentLoacalSource.localdata = prev_this.metrices.toJSON();
});
timeout = setTimeout(_.bind(this.getAgentMetric, this), 5000);
},
Browsers can handle a heck of a lot more than a thousand objects without any strain, so I don't think it's the fact that you are simply requesting a large amount of data from the backend. It's more likely that some of your parsing or rendering code is slow.
A few possibilities without seeing any more of your code:
It really depends on what you're doing here, but I'm going to assume that you aren't using a templating library (hoganjs, handlebarsjs, etc). You should definitely look into using one as they speed things up quite a bit and make generating html a lot easier.
Are you running .append() for each individual model that you render? This will really slow things down. You should generate all of the html that needs to be generated, and then run .append() once.
What kind of event listeners are you adding for each model (if any)? Listing to scroll events without a debounce ends up slowing down your browser, especially if you add a bunch of them.
Unrelated to your slowness issues, there are a few problems that I see with this code:
Your timeout should be called from an .always() function in ajax to prevent concurrent requests from going out if for whatever reason a request is slow.
this.metrices.fetch(...)
.always(function() {
timeout = setTimeout(...);
}.bind(this));
Requests that are simply fetching data should use a GET instead of a POST request type. You can see https://stackoverflow.com/a/3477374/5780021 for more info about this.
I would recommend timing some of your code to see where the slowness is actually happening. This will allow you to actually determine how long things are taking between to points in code.
Firefox console.time
Chrome console.time
IE console.time

Does anyone know of a webservice for looking up definitions of words that would be able to return results in JSON?

I found http://words.bighugelabs.com/api.php but nothing like this for definitions/dictionary.
Ideally I'd grab a dictionary file and build my own API for this, but this is for a demo and we need something short-term that can be called from within a javascript function.
wiktionary.org provides an API for example:
http://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=Television&format=json
gives back
{
"query": {"searchinfo": {"totalhits": 208862},
"search": [{
"ns": 0,
"title": "Television",
"snippet": "<span class='searchmatch'>Television<\/span> (TV) is a widely used telecommunication medium for transmitting and receiving moving images , either monochromatic (\"black <b>...<\/b> ",
"size": 28228,
"wordcount": 3566,
"timestamp": "2009-10-02T15:09:56Z"},
...
]},
"query-continue": {"search": {"sroffset":10}}
}
I think this is what you are looking for
bighugelabs API - Json fromat
aonaware services - XML format
Not sure if it would fit your needs, but answers.com has webmaster tools that offer various services, including dictionary lookup. Don't know if any can be called from javascript.
At short notice you could set up a reverse-proxy on your server that lets you AJAX your favorite dictionary website and then 'scrape' the definitions from the document that is returned. It's obviously not a long term solution but for a one time thing, you probably won't get into trouble.
This is a web service and have several dictionaries:
http://services.aonaware.com/DictService/DictService.asmx
P.S. I did not notice the JSON part of your question.