Create a Web Service in Intel XDK for Parse.com RE

2020-06-30 03:04发布

Has anyone had success creating a new web service in XDK for either DreamFactory or Parse.com REST APIs? I'm able to get a response via curl from a command line for both, so it seems like it should be doable. So far I haven't been able to make either of them work.

For reference, these are the curl api calls:

DreamFactory (requires a session opener call first):

$ curl -X POST http://ec2-[my server].compute.amazonaws.com:80/rest/user/session -H "X-DreamFactory-Application-Name: testapp" -d '{"email": "test@example.com", "password" : "[my password]"}'


(This returns a large JSON string which includes the session ID, used below)

$ curl -X GET http://ec2-[my server].compute.amazonaws.com:80/rest/testapp/roles -H "X-    DreamFactory-Application-Name: testapp" -H "X-DreamFactory-Session-Token: [my session]"
{"record":[{"id":1,"rolename":"Agent","description":"agent"},{"id":2,"rolename":"Client","description":"client"},{"id":3,"rolename":"Admin","description":"administrator"}]}


Parse.com:

$ curl -X GET   -H "X-Parse-Application-Id: [my appid]"   -H "X-Parse-REST-API-Key: [my api key]"   https://api.parse.com/1/classes/TestObject
{"results":[{"foo":"bar","createdAt":"2014-07-19T22:07:52.874Z","updatedAt":"2014-07-19T22:07:52.874Z","objectId":"jSpF1RrOy4"}]}


I'm new to JSON, so I suspect something is wrong in one or more of my apiconfig.json, testapp.json, or testapp.js files. I've experimented with them enough that they're kind of a mess now, but I can post them if it'll help. I'm hoping someone who has successfully created an XDK web service for either of these APIs or one like them can provide some guidance.

Thanks!

3条回答
成全新的幸福
2楼-- · 2020-06-30 03:25

Copied from an XDK support engineer's reply to a separate question on their HTML5 dev forum (http://go.shr.lc/1nr6fsT):

For APIs that need two keys in the URL string, add these fields to the apiconfig.json file:

"auth": "key",
"keyParam": "apiKey",
"signature": "apiSecret"

The key values can be accessed as credentials.apiKey and credentials.apiSecret in the .js file.

For APIs that need 2 keys via the headers. Put the required headers in a variable, key_info, and in the .js file, use this:

return $.ajax({url: url, 
               data: key_info
              });

In the code you have posted, the service name listed in apiconfig.json file is 'parsedbtest' while the file names are 'parsetestdb.js' and 'parsetestdb.json'. Fix this by changing the apiconfig entry to:

{
    "parsetestdb": {
        "name": "test db parse.com",
        "dashboardURL": "https://www.parse.com/docs/rest",
        "auth": "key",
        "keyParam": "apiKey",
        "signature": "apiSecret"
    }
}

Then your parsetestdb.js will be:

(function (credentials) {
  var exports = {};
  exports.TestObject = function (params) {
    var url = 'https://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:javascript-key='+ credentials.apiKey +'@api.parse.com/1/classes/TestObject'; //Or credentials.apiSecret.
    return $.ajax({url: url});
  };
  return exports;
})

You can also use params.objectId and params.foo to enter other parameters.

Also, the method name used above is 'TestObject'. This should match the method name in the .json file (thus, no whitespaces). So the parsetestdb.json will be:

{
   "endpoints":[
      {
         "name":"Methods",
         "methods":[
            {
               "MethodName":"TestObject",
               "HTTPMethod":"GET",
               "URI":"TestObject",
               "RequiresOAuth":"N",
               "parameters":[
                   {
                     "Name":"objectId",
                     "Required":"N",
                     "Location":"query",
                     "Type":"string"
                  },
                  {
                     "Name":"foo",
                     "Required":"N",
                     "Location":"query",
                     "Type":"string"
                  }
               ]
            }
         ]
      }
   ]
}
查看更多
小情绪 Triste *
3楼-- · 2020-06-30 03:25

I do not believe your problem is Intel-XDK related but posting some code may help us solve the issue or give some guidance. Although, if you changed some of the configuration files within the project then you might run into some issues.

AJAX (Asynchronous JavaScript and XML) can be used to interact with REST web services from within your project. There is plenty of examples on Stack Overflow or Google to help you make HTTP requests to the two web services you previous described.

查看更多
欢心
4楼-- · 2020-06-30 03:25

For DreamFactory, see here.

If you have a table named roles in the local MySQL db your URL for a GET should be /rest/db/roles.

查看更多
登录 后发表回答