Getting Started Using the API in Python


#1

hello! I’ve just started playing with speckle today and have run into some problems trying to use the API in python. I’m not too familiar with this so I may be missing something obvious, but my call doesn’t seem to pass on the parameters. this is how I’ve set it up:

import requests
import simplejson as json

headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'my-key'
}

params = {
    'name': 'izzy',
    'email': 'my@email.com',
}

r = requests.post('https://hestia.speckle.works/api/accounts/search', params = params, headers = headers)

These are some of the responses I get:

search:  {'success': False, 'message': 'no search criteria present, or too short search string (must be > 2).'}
login:  {'success': False, 'message': 'Invalid credentials.'}
register:  {'success': False, 'message': 'Do not fuck with us. Give us your email.'}

(lol at the last one)

I’m able to get and create, but I just can’t pass my parameters. if someone could please point out to me where I’m going wrong, I’d really appreciate it!

thanks!
izzy

python: 3.6.4
speckle: v1.2.6.110


#2

Hello Izzy, disclaimer: I have no knowledge of python. I’ll point you towards @TomSvilans’s work on the blender plugin, which uses python and he’s got quite a bit of the speckle api calls set up in a separate speckle python library.

Hope this helps!


As a little extra, you could just register on the test server hestia and get your api token from your profile page - you can use this thereafter straight away in your authorization headers.

As well, if on windows, try using fiddler to debug your requests; you can see exactly what headers you’re sending, any body params, etc.


#3

hi Dimitrie, thanks for the link to the python lib! I’ll have a look through and see if I can work it out.

I’m on hestia and am using my actual API token in my script, I just didn’t want to post it on here for fear that someone would have a bit too much fun with it haha

I’ll def try out fiddler; thanks for the recommendation!


#4

Hi Izzy,

Take a look at https://github.com/speckleworks/PySpeckle and - once you have installed it - take a look at the implemented API calls in SpeckleClient.py. It should be enough to get you started, but do tell if you run into issues…


#5

hi Tom,

thanks a lot for the help; I’m mostly up and running! I have no problems reading or deleting, however I did run into one problem trying to create a stream. I’ve set it up as so:

speck = speckle.SpeckleApiClient()
speck.set_profile(creds['server'],creds['apitoken'])
speck.StreamCreateAsync('python stream test!')

return:

AttributeError: 'SpeckleApiClient' object has no attribute 'baseUrl'

I’m thinking it might just be a typo in the SpeckleClient.py; the other calls work fine with url = self.server while the StreamCreateAsync def defines the url as self.baseURL?

other than that though, it’s all working smoothly. thanks again!


#6

Hi @izzylys, yes, nice catch. That must be a typo left over from the rewrite. It’s fixed and uploaded.

Either pull the latest version from Github or do a pip upgrade to v0.2.6.

Happy to hear it’s working otherwise!


#7

thanks for the update @TomSvilans!
sorry just one more thing I should have also noticed, but I think the return for that def should be r = self.session.post() rather than r = session.post()? it’s currently throwing a name error:

NameError: name 'session' is not defined

#8

Yep, sloppy checking on my part. Updated.


#9

Also, @izzylys, if you’re so inclined, there are a bunch of simple API calls that need to be implemented. If you’re up to speed with Git and forking and such, have a go at implementing some if you feel that they are important, submit a pull request, etc. Most of it is copypasta from other API calls, using the .NET client as a reference for the relevant URLs and such.

I’'m just quite busy with other things at the moment so I can’t devote a huge amount of time to fleshing out the rest of the client, and any extra hands on the Python client would be really appreciated! Just an invitation :slight_smile:


#10

@TomSvilans I’m a bit of a noob, but I’m happy to help out! I’ll def have some time later this week to take a look and see what I can do :+1: