stackvana / hook.io

Open-Source Microservice Hosting Platform
https://hook.io
Other
1.26k stars 117 forks source link

Datastore access in Python? #272

Open rw950431 opened 7 years ago

rw950431 commented 7 years ago

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

Marak commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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()
pyhedgehog commented 7 years ago

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

rw950431 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 )

rw950431 commented 7 years ago

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

pyhedgehog commented 7 years ago

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.