deontologician / restnavigator

A python library for interacting with HAL+JSON APIs
MIT License
43 stars 15 forks source link

Custom Hooks #13

Open deontologician opened 10 years ago

deontologician commented 10 years ago

I've been looking at adding custom hooks for a while, so custom behaviors can be handled by consumers of rest_navigator without needing to subclass etc. HALNavigator isn't designed especially well for being extended by subclasses, and I don't like subclassing by external consumers anyway, it requires too much knowledge of the guts of the class.

Another principle is to make the hook system simple/limited at first, it can always get more capabilities later, and if someone really really needs an out, you can always subclass.

N = HALNavigator('https://example.com/api')
N.hook(rel='xx:hams').state = change_state_callback
N.hook(profile='some_profile').links = change_links_callback
N.hook(rel='xx:hams', name='barry').headers = change_session_headers_callback
N.hook(state=lambda s: s['foo']['bar'] == 12).state = change_state_callback
N.hook(header=('content-type', 'application/hal+json')).state = 

There will need to be a new class, with a few properties:

Hook
  state
  links
  headers
  side_effect

The Hook is created and registered by the .hook method on Navigators That method also returns the new Hook object.

my_hook = N.hook(...)
my_hook.side_effect = ...
my_hook.state = ...
... # sometime later
my_hook.unregister() # removes it from the navigator
# or alternately:
N.unregister_hook(my_hook)

The properties of the Hook class are:

The hook method on HALNavigator has several keyword arguments (they can't be passed positionally):

def hook(self, **kwargs):

For each of these, the argument can either take a literal value to match, or it can take a function. The function receives the corresponding property and should return True or False to determine if the property matches

N.hook(method='GET')

Other notes:

This is a rough draft, and basically has my thoughts all out of order. @dudycooly, I'd appreciate any comments you have

deontologician commented 10 years ago

@dudycooly I made you a collaborator on this repository, so if you feel like working on this over the next two weeks have at it. I'm going to have limited internet until the 14th, and will be working on #7 (embedded support) offline. If you end up working on hooks, just assign yourself and merge whatever you think is good enough into next. I can probably comment from time to time when I get internet access.

dudycooly commented 10 years ago

@deontologician Unable to work on this project for last two days. On Hook, you already have a detailed proposal and looks great. This will be handy for any Advance users. I had a very simple case in my mind initially and have started working on it. Once you are back, will go through in detail

On 4 November 2014 01:21, Josh Kuhn notifications@github.com wrote:

@dudycooly https://github.com/dudycooly I made you a collaborator on this repository, so if you feel like working on this over the next two weeks have at it. I'm going to have limited internet until the 14th, and will be working on #7 https://github.com/deontologician/rest_navigator/issues/7 (embedded support) offline. If you end up working on hooks, just assign yourself and merge whatever you think is good enough into next. I can probably comment from time to time when I get internet access.

— Reply to this email directly or view it on GitHub https://github.com/deontologician/rest_navigator/issues/13#issuecomment-61579171 .

dudycooly commented 10 years ago

@deontologician I am just wondering when you are planning to merge "next" branch to master? CREATE response fix went in master today may be over-written by this merge. just heads up

On 6 November 2014 00:09, Jp jay.naveen@gmail.com wrote:

@deontologician Unable to work on this project for last two days. On Hook, you already have a detailed proposal and looks great. This will be handy for any Advance users. I had a very simple case in my mind initially and have started working on it. Once you are back, will go through in detail

On 4 November 2014 01:21, Josh Kuhn notifications@github.com wrote:

@dudycooly https://github.com/dudycooly I made you a collaborator on this repository, so if you feel like working on this over the next two weeks have at it. I'm going to have limited internet until the 14th, and will be working on #7 https://github.com/deontologician/rest_navigator/issues/7 (embedded support) offline. If you end up working on hooks, just assign yourself and merge whatever you think is good enough into next. I can probably comment from time to time when I get internet access.

— Reply to this email directly or view it on GitHub https://github.com/deontologician/rest_navigator/issues/13#issuecomment-61579171 .

deontologician commented 10 years ago

Basically, I was going to merge next when it was ready for 0.3. I think hooks and embedded support should get it there On Nov 7, 2014 7:49 AM, "dudycooly" notifications@github.com wrote:

@deontologician I am just wondering when you are planning to merge "next" branch to master? CREATE response fix went in master today may be over-written by this merge. just heads up

On 6 November 2014 00:09, Jp jay.naveen@gmail.com wrote:

@deontologician Unable to work on this project for last two days. On Hook, you already have a detailed proposal and looks great. This will be handy for any Advance users. I had a very simple case in my mind initially and have started working on it. Once you are back, will go through in detail

On 4 November 2014 01:21, Josh Kuhn notifications@github.com wrote:

@dudycooly https://github.com/dudycooly I made you a collaborator on this repository, so if you feel like working on this over the next two weeks have at it. I'm going to have limited internet until the 14th, and will be working on #7 https://github.com/deontologician/rest_navigator/issues/7 (embedded support) offline. If you end up working on hooks, just assign yourself and merge whatever you think is good enough into next. I can probably comment from time to time when I get internet access.

— Reply to this email directly or view it on GitHub < https://github.com/deontologician/rest_navigator/issues/13#issuecomment-61579171>

.

— Reply to this email directly or view it on GitHub https://github.com/deontologician/rest_navigator/issues/13#issuecomment-62066075 .

deontologician commented 9 years ago

@dudycooly I am finally back from vacation. Working on the embedded stuff still. Let me know if you want to discuss anything

dudycooly commented 9 years ago

@deontologician - Hope you had a good vacation. Let us catch up tonight

On 1 December 2014 at 02:07, Josh Kuhn notifications@github.com wrote:

@dudycooly https://github.com/dudycooly I am finally back from vacation. Working on the embedded stuff still. Let me know if you want to discuss anything

— Reply to this email directly or view it on GitHub https://github.com/deontologician/rest_navigator/issues/13#issuecomment-65012653 .

dudycooly commented 9 years ago

@deontologician - Pls ping if you are online

deontologician commented 9 years ago

@dudycooly sorry I missed your message. I don't have your email, so just ping me when you get on if you can