In case we need to make two step rest api call using magrite agent? How to go about the same? - palantir-foundry

I need to make a rest api call using magrite agent, but the challenge is the api call comprises of two steps.
POST call to receive the access token
GET call to use the bearer token and call the query
Let me know if anyone can help.

Assuming you get a new access token each time you want to get the data, the process is:
Call to get the token
Extract the token from the response
Call with the token to get the data
The magritte rest plugin supports this!
Preferred Approach
If you have access to edit the magritte source then there is a better way, you can use an Auth Call Source (magritte-rest-auth-call-source) to automate this process for all syncs. The source will perform steps 1 & 2:
type: magritte-rest-v2
sourceMap:
...
my_new_auth_source:
type: magritte-rest-auth-call-source
url: 'https://my-api.com/'
headers:
Authorization: '{%access_token%}'
authCall:
type: magritte-rest-call
method: GET
saveResponse: false
path: /get/access/token
extractor:
- type: magritte-rest-json-extractor
assign:
access_token: /token/json/path
The call would then look like:
type: rest-source-adapter2
oneFilePerResponse: false
cacheToDisk: false
restCalls:
- type: magritte-rest-call
path: /get/data
method: GET
Alternative
If the data and the token are at different domain names (i.e. requiring different sources), or if you cannot edit the source, then this is an alternative. Keeping any tokens in the source however is preferred, so only use this method if you have to.
You can have multiple calls in a single sync, and state is preserved between the calls, an example might look something like this:
type: rest-source-adapter2
oneFilePerResponse: false
cacheToDisk: false
restCalls:
- type: magritte-rest-call
path: /get/access/token
method: GET
saveResponse: false
extractor:
- type: magritte-rest-json-extractor
assign:
access_token: /token/json/path
- type: magritte-rest-call
path: /get/data
method: GET
saveResponse: true
parameters:
token: '{%access_token%}'
A couple of gotchas & other points with this approach:
saveResponse is true by default (I left it in the second call for clarity), so it needs to be explicitly set to false in the first call to avoid saving your token in the output dataset!
in the event that the data and token are behind different domains (so in different sources) you can add source: my_data/token_source to each call to point them at the right source.
More Detail
More detail can be found in the documentation, open the Foundry docs and search for REST API Plugin to find it.

Related

Creating a user in openedx by REST api (or anything else then traditional method)

I need to create users in Open edX and sign them in via an API call, and thus do all the API stuff. What the major idea here is to create a one log in system where my user can log into this software we have and thus browse all the courseware and attend classes and track his data through software. The interaction between course and the software will be done by the REST API.
Is copying his identity into the valid table/database of the openedx do the job, but it still won't solve the online problem.
This is not possible at the moment in Open edX, as there is no API to create users. See the list of available APIs.
But it would not be too difficult to create an extra endpoint to create new users. To that end, I suggest you make use of the UserProfileFactory available in students.tests.factories: https://github.com/edx/edx-platform/blob/master/common/djangoapps/student/tests/factories.py#L39
It's a factory used for testing but that can also be used in production -- it's a dirty hack, but it works.
It possible to create/register user in Open edX using the REST API.
Send POST method to this url: youredxdomain.com/user_api/v1/account/registration/
Send the method to body using form-data.
In the url above you will get field in json like this
{
restrictions: {
min_length: 3,
max_length: 254
},
required: true,
name: "email",
errorMessages: { },
placeholder: "username#domain.com",
defaultValue: "",
instructions: "",
type: "email",
label: "Email"
},
When you send POST method to this url make sure to set key based on value of name in the json, for example the code key of the json above is email and set value to your email. Do the same on other field.
Hope it help.

ExtJS Reading JSON

I am trying to recreate the Editable Big Data Grid from Sencha's Kitchen Sink. I have copied the files exactly (the code is found on the right under the collapsed details window) and the only change I have made is "KitchenSink" becoming the name of my app, "FreshApp". I also copied the BigData.js, Init.js, and Order.js files from the Sencha SDK. My app builds and gives me no errors, but the JSON data does not load, giving me a blank table.
I assume this code (found at /data/BigData.js) is supposed to serve up JSON to /FreshApp/BigData:
Ext.ux.ajax.SimManager.register({
'/FreshApp/BigData': {
type: 'json',
data: process([{
And I would assume this code (found at /app/store/BigData.js) is supposed to retrieve it:
proxy: {
type: 'ajax',
limitParam: null,
url: '/FreshApp/BigData',
reader: {
type: 'json'
}
},
When I navigate to http://localhost:1841/FreshApp/BigData, I get a 404. I have had no problems using XML, but JSON is giving me fits. Any help would be appreciated. Thanks.
Have you tried loading the json directly, without the SimManager? Put your json file somewhere on your app folder structure and point the url of the proxy to it as a relative path. Something like this:
proxy: {
type: 'ajax',
limitParam: null,
url: '../resources/MyBigData.json',
reader: {
type: 'json'
}
}
If this doesn't work either, and your a 100% sure, that the relative path is correct, then you should check if:
the MIME-Type for Json is configured in your server
you/your server has sufficient rights to access the resource folder/ the file.

Google Drive Upload Image - Data not returning webContentLink - Direct permenant link

I am uploading an image to the API successfully like this:
https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart
Authorization: 'Bearer ' + token,
'Content-Type': 'multipart/form-data; boundary="' + boundary + '"'
--data: multipartRequestBody
Where multipartRequestBody is:
{
name: 'screenshot.png',
mimeType: 'image/png',
// parents: ['0B5IwavgVGMN9ekR2dEhuaUxkZTA'] // set this to the id of a the folder you want to upload it into
};
Is there some keys I can provide to my mulitpartRequestBody that will tell GDrive to upload this as a publicly shared file and return the webContentLink? Which I can use in src of <img src="..."> with no restrictions on view bandwidth?
I have tried setting writersCanShare: true however it still won't return a webContentLink. I have also tried adding type: 'anyone', role: 'reader' but that didn't work either.
Basically I am trying to do what https://drive.google.com/uc?export=view&id={fileId} does, but within the insert call to the API.
Thanks
Short answer to all your requirements is no.
Drive is not designed to be a free public hosting service, but rather sharing/viewing/editing of content between people. You will run in to things like bandwidth limitations, abuse detection, etc if you really are trying to use it in a manner that requires "no restrictions on view bandwidth".
The other main part is you will have to do a files.get request from the id returned in the files.insert call - you will not be able to do it in a single API call on just the insert.
You would be much better served by using Google Cloud Storage which has a REST/JSON API similar to what you are using with Drive.

"No 'Access-Control-Allow-Origin'" Error while sending JSON Request to Shopify

I have Shopify account. In the Admin panel, Admin can add any number of products into Shopify store.
But I need to register Custom Product by the public user from my Shopify Store through the Client side.
i have tried the following code.
$('#send').click(function() {
$.ajax({
/* xxxx... : Api key
yyyy... : password */
url: 'https://xxxxxxxxxxxxxxxxxxxxxxxxxx:yyyyyyyyyyyyyyyyyyyyyyy#testing.myshopify.com/admin/orders.json',
type: 'POST',
dataType: 'json',
data: {
"product": {
"title": "Burton Custom Freestlye 151",
"body_html": "<strong>Good snowboard!</strong>",
"vendor": "Burton",
"product_type": "Snowboard",
"tags": "Barnes & Noble, John's Fav, \"Big Air\""
}
},
success: function(response) {
alert(response);
}
});
});
I am getting the following error:
XMLHttpRequest cannot load https://testing.myshopify.com/admin/orders.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://testing.myshopify.com' is therefore not allowed access.
#Deena. In case you missed it this use of Javascript is horribly insecure.
You are making your API keys public and hence anyone can alter or erase your entire shop.
Instead create a private App that you can properly control security wise, and use Javascript in a controlled fashion without exposing your API keys to the world.
You will be amazed at how quickly your reputation will be destroyed if you deploy unsafe code like this and your client ends up responsible for very bad things happening.
For anyone else drifting into this topic, your front-end JS can do this by calling an App Proxy, providing security and the ability to muck about with the RestAPI or the GraphQL approach.
The problem here is that your origin is insecure (http://) and resource is secure (https://). This causes the browser to treat them as different domains and since your resource (orders.json) doesn't specify that requests from http:// version of the URL should be allowed, the request fails, because by default cross-domain requests are forbidden for security considerations. You should be able to run the same script, if you access the page where your script runs via https://.

#3503 Error Attempting to Publish Custom Action via JSSDK

I am attempting to work out what is needed to publish a custom open graph action that includes user generated photos. I have made it through the approval process and am currently testing using the JSSDK. I have the following code which seems to work:
FB.api('/me/cheezburger-app:create', 'post', {
meme:'http://cheezburger.com/6459677184'
}, function(response){
console.log(response);
});
My understanding is, to publish this as user generated photo all I need to do is include an image property containing an array of objects like this:
FB.api('/me/cheezburger-app:create', 'post', {
meme:'http://cheezburger.com/6509097984',
image:[{user_generated:true, url:'https://i.chzbgr.com/completestore/12/8/13/c9Smb0ba2EGTLepkCgEp2g2.jpg'}]
}, function(response){
console.log(response);
});
Sadly, this returns the following error:
{"error":{"message":"(#3503) \"[{\"user_generated\":true,\"url\":\"https://i.chzbgr.com/completestore/12/8/13/c9Smb0ba2EGTLepkCgEp2g2.jpg\"}]\" is an invalid value for property \"image:url\" with type \"URL\"","type":"OAuthException","code":3503}}
I was able to successfully get the action to publish using the graph api explorer, but it is not using a javascript object and jquery. I assume I am doing something wrong with the way I am passing the extra parameter. But I just can't figure out what.
I haven’t tried this yet, but my guess (since it’s not clearly described in the docs) would be that you have to give a string value containing JSON-encoded data like in other similar cases.
image: '[{"user_generated":"true","url":"https:\/\/i.chzbgr.com\/completestore\/12\/8\/13\/c9Smb0ba2EGTLepkCgEp2g2.jpg"}]'