How do I specify specific data from an API? - api

I am attempting to extract specific data from the Steam API so far I have been able to filter the results, here is an example;
https://store.steampowered.com/api/appdetails?appids=218620&filters=metacritic
Which gives me;
{"218620":
{"success":true,
"data":
{"metacritic":
{"score":79,"url":"https:\/\/www.metacritic.com\/game\/pc\/payday-2?ftag=MCD-06-10aaa1f"}
}
}
}
However, what I want is just the numerical score, so in this instance, it would just display '79'.
I am presenting the data using an iframe which is why it needs to call the specific value. If anyone has any suggestions about how I can do this or maybe offer up an alternative way to parse the data.
My coding skills are limited so this might seem like a basic question or a rather rudimentary way to extract the data so apologies in advance. I am also adding the code to Shopify which is PHP based if that helps.
I know https://steamspy.com/api.php have their own API which offers similar data would it be easier to collect it from there instead of directly from the Steam Store?

The data you get is in JSON format. JSON is designed for easy data access.
You did not specify your programming language and I have no experience with shopify. So here it is in Javascript. Try this JSFiddle.
const jsonObject = {
"218620":
{"success":true,
"data":
{"metacritic":
{"score":79,"url":"https:\/\/www.metacritic.com\/game\/pc\/payday-2?ftag=MCD-06-10aaa1f"}
}
}
};
const metacriticScore = jsonObject["218620"].data.metacritic.score;
console.log(metacriticScore);

Related

Can prisma achieve the same things as graphql?

I'm having a hard time understanding what graphql can achieve on top of prisma besides having a singular endpoint.
I've been hearing that the main selling point of graphql is the fact that you are able to query for the specific fields you want to receive, but can't that also be achieved with prisma using the "select" property?
const users = await user.findMany({
select: {
email: true,
},
});
Thank you for your answers!
I understand your confusion. I'll try to make the difference between Prisma and GraphQL clearer for you.
Prisma
Prisma is an ORM like tool. This means that it helps you search for data in your database. Helps you create queries, join data and etc. Prisma is a tool that lives in your back-end (usually) and helps you find the data that you will send to your front-end or mobile application.
GraphQL
GraphQL is a way to communicate with your back-end. So let's say that you want get the name of a user based on its id. On a REST API (that is the more common way) you would send a GET request to the server, and it would respond with a JSON with all of the data for this user.
// Imagine this is your request to the server
await api.get(http://myserver.com/user/1)
// Imagine this is your JSON response
{
name: "Person",
age: 24,
address: "Avenue Street, number 7",
...
}
You see that even though you just wanted the name of the user you got all of the data, when using a REST API. But with GraphQL that is not the case. You can request for only the data that you want.
// Your request to the server would look like this
{
GetUserById(1) {
name
}
}
// In the response you would be getting a JSON with only the data that you requested
{
name: "Person"
}
That is what GraphQL is used for, requesting specific data to your back-end and receiving only what you need. In both cases, REST API and GraphQL, you can use Prisma. It will help you search in your database for the data that was requested.
If you want to know more about Prisma you can check their website where you will find more about how it works and how to use it.
If you want to know more about GraphQL you can check their FAQ page
You can also learn how to use Prisma and GraphQL together

How to reuse data obtained by axios calls between VueJS components?

I am sorry if this question has already been asked, but i can't seem to find a clear explanation for my problem.
Problem: i have a web page showing a client's problem. This page need data like client ID or problem number to display it in different parts of the page (header, menu, body). This data is obtained with axios calls.
So i have multiple components which need the same data from the same axios call.
I can't understand how it is possible to make one axios call, for client ID for example, and share it with multiple components instead of making an axios call each time i need the data.
Are mixins a solution?
I have tried to use a mixin:
Vue.axiosGet({
methods: {
request: function(url) {
return axios.get(url).then(response => {
return response.data;
});
}
}
});
But now i don't know where or how to store the data i'll get.
EDIT: i forgot to precise that i don't want to use VueX.
You say that you don't want to use Vuex, but why not? I would recommend you use the Vuex store to achieve what you're looking for. A vuex store will cache/hold information in it, meaning that you can easily access it on a different page. Vuex would solve your problem and allow you make a call once, and later access the data. No need to re-invent the wheel, it's an effective way to achieve what you need.
If you really don't wanna use Vuex, there're always solutions like nedb, but I'd strongly recommend Vuex.

Pulling data, mainly text from websites

I'm fairly new to programming in general but I have delved into vb.net recently. I was looking into how to grab data from a website but don't fully comprehend how to or through what means.
My main end game is to just pull data for a lack of better word. Say for instance, a website shows text, I'd like to be able to pull that text into my program using vb.net. Is that possible?
I don't know how to use javascript, php and know only little of html/css. I'm of course willing to learn but I haven't had much luck searching for this specific information or solution. Without access to the websites database, is there another way for my app to read the website for what it would be displaying and retrieve this information? I'm mainly concerned with text, no other information is really needed. From what i've gathered, the information is tagged by something like an ID, or class? Any help would be appreciated.
Web servers essentially have 2 operations, POST, which is you sending something to the website, and GET, which is you requesting something from the website.
For the most part you can just do a GET of some sort on the website and it'll return what you request in a data format, likely JSON. You can often look for fields based on CSS selectors in the web page itself. Though, there are other ways and some HTTP libraries may return the data to you in various formats, mainly JSON and XML.
I'd suggest right click->view source on a web page to learn more about how they're structured. As for how to do a GET in javascript on some website, this code would grab the website at the URL you give it.
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); //the get request
xmlHttp.send( null );
return xmlHttp.responseText; //the website data in XML format.
}

Hypermedia Api - Presenting picklist data

I am creating a hypermedia api that conforms to the HAL spec
When a user submits a payment they need to specify what type of card they are using (Visa, Master Card etc)
So for a particular field that is submitted there is a specific list of values that can be used
How do I present that pick list to the user?
As embedded data?
Is there generally a way to associate a field with a given set of data?
I realise the HAL spec is very small and doesnt cover this issue specifically. But in general hypermedia apis how do people usually present this data?
Or should I simply explain the field in the CURIE link?
thanks
You are right, HAL does not specifically cover this issue. You can solve this by essentially copying HTML. There are different widgets defined in HTML to present stuff, for example a combobox with listed options.
You can define a media-type that has similar controls in it, and you can define the processing model for the media-type as well. It can be a json representation of course, does not need to be xml.
For example
{
...
"cardType": {
"inputType": "select",
"possibleValues": ["Visa", "MasterCard", ... ]
}
...
}
There is no ready-made format that I know of unfortunately.

Retrieve the list of friends that did a custom action on a custom object in open graph 2

I would like to do something like facepile using the graph api with open graph 2 actions : from a custom object and a custom object, give me the friends (using my facebook application) that did this action on this object.
The problem is that using FQL, I cannot query custom objects and actions. Using the graph API, I cannot find a way to intersect the list of my friends with the object I'm interested in.
The best I could do was the following using the batch mode of the graph API :
batch=[
// First we get the list of friends that are using my facebook application
{ "method": "GET", "relative_url": "fql?q=SELECT+uid+FROM+user+WHERE+uid+IN+(SELECT+uid1+FROM+friend+WHERE+uid2=me())+AND+is_app_user=1+LIMIT+0,49", "name": "friends"},
// Then query each friend to get the list of objects that went through my namespace:testaction
{ "method": "GET", "relative_url": "{result=friends:$.data.0.uid}/namespace:testaction" },
{ "method": "GET", "relative_url": "{result=friends:$.data.1.uid}/namespace:testaction" },
...
{ "method": "GET", "relative_url": "{result=friends:$.data.49.uid}/namespace:testaction" }
]
It's quite inefficient and does not fully resolve my issue since :
I still have to filter the results to get only the one that matches
the object I want
If there is a large number of objects in namespace:testaction, I have to go through paging, doing more queries (I try to minimize the number of queries)
Do you see a better way to do this ?
This probably isn't exactly what you're looking for, but given the fact that facebook (AFAIK) doesn't provide (and will probably never provide) the ability to do this. I think you should simply store the information yourself and then query the data from your own database. It would be like what you're doing in your question, but you can optimize it since it's your database.
I'm sure you thought about this already, but someone had to say it.
It's now possible to do this with one Graph API request:
GET https://graph.facebook.com/me/friends?limit=50&fields=name,namespace:testaction.limit(100)
see field expansion and updates to the graph API.
If the answer derickito gave is not enough, you should explore getting your app on the Facebook white-list (aka become a partner) to get at some the private Graph API where this functionality might exist, but is not available for "normal" application that are stuck using the public Graph API.