Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Retrieve data from a site having its API
Started by robin2000milan Mar 08 2018 02:33 AM

- - - - -
4 replies to this topic
api coronasdk webservice
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

robin2000milan

[GLOBAL: userInfoPane.html]
robin2000milan
  • Observer

  • 4 posts
  • Corona SDK

Hello,

i'm building an app for a company and i need some content that are on their site (http://www.pnitaly.com/content/7-primrose). I got the access to the API of this site, so i have an username and a password to login on http:///www.pnitaly.com/api.

Now i need to retrieve data from this and put them on my app, but i'm not able to do it. Can someone give me an example of how i can retrieve something using the API of a site, and explain me how to do it exactly.

Sorry for my bad english.

 

Robin



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,925 posts
  • Enterprise

Hi Robin. The question may seem specific, but it's really quite generic, so I'll try to provide you some answers in general terms.

 

First, there isn't enough known by us to know how the API works. Generally speaking most web API's are either in something known as SOAP or something know as REST or RESTful.  I don't know if anyone has successfully gotten a Corona app working with a SOAP based system. The authentication requires a lot of encryption and such that's difficult to accomplish. That doesn't mean it's possible, I'm just unaware of any one. SOAP is one of the least used setups. Most people are doing REST based API's these days.

 

Corona works great with REST unless there is some really unorthodox authentication schemes. You would use the Corona network.request() API to make a call to your API using HTTP GET, POST, PUT, etc. methods and provide a listener function that will watch for errors as well as the returned data.

 

REST api's generally return data in either XML or JSON format. Without knowing the API, I can't speculate, but frequently they can return either based on the HTTP request you make to the API. How you control this will vary greatly from REST api to another. If you have the option to work with JSON data that will be the best since it's data format is very similar to Lua tables. You don't need to know JSON to use JSON. We offer a json.decode() API that you can pass a string of JSON formatted table to and you get a Lua table back and then you get direct access to the data.  If you're limited to XML, you will have to search the Internet for a Lua XML parser. We have one included in our Business App sample (https://github.com/coronalabs-samples/business-app-sample) that you can use as well, though we don't have formal support for it.  With XML you end up with a table too, but it has weird formats. XML allows a key to have both key-value pairs as well as the data with the XML tag such as:

<person age=30, height=72, weight=180, address = "101 East North Street">John Smith</person>

 

instead of JSON's simple key = value pairs. So let's hope you have JSON as an option, it will make your life easier.

 

You can study another Corona sample app: CoronaWeather (https://github.com/coronalabs-samples/CoronaWeather) which uses a simple HTTP GET request to fetch weather data.

 

See: https://docs.coronalabs.com/api/library/network/request.html for usage on network.request().

 

Beyond this, any one helping you will need to know a lot more about the API, how you authenticate to it (looks like Basic HTTP authentication to me)

 

Rob



[TOPIC: post.html]
#3

robin2000milan

[GLOBAL: userInfoPane.html]
robin2000milan
  • Observer

  • 4 posts
  • Corona SDK

Thank you for your answer Rob.

I tried to analyze the app you made, CoronaWeather and i understand a few things, but i'm feeling lost yet :unsure:

The API i have is in XML format, i attach to this reply the screenshot of them, to make you understand.

If i want to retrive data from this API that i attached, what should i do? can you make me an example? I'll be very grateful if you can help me with an example of main.lua file in which you call this API. I'm desperate, this is for a school project and i need to finish this as fast as i can.

Thank you.

 

Robin

Attached Thumbnails

  • domanda.PNG


[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,925 posts
  • Enterprise

If your data is in XML, you're going to need the xml.lua file from the Business App sample. You might want to look at the rss.lua file included in there since it has example code of taking XML data and using xml.lua to turn it into a table.

 

This is where it gets tricky because XML doesn't map directly to Lua key-value style tables. You're likely going to end up with table entries named like:

yourTable.child[1].child

Once you figure out what's in each of these, then you can get them into more usable names:

 if yourTable.child[j].name == "href" then

    yourRealTable.href = item.child[j].value
end

That of course assumes you have a bunch of entries in the child table. Like I said, study the rss.lua file.  Posting a copy/paste of your API would be better than a screen shot. Also study table printing techniques which will help you map your XML file to the table the xml.lua output.

 

Rob



[TOPIC: post.html]
#5

robin2000milan

[GLOBAL: userInfoPane.html]
robin2000milan
  • Observer

  • 4 posts
  • Corona SDK

Hi,

here's a part of the XML code taken from: http://www.pnitaly.com/api

 

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<api shopName="Project Nails Italy srl">
<categories xlink:href="http://www.pnitaly.com/api/categories" get="true" put="false" post="false" delete="false" head="true">
<description xlink:href="http://www.pnitaly.com/api/categories" get="true" put="false" post="false" delete="false" head="true">The product categories</description>
<schema xlink:href="http://www.pnitaly.com/api/categories?schema=blank" type="blank"/>
<schema xlink:href="http://www.pnitaly.com/api/categories?schema=synopsis" type="synopsis"/>
</categories>
<combinations xlink:href="http://www.pnitaly.com/api/combinations" get="true" put="false" post="false" delete="false" head="true">
<description xlink:href="http://www.pnitaly.com/api/combinations" get="true" put="false" post="false" delete="false" head="true">The product combinations</description>
<schema xlink:href="http://www.pnitaly.com/api/combinations?schema=blank" type="blank"/>
<schema xlink:href="http://www.pnitaly.com/api/combinations?schema=synopsis" type="synopsis"/>
</combinations>
<countries xlink:href="http://www.pnitaly.com/api/countries" get="true" put="false" post="false" delete="false" head="true">
<description xlink:href="http://www.pnitaly.com/api/countries" get="true" put="false" post="false" delete="false" head="true">The countries</description>
<schema xlink:href="http://www.pnitaly.com/api/countries?schema=blank" type="blank"/>
<schema xlink:href="http://www.pnitaly.com/api/countries?schema=synopsis" type="synopsis"/>
</countries>
<currencies xlink:href="http://www.pnitaly.com/api/currencies" get="true" put="false" post="false" delete="false" head="true">
<description xlink:href="http://www.pnitaly.com/api/currencies" get="true" put="false" post="false" delete="false" head="true">The currencies</description>
<schema xlink:href="http://www.pnitaly.com/api/currencies?schema=blank" type="blank"/>
<schema xlink:href="http://www.pnitaly.com/api/currencies?schema=synopsis" type="synopsis"/>
</currencies>
 
 
Thanks.

 




[topic_controls]
[/topic_controls]