<commit_before>#!/usr/bin/env python

try:
    import json
except:
    import simplejson as json
import uuid
import logging
import barrister
from bottle import post, request, response, run

#############################

# Define some constants for our error codes
ERR_INVALID = 100
ERR_DENIED  = 101
ERR_LIMIT   = 102

class ContactService(object):

    def __init__(self):
        self.contacts = { }

    def put(self, contact):
        contactId = self._get_or_create_id(contact, "contactId")
        if not self.contacts.has_key(contactId):
            userId = contact["userId"]
            if len(self.getAll(userId)) >= 10:
                # This RpcException will be automatically marshaled and re-thrown
                # on the client side
                msg = "User %s is at the 10 contact limit" % userId
                raise barrister.RpcException(ERR_LIMIT, msg)
        self.contacts[contactId] = contact
        return contactId

    def get(self, contactId, userId):
        if self.contacts.has_key(contactId):
            c = self.contacts[contactId]
            if c["userId"] != userId:
                msg = "userId %s doesn't own contact" % userId
                raise barrister.RpcException(ERR_DENIED, msg)
            else:
                return c
        else:
            return None

    def getAll(self, userId):
        arr = [ ]
        for cid, c in self.contacts.items():
            if c["userId"] == userId:
                arr.append(c)
        return arr

    def delete(self, contactId, userId):
        if self.get(contactId, userId) != None:
            del self.contacts[contactId]
            return True
        else:
            return False

    def _get_or_create_id(self, d, key):
        if not d.has_key(key) or d[key] == "":
            d[key] = uuid.uuid4().hex
        return d[key]

#############################

#
# Our single URL handler for this interface
#
@post('/contact')
def contact():
    # Read the raw POST data and deserialize the JSON
    req = json.loads(request.body.read())

    # Hand the request to the Barrister Server object
    # It will call the correct function on the ContactService
    resp = server.call(req)

    # Set the MIME type, and serialize the response to JSON
    response.content_type = 'application/json'
    return json.dumps(resp)

if __name__ == '__main__':
    # initialize the Python logger. totally optional.
    logging.basicConfig()

    # load the IDL JSON and create a barrister.Contract object
    contract = barrister.contract_from_file("contact.json")

    # create a Server that will dispatch requests for this Contract
    server = barrister.Server(contract)

    # register our class with the "ContactService" interface
    server.add_handler("ContactService", ContactService())

    # Starts the Bottle app
    run(host='localhost', port=7186)<commit_msg>use server.call_json() in example app<commit_after>#!/usr/bin/env python

import uuid
import logging
import barrister
from bottle import post, request, response, run

#############################

# Define some constants for our error codes
ERR_INVALID = 100
ERR_DENIED  = 101
ERR_LIMIT   = 102

class ContactService(object):

    def __init__(self):
        self.contacts = { }

    def put(self, contact):
        contactId = self._get_or_create_id(contact, "contactId")
        if not self.contacts.has_key(contactId):
            userId = contact["userId"]
            if len(self.getAll(userId)) >= 10:
                # This RpcException will be automatically marshaled and re-thrown
                # on the client side
                msg = "User %s is at the 10 contact limit" % userId
                raise barrister.RpcException(ERR_LIMIT, msg)
        self.contacts[contactId] = contact
        return contactId

    def get(self, contactId, userId):
        if self.contacts.has_key(contactId):
            c = self.contacts[contactId]
            if c["userId"] != userId:
                msg = "userId %s doesn't own contact" % userId
                raise barrister.RpcException(ERR_DENIED, msg)
            else:
                return c
        else:
            return None

    def getAll(self, userId):
        arr = [ ]
        for cid, c in self.contacts.items():
            if c["userId"] == userId:
                arr.append(c)
        return arr

    def delete(self, contactId, userId):
        if self.get(contactId, userId) != None:
            del self.contacts[contactId]
            return True
        else:
            return False

    def _get_or_create_id(self, d, key):
        if not d.has_key(key) or d[key] == "":
            d[key] = uuid.uuid4().hex
        return d[key]

#############################

#
# Our single URL handler for this interface
#
@post('/contact')
def contact():
    # Read the raw POST data 
    data = request.body.read()

    # Hand the request to the Barrister Server object
    # It will call the correct function on the ContactService
    resp_json = server.call_json(data)

    # Set the MIME type, and return the response
    response.content_type = 'application/json'
    return resp_json

if __name__ == '__main__':
    # initialize the Python logger. totally optional.
    logging.basicConfig()

    # load the IDL JSON and create a barrister.Contract object
    contract = barrister.contract_from_file("contact.json")

    # create a Server that will dispatch requests for this Contract
    server = barrister.Server(contract)

    # register our class with the "ContactService" interface
    server.add_handler("ContactService", ContactService())

    # Starts the Bottle app
    run(host='localhost', port=7186)
{'fraction_non_alphanumeric': 0.10754017305315204, 'fraction_numerical': 0.006357054564718347, 'mean_word_length': 2.4099939795304035, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7027553', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1582, 'n_words': 501}