WebPy with Proper OO

WebPy with Proper OO

Postby hafnero » Sun Jun 16, 2013 11:24 pm

Hi,

I'm trying to build a python based API application to serve HTML, XML, and JSON.

Initially I found webpy http://webpy.org/ and it seemed like as a library it would meet my needs. However after some more reading, it doesn't seem to support true Object Oriented Principles.

Example:

If i wanted to create a class to handle a particular part of the application (in this case an online Crypto Currency Wallet) I would have to create a new class for each type of POST I want to make, however when interacting the bitcoin client I am using a library called bitcoin-python (http://laanwj.github.io/bitcoin-python/doc/). If you review this library there are many functions which accept different parameters depending on the action you wish to carry out.

So bearing this in mind, I would normally write a Class to handle all the functions based on the URL mappings for the API. I started doing this and came across what seems to be a limitation of the webpy library, each class can only have a single GET and POST function, this makes it quite long winded to develop with as I would have to create a class for each function that I wanted to carry out using the API, instead of having a single Class which would allow for different POST variable per function. You can see an example of this on their site http://webpy.org/src/blog/0.3 specifically the following code:

Code: Select all
""" Basic blog using webpy 0.3 """
import web
import model

### Url mappings

urls = (
    '/', 'Index',
    '/view/(\d+)', 'View',
    '/new', 'New',
    '/delete/(\d+)', 'Delete',
    '/edit/(\d+)', 'Edit',
)


### Templates
t_globals = {
    'datestr': web.datestr
}
render = web.template.render('templates', base='base', globals=t_globals)


class Index:

    def GET(self):
        """ Show page """
        posts = model.get_posts()
        return render.index(posts)


class View:

    def GET(self, id):
        """ View single post """
        post = model.get_post(int(id))
        return render.view(post)


class New:

    form = web.form.Form(
        web.form.Textbox('title', web.form.notnull,
            size=30,
            description="Post title:"),
        web.form.Textarea('content', web.form.notnull,
            rows=30, cols=80,
            description="Post content:"),
        web.form.Button('Post entry'),
    )

    def GET(self):
        form = self.form()
        return render.new(form)

    def POST(self):
        form = self.form()
        if not form.validates():
            return render.new(form)
        model.new_post(form.d.title, form.d.content)
        raise web.seeother('/')


class Delete:

    def POST(self, id):
        model.del_post(int(id))
        raise web.seeother('/')


class Edit:

    def GET(self, id):
        post = model.get_post(int(id))
        form = New.form()
        form.fill(post)
        return render.edit(post, form)


    def POST(self, id):
        form = New.form()
        post = model.get_post(int(id))
        if not form.validates():
            return render.edit(post, form)
        model.update_post(int(id), form.d.title, form.d.content)
        raise web.seeother('/')


app = web.application(urls, globals())

if __name__ == '__main__':
    app.run()


As you can see here each part of CRUD has its own class with its own POST/GET functions. However I am trying to build a class to handle all interaction between the API and the bitcoin client within a single class. My code so far (please forgive it, as its very early stages at this point):

Code: Select all
import web
import bitcoinrpc
import bitcoinrpc.data

urls = (
  '/', 'index',
  '/bitcoin/UserBalance/(.+)', 'bitcoin',
  '/bitcoin/SendFromUser/(.+)','bitcoin',
  '/bitcoin/ListAccounts/','bitcoin'
)

class index:
    def GET(self):
        return "Hello, world!"

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

class bitcoin:
   def POST(self):
            i = web.input()
        if i.action == "GetUserBalance":
            getUserBalance(i.bitcoinAccount)
        elif i.action == "SendFromUser":
            sendFromUser(i.Account, i.to,i.amount)
        elif i.action == "ListAccounts":
            listAccounts()
           
   def __init__(self):
            conn = bitcoinrpc.connect_to_remote('Username', 'password', host='localhost', port=8332)
       
   def getUserBalance(self, bitcoinAccount):
      balance = conn.getaccountaddress(bitcoinAccount)
      return balance
   
   def sendFromUser(self, bitcoinAccount, to, amount):
      txid = conn.sendfrom(bitcoinAccount, to, amount)
      return txid
   
   def listAccounts(self):
      accounts = conn.listaccounts()
      return accounts


Does anyone know of a way were I can keep this type of OO model with webpy maybe by using some kind of Inheritance for the POST function? Or if you can suggest a better library or framework to achieve this.

Many Thanks in Advance :)
hafnero
 
Posts: 1
Joined: Sun Jun 16, 2013 10:56 pm
Location: UK

Return to Web Development

Who is online

Users browsing this forum: No registered users and 2 guests