def do_POST(self):
        try:
            parsed_url = urlparse(self.path)

            self.path = parsed_url.path

            # initialize mime type to be application/json
            mime_type = mime_types.get('json')

            # initialize response to client
            response = ''

            # api request
            if is_api(self.path):

                api_regex_match = get_api_match(self.path)
                # extract api call from request
                api_call = api_regex_match.group(3)
                api_params = parse_qs(parsed_url.query)
                # load post data and convert to dict
                post_data = from_json(
                    self.rfile.read(
                        int(self.headers.getheader('Content-Length'))))

                if post_data['action'] == 'update':
                    response = to_json(
                        database.update(
                            api_db_mapping.mapping[api_call]['object_type'],
                            from_json(post_data['store'])))
                elif post_data['action'] == 'delete':
                    response = to_json(
                        database.delete(
                            api_db_mapping.mapping[api_call]['object_type'],
                            from_json(post_data['store'])))

            self.send_response(200)
            self.send_header('Content-type', mime_type)
            self.end_headers()

            self.wfile.write(response)

            print response

            return

        except IOError:
            self.send_error(404, 'File not found: %s' % self.path)
    def do_GET(self):

        try:
            parsed_url = urlparse(self.path)

            self.path = parsed_url.path

            # initialize mime type to be text/html
            mime_type = mime_types.get('html')

            # initialize response to client
            response = ''

            # api request
            if is_api(self.path):
                api_regex_match = get_api_match(self.path)
                # extract api call from request
                api_call = api_regex_match.group(3)
                api_params = parse_qs(parsed_url.query)
                response = to_json(
                    database.query(
                        api_db_mapping.mapping[api_call]['object_type']))
                mime_type = mime_types.get('json')

            # file request/normal request
            else:
                self.path = restaurant_cfg[
                    'WEB_ROOT'] + self.path if self.path != '/' else restaurant_cfg[
                        'WEB_ROOT'] + '/html/restaurants.html'
                # check the file extension and set the correct mime type
                # filename = os.path.splitext(self.path)[0]
                file_extension = os.path.splitext(self.path)[1][1:]
                mime_type = mime_types.get(file_extension)

                with codecs.open(self.path, 'r', encoding='utf-8') as fp:
                    response = fp.read()
                    # print response

            self.send_response(200)
            self.send_header('Content-type', mime_type)
            self.end_headers()

            self.wfile.write(response)

            return

        except IOError:
            self.send_error(404, 'File not found: %s' % self.path)
예제 #3
0
 def on_put(self, req, resp, user_id, document_id, element_id):
     data = req.media
     resp.body = helpers.to_json(
         documents.update_element(user_id, document_id, element_id, data))
예제 #4
0
 def on_get(self, req, resp, user_id, document_id):
     resp.body = helpers.to_json(
         dict(data=documents.find_elements(user_id, document_id)))
예제 #5
0
 def on_get(self, req, resp, user_id, document_id):
     resp.body = helpers.to_json(documents.get(user_id, document_id))
예제 #6
0
 def on_post(self, req, resp, user_id):
     data = req.media
     resp.body = helpers.to_json(documents.create(user_id, data))
예제 #7
0
 def on_get(self, req, resp, user_id):
     data = documents.find(user_id, **req.params)
     resp.body = helpers.to_json(dict(data=data))
예제 #8
0
파일: auth.py 프로젝트: benthepoet/ek512
 def on_post(self, req, resp):
     data = req.media
     user, token = security.authenticate(**data)
     resp.body = helpers.to_json(dict(user, token=token.decode()))
def get_all_menu_items(object_kind):
    # jsonify doesn't work with lists
    print 'inside get_all_menu_items'
    response = database.query_all(
        api_db_mapping.mapping[object_kind]['object_type'])
    return to_json(response)
예제 #10
0
파일: users.py 프로젝트: benthepoet/ek512
 def on_get(self, req, resp, user_id):
     resp.body = helpers.to_json(security.get_user(user_id))