rest - How to handle python objects built from a web API that have references to each other? -


i'm building client library web api exposes objects this:

# objs/foo/obj_id_1 {id: "foo_id_1"  name: "your momma"  bar: "bar_id_2"}  # objs/bar/obj_id_1 {id: "bar_id_2"  name: "puh-lease"  foo: "foo_id_1"} 

so, objects of type foo have reference objects of type bar , vice versa.

in python client library, build python objects out of api data. foo instance have bar attribute containing bar instance instead of id of bar , bar instance has reference foo instance in foo attribute.

my python classes have save , refresh methods post or web api, , sub-objects. example, if call a_foo.refresh(), automatically call a_foo.bar.refresh().

the above simplified example, , there might many different classes referring same instance of bar or foo or of many other types of objects get.

i think question 2 questions:

  1. what design or strategy ensure when build object api data of references other objects point same object if i've built object previous api requests?
  2. when call save or refresh what's design or strategy prevent infinite loop when 2 or more objects refer each other?

this pretty broad question.

1) 1 way use factory method pattern. example:

def get_foo(_id):     get_foo.foos = dict()     def real_get_foo(_id):         # foo api         get_foo.foos[_id] = foo         return foo     if _id in get_foo.foos:         return get_foo.foos[_id]     return real_get_foo(_id) 

2) don't think it's idea make nested saves. if write foo.bar.x = 5 followed foo.save() wouldn't expect bar saved. why? because called save() on foo, , shouldn't have worry unwanted saves on related objects.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -