Web API extension

49 views
Skip to first unread message

Michael Zhilin

unread,
May 6, 2013, 10:45:02 AM5/6/13
to reviewb...@googlegroups.com
Hi,

I want to write custom Web API as extension. Is it possible? Do anybody have sample?

Thank you in advance,
 Michael 

Steven MacLeod

unread,
May 6, 2013, 10:53:31 AM5/6/13
to reviewb...@googlegroups.com
Hi Michael,

It is possible to extend the Web API. The only public extension I know of that does this is Review Bot ( https://github.com/smacleod/ReviewBot ).

The way you make this work is by writing your new resource classes which inherit from WebAPIResource, and then telling your Extension class about them. The new resources will live under you extension in the Web API ( e.g. 'http://.../api/extensions/<extension-identifier>/<new-resource>/' )

You can look at https://github.com/smacleod/ReviewBot/blob/master/extension/reviewbotext/resources.py for some example resources Review Bot defines, and then https://github.com/smacleod/ReviewBot/blob/master/extension/reviewbotext/extension.py for how to tell the extension about your resources.

Notice the 'resources' attribute on the class:

resources =
    [
        review_bot_review_resource,
        review_bot_tool_resource,
    ]


--
 
---
You received this message because you are subscribed to the Google Groups "reviewboard-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reviewboard-d...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Zhilin

unread,
May 7, 2013, 9:39:24 AM5/7/13
to reviewb...@googlegroups.com
Thanks, Steven!

The only unclear thing is JSON serialization of results. I've implemented POST "create" method and want to return new object with parameters. I've got following:
/home/reviewer/rb01/lib/python2.6/site-packages/Djblets-0.7.13-py2.6.egg/djblets/webapi/core.py in default
  1.             raise TypeError("%r is not JSON serializable" % (o,))
    ...
VariableValue
self
<djblets.webapi.core.JSONEncoderAdapter object at 0x7fd13f32d610>
result
None
o
<RepositoryName: RepositoryName object>

The code is very simple:

   def create(self, request, name, repo, automatic=False, *args, **kwargs):
        
        repository = Repository.objects.get(id=repo)
        
        new_repo, created = RepositoryName.objects.get_or_create(
            name=name            
            )
        new_repo.repo=repository
        new_repo.diana=automatic
        
        new_repo.save()
        
        return 201, {
            self.item_result_key: new_repo
        }

What should I write to make model serializable?

Thank you in advance,
 Michael
Reply all
Reply to author
Forward
0 new messages