API request in rhino python

I’m trying to build a rhino model based on a json data from an API request.
iron python does not have the requests library installed, so I get the error message: Message: ‘No module named requests’

Is there another way to acces external API’s or a way to install libraries into iron python?

I’m using rhino 5 on macos

Which specific API are you trying to request from?

You can always add PY libraries to the search Importing Python 2.7 library modules to Rhino

and this:

What protocol does the APi use?

Of course the JSON module will help interpret the data. once you receive it.

I’m trying to request from my own server, a REST http API request that returns json data that I’ll use for building geometry.

I got this far:

  • added request library to the scripts folder
  • now another library is asked for: added urllib3 to the scripts folder too
  • I get an error message when loading my python script that imports the request library:

Message: ‘module’ object has no attribute ‘_getframe’

Traceback:
line 692, in exec_, “/Users/joostbarendregt/Library/Application Support/McNeel/Rhinoceros/scripts/urllib3/packages/six.py”
line 701, in , “/Users/joostbarendregt/Library/Application Support/McNeel/Rhinoceros/scripts/urllib3/packages/six.py”
line 2, in , “/Users/joostbarendregt/Library/Application Support/McNeel/Rhinoceros/scripts/urllib3/exceptions.py”
line 11, in , “/Users/joostbarendregt/Library/Application Support/McNeel/Rhinoceros/scripts/urllib3/connectionpool.py”
line 8, in , “/Users/joostbarendregt/Library/Application Support/McNeel/Rhinoceros/scripts/urllib3/init.py”
line 43, in , “/Users/joostbarendregt/Library/Application Support/McNeel/Rhinoceros/scripts/requests/init.py”
line 2, in , “/Users/joostbarendregt/GIT_BB/craftr_rhino/design.py”

line 692 that it’s referring to is this line:

frame = sys._getframe(1)

Tried in both rhino5 and rhino WIP

You seem to be running into this problem:

Rhino.Python runs on IronPython 2.7.5.

But is there another way to make these requests?

Have you tried the UrlLib? It is a standard module:

import urllib
file = urllib.urlopen('http://www.google.com')
print file.read()

If the data is Json, then you may be able to decode it using the JSON lib:

import json
datastore = json.loads(file.read)

Here is an example:

def readrequest():
    file = urllib.urlopen('http://feeds.citibikenyc.com/stations/stations.json')
    recieved_data =file.read()
    file.close
    datastore = json.loads(recieved_data)
    print datastore['stationBeanList'][1]

Or for even more fun:

def readrequest():
    file = urllib.urlopen('http://feeds.citibikenyc.com/stations/stations.json')
    recieved_data =file.read()
    file.close
    datastore = json.loads(recieved_data)
    for item in datastore['stationBeanList']:
        print 'The station at {} has {} available bikes.'.format(item.get('stAddress1'), item.get('availableBikes'))

Does that help?

3 Likes

urllib was not enough to make the request I wanted, but urllib2 helped out:

def requestAPIData(url, jsonRequestData):
    request = urllib2.Request(url)
    request.add_data(json.dumps(jsonRequestData))
    request.add_header("Content-type", "application/json")
    request.add_header("Accept", "application/json")

    r = urllib2.urlopen(request)
    geometryJson = json.load(r)
    return geometryJson

thanks for the help!

2 Likes

Can I ask what API service you are using that required using the request object?

I have a similar problem. I wanna send a Dictionary to my Server and receive the results. Unfortunately the requests module is not available …can you please help me with a Workaround Scott?

Best Regards, Sebastian

Grasshopper_AM.gh (6.8 KB)

An easier approach might be using a JSON plugin for Grasshopper? Would this work? https://www.food4rhino.com/app/jswan

Hey guys,
We faced the same problems and it was not easy to get this done due to the limitations of Iron py. We finally solved it.
This piece of code is now open, feel free to use it.

IronPythonHttpRequest.py

4 Likes