Jump to content
Developer Wiki and Function Reference Links ×

JSON rest api calls in vectorscript


Recommended Posts

HI everyone,

 

I've come across multiple marionette scripts that use rest api to retrieve json information. The caveat - they use python, that require additional libraries to use the GET function.

my question - can vectorscript do something similar without additional libraries? I am looking to use an API to extract records from a remote web based json api. As it appears, vw script can open a web browser using the showwebdlg function. I was looking to see if I can pull records from json using vectorscript.

 

Thank you in advance!! 

 

here is an example from 

 

@Marionette.NodeDefinition
class Params(metaclass = Marionette.OrderedClass):
#APPEARANCE
    #Name
    this = Marionette.Node( 'Pass' )
    this.SetDescription( 'Do nothing node. It just passes the input to the output: y = x' )

    #Input Ports
    x = Marionette.PortIn( [], 'item' )
    x.SetDescription( "input" )

    #OIP Controls

    #Output Ports
    y = Marionette.PortOut('item')   
    y.SetDescription( "output" )

#BEHAVIOR
    #this.SetListAbsorb()
    
def RunNode(self):
    import requests
    
    #script
    api_url = "https://jsonplaceholder.typicode.com/todos/1"
    response = requests.get(api_url)
    vs.Message(str(response.json()))
    x = 0

    #outputs
    self.Params.y.value = x

 

Edited by Samuel Derenboim
Link to comment

The forum allows me not to paste some code ... I think maybe for security reasons.
 

Or your testing api request:

import urllib.request
import json


api_url = "https://jsonplaceholder.typicode.com/todos/1"

result_string = ''
with urllib.request.urlopen(api_url) as f:
   result =  json.loads(f.read().decode('utf-8'))


vs.AlrtDialog(str(result))

 

An other real example with a http request url, if you can't get https to work

 

import urllib.request
import json
import math


#dezimalgrad in minuten und Sekunden umwandeln (copy paste aus dem Netz)
def deg_to_dms(deg, type='lat'):
	deg = float(deg)
	decimals, number = math.modf(deg)
	
	d = int(number)
	m = int(decimals * 60)
	s = (deg - d - m / 60) * 3600.00
	compass = {
		'lat': ('N','S'),
		'lon': ('E','W')
	}
	compass_str = compass[type][0 if d >= 0 else 1]
	return '{}º{}\'{:.2f}"{}'.format(abs(d), abs(m), abs(s), compass_str)




h = vs.FSActLayer()
x = 2600000
y = 1200000
z = 500
#Nimmt Position von aktiviertem Hilspunkt, Objekt oder Symbol sonst Bern Sternwarte als Vorgabewert
if h != vs.Handle(0):
	x,y,z = vs.GetLocus3D(h)


#vs.AlrtDialog(str(vs.GeogCoordToVWN(x, y)))
vs.AlrtDialog(str(vs.VWCoordToGeog(x, y)))



xPt, yPt, zPt = vs.PtDialog3D('LV95 Koordikante zu WGS84', x, y, z)

#Abfragestring auf dem Geportal direkt im Internet
url = "http://geodesy.geo.admin.ch/reframe/lv95towgs84?easting="+str(xPt)+"&northing="+str(yPt)+"&altitude="+str(zPt)+"&format=json"


result_string = ''
with urllib.request.urlopen(url) as f:
   result =  json.loads(f.read().decode('utf-8'))


#Antwort vom Geoportal interpretieren und umwandeln in json
result_string = str(result['easting']) + '\r' + str(result['northing']) + '\r' + str(result['altitude'])+'\r\r'\
'Grad: '

result_string += '\r'
result_string += 'Grad Minuten Sekunden: \r' + deg_to_dms(result['easting'], 'lat') +'  ' + deg_to_dms(result['easting'], 'lon')

vs.AlrtDialog(str(result_string))

 

Edited by DomC
Link to comment

 OK, the part of the code which I will not paste is the part how you update your certificates by the script on mac computers to make python accept them to communicate over TLS (SSL) (https instead of http).

caution should be exercised. 

Edited by DomC
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...