Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datastore access in Python? #272

Open
rw950431 opened this issue Feb 2, 2017 · 10 comments
Open

Datastore access in Python? #272

rw950431 opened this issue Feb 2, 2017 · 10 comments

Comments

@rw950431
Copy link

rw950431 commented Feb 2, 2017

Is the datastore function available under Python? I've tried Hook['datastore'] and Hook.datastore without success..

@Marak
Copy link
Collaborator

Marak commented Feb 2, 2017

It is available and possible, but not seamlessly integrated like other languages.

You'll have to make an HTTP request to the datastore REST API at http://hook.io/datastore. I think @pyhedgehog has done some work on this already for Python.

This is how the other languages access the datastore, through a prebuilt SDK we include which makes HTTP requests. Would be nice to get this fully integrated for Python.

@rw950431
Copy link
Author

rw950431 commented Feb 3, 2017

Thanks Marek.
I went down that path and had some success with GET- was able to get some json I had input via the web interface although I had to double decode to get the data back.

url='https://hook.io/datastore/get?key=try_it&hook_private_key=%s' % ( Hook['env']['hook_private_key'] )
adde=json.loads(json.loads(urlopen(url).read()))
pprint.pprint(adde)

However I'm stuck on how to supply the private key to my SET command when uploading JSON- anyone can help?

@Marak
Copy link
Collaborator

Marak commented Feb 3, 2017

@rw950431 Maybe try setting the hookio-private-key header? Or trying setting hook_private_key in the body of the request. It should work.

@rw950431
Copy link
Author

rw950431 commented Feb 6, 2017

Solved (to my satistaction at least). Theres probably neater and more pythonic ways to do it but I'm happy with this.
Thanks for the info Marak.

import json
from urllib2 import urlopen,Request
class Datastore(object):
  """ assumes you have a key with database permissions stored in Hook['env']['hook_private_key'] """
  def set(self,key,value):
    data=json.dumps({"key": key, "value": value, "hook_private_key":Hook['env']['hook_private_key'] })
    req=Request('https://hook.io/datastore/set',data,{'Content-Type': 'application/json'})
    #Returns "OK" or a json error message
    return json.loads(urlopen(req).read())

  def get(self,key):
    url='https://hook.io/datastore/get?key=%s&hook_private_key=%s' % (key,Hook['env']['hook_private_key'])
    return json.loads(urlopen(url).read())
    #returns None if key doesnt exist, otherwise stored object


  def delete(self,key):
    url='https://hook.io/datastore/del?key=%s&hook_private_key=%s' % (key,Hook['env']['hook_private_key'])
    #returns 1 if key was found, 0 if not
    return urlopen(url).read()

  def recent(self):
    url='https://hook.io/datastore/recent?hook_private_key=%s' % (Hook['env']['hook_private_key'])
    #returns list of recent keys
    return json.loads(urlopen(url).read())

def main():
  ds=Datastore() # create datastore object
  d={"lat":-45.00, "lon": 139.6} # sample data
  print ds.set('try3',d)
  print ds.get('try3')
  print ds.delete('try3')
  print ds.recent()


if __name__ == '__main__':
    main()

@rw950431 rw950431 closed this as completed Feb 6, 2017
@pyhedgehog
Copy link

hook.io-sdk-python supports all these and many other functions from JS hook.io-sdk.

@rw950431
Copy link
Author

rw950431 commented Feb 6, 2017

I'm trying to write code to run as a standalone service used to receive and process simple requests from a microcontroller. Perhaps I've misunderstood the SDK concept but it seemed like you needed to run it on your own endpoint device. Happy to learn more if I'm off the track here..

@pyhedgehog
Copy link

pyhedgehog commented Feb 8, 2017

What do you mean by "endpoint device"? SDK can be used either inside hook or as client on any device connected to internet to create hook, access logs, setup environment/datastore/fs, configure subdomains and so on.

@Marak
Copy link
Collaborator

Marak commented Feb 8, 2017

This is a good discussion. It's long overdue that we merge hook-io-sdk-python and other work from @pyhedgehog into the master branches.

Looking forward to getting the official python SDK out soon ( and integrated with all hook services )

@Marak Marak reopened this Feb 8, 2017
@rw950431
Copy link
Author

rw950431 commented Feb 8, 2017

@pyhedgehog - can you give a basic example of accessing the SDK inside a hook? I couldnt find any documentation about that.

@pyhedgehog
Copy link

There are one problem about this - installing (and auto-installing like with npm) of python packages not yet available. We are working on it with Marak, but don't hold your breath. There are temporary solution - use helpers/compilehook.py script to embed hookio package inside script source.

Sorry, right now I have no spare time to write complete documentation. PRs would be great. But note that I've found first attempt of implementation somewhat un-pythonic and going to separate normal, stream and raw interfaces from each other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants