[Solved] Get data from REST API

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
jza
Posts: 239
Joined: Mon Nov 03, 2008 11:33 am
Location: Cancún, Mexico

[Solved] Get data from REST API

Post by jza »

Hi I would like to know if there is a script that uses REST API from a website for Live Data. So I got python script to do it on the CLI:

Code: Select all

import sys,requests

getme= requests.get("https://api.vaultoro.com/latest")
quote=float(getme.text)
print calc
>>> '0.07696479'
Of course a simple curl on bash can return that:

Code: Select all

 $ curl https://api.vaultoro.com/latest
0.07696479
What I want is to have a macro that will do this request and return the value into a cell. Also some aditional information for possible parameters when trying to do post. I might want to keep it portable so an OOoBasic would be fine.

regards.
Last edited by jza on Sun Dec 27, 2015 6:40 am, edited 2 times in total.
AOO 4.1.1 on Arch Linux
User avatar
karolus
Volunteer
Posts: 1242
Joined: Sat Jul 02, 2011 9:47 am

Re: Get data from REST API

Post by karolus »

Hallo

http://python-requests.org

Code: Select all

import requests

def get_value():
    
    doc = XSCRIPTCONTEXT.getDocument()
    sel = doc.CurrentSelection    
    url = "https://api.vaultoro.com/latest"
    res = requests.get(url)
    sel.Value = res.json()
 
Karolus
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
User avatar
jza
Posts: 239
Joined: Mon Nov 03, 2008 11:33 am
Location: Cancún, Mexico

Re: Get data from REST API

Post by jza »

Sorry I should mention that I wanted this for Basic, not Python. I want to make this portable without the next user to have to run the system python (windows users). The AOO python also doesnt include requests and many end users will be intimidated asking them, oh btw you need to manually install requests on your AOO python-path.
AOO 4.1.1 on Arch Linux
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Get data from REST API

Post by B Marcelly »

Hi,
Solution in Basic:

Code: Select all

Function getWebService(request As String) As String
Dim sfa As Object, flux As Object, repDoc As Object
Dim repXml As String

sfa = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
repDoc = CreateUnoService("com.sun.star.io.TextInputStream")
repXml = ""
On Error GoTo badRequest
flux = sfa.openFileRead(request)
repDoc.InputStream = flux
Do while not repDoc.isEOF
  repXml = repXml & " " & repDoc.readLine
Loop
flux.closeInput
repDoc.closeInput
finished:
getWebService = repXml
Exit Function

badRequest:
  Resume finished
End Function
Usage (if the answer does not need further analysis):

Code: Select all

Dim rep As String

rep = getWebService("https://api.vaultoro.com/latest")
MsgBox rep
Note : LibreOffice 4.2 has introduced the Calc function WEBSERVICE().
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
User avatar
karolus
Volunteer
Posts: 1242
Joined: Sat Jul 02, 2011 9:47 am

Re: Get data from REST API

Post by karolus »

Hallo

theres no need for third-party--requests, use the builtin-module `urllib2` instead.
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
User avatar
jza
Posts: 239
Joined: Mon Nov 03, 2008 11:33 am
Location: Cancún, Mexico

Re: Get data from REST API

Post by jza »

Basic doesnt have urllib2
AOO 4.1.1 on Arch Linux
User avatar
karolus
Volunteer
Posts: 1242
Joined: Sat Jul 02, 2011 9:47 am

Re: Get data from REST API

Post by karolus »

jza wrote:Basic doesnt have urllib2
no surprise, but python
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Post Reply