Mercurial > p > roundup > code
comparison roundup/rest.py @ 5590:4d8746c73fdb REST-rebased
Change the way core function is called
Change the return header to allow all methods
committer: Ralf Schlatterbeck <rsc@runtux.com>
| author | Chau Nguyen <dangchau1991@yahoo.com> |
|---|---|
| date | Wed, 30 Jan 2019 10:26:35 +0100 |
| parents | 5a2de4c19109 |
| children | a25d79e874cb |
comparison
equal
deleted
inserted
replaced
| 5589:5a2de4c19109 | 5590:4d8746c73fdb |
|---|---|
| 26 code = 404 | 26 code = 404 |
| 27 data = msg | 27 data = msg |
| 28 except Unauthorised, msg: | 28 except Unauthorised, msg: |
| 29 code = 403 | 29 code = 403 |
| 30 data = msg | 30 data = msg |
| 31 except (hyperdb.DesignatorError, UsageError), msg: | 31 except UsageError, msg: |
| 32 code = 400 | 32 code = 400 |
| 33 data = msg | 33 data = msg |
| 34 except (AttributeError, Reject), msg: | 34 except (AttributeError, Reject), msg: |
| 35 code = 405 | 35 code = 405 |
| 36 data = msg | 36 data = msg |
| 70 | 70 |
| 71 class RestfulInstance(object): | 71 class RestfulInstance(object): |
| 72 """The RestfulInstance performs REST request from the client""" | 72 """The RestfulInstance performs REST request from the client""" |
| 73 | 73 |
| 74 def __init__(self, client, db): | 74 def __init__(self, client, db): |
| 75 self.client = client # it might be unnecessary to receive the client | 75 self.client = client |
| 76 self.db = db | 76 self.db = db |
| 77 | 77 |
| 78 protocol = 'http' | 78 protocol = 'http' |
| 79 host = self.client.env['HTTP_HOST'] | 79 host = self.client.env['HTTP_HOST'] |
| 80 tracker = self.client.env['TRACKER_NAME'] | 80 tracker = self.client.env['TRACKER_NAME'] |
| 138 except hyperdb.HyperdbValueError, msg: | 138 except hyperdb.HyperdbValueError, msg: |
| 139 raise UsageError(msg) | 139 raise UsageError(msg) |
| 140 | 140 |
| 141 return prop | 141 return prop |
| 142 | 142 |
| 143 def error_obj(self, status, msg, source=None): | |
| 144 """Return an error object""" | |
| 145 self.client.response_code = status | |
| 146 result = { | |
| 147 'error': { | |
| 148 'status': status, | |
| 149 'msg': msg | |
| 150 } | |
| 151 } | |
| 152 if source is not None: | |
| 153 result['error']['source'] = source | |
| 154 | |
| 155 return result | |
| 156 | |
| 143 @_data_decorator | 157 @_data_decorator |
| 144 def get_collection(self, class_name, input): | 158 def get_collection(self, class_name, input): |
| 145 """GET resource from class URI. | 159 """GET resource from class URI. |
| 146 | 160 |
| 147 This function returns only items have View permission | 161 This function returns only items have View permission |
| 742 # priority : extension from uri (/rest/issue.json), | 756 # priority : extension from uri (/rest/issue.json), |
| 743 # header (Accept: application/json, application/xml) | 757 # header (Accept: application/json, application/xml) |
| 744 # default (application/json) | 758 # default (application/json) |
| 745 | 759 |
| 746 # format_header need a priority parser | 760 # format_header need a priority parser |
| 747 format_ext = os.path.splitext(urlparse.urlparse(uri).path)[1][1:] | 761 ext_type = os.path.splitext(urlparse.urlparse(uri).path)[1][1:] |
| 748 format_header = headers.getheader('Accept')[12:] | 762 accept_header = headers.getheader('Accept')[12:] |
| 749 format_output = format_ext or format_header or "json" | 763 data_type = ext_type or accept_header or "json" |
| 750 | 764 |
| 751 # check for pretty print | 765 # check for pretty print |
| 752 try: | 766 try: |
| 753 pretty_output = input['pretty'].value.lower() == "true" | 767 pretty_output = input['pretty'].value.lower() == "true" |
| 754 except KeyError: | 768 except KeyError: |
| 758 self.client.setHeader("Access-Control-Allow-Origin", "*") | 772 self.client.setHeader("Access-Control-Allow-Origin", "*") |
| 759 self.client.setHeader( | 773 self.client.setHeader( |
| 760 "Access-Control-Allow-Headers", | 774 "Access-Control-Allow-Headers", |
| 761 "Content-Type, Authorization, X-HTTP-Method-Override" | 775 "Content-Type, Authorization, X-HTTP-Method-Override" |
| 762 ) | 776 ) |
| 763 if resource_uri in self.db.classes: | 777 self.client.setHeader( |
| 764 self.client.setHeader( | 778 "Allow", |
| 765 "Allow", | 779 "HEAD, OPTIONS, GET, POST, PUT, DELETE, PATCH" |
| 766 "HEAD, OPTIONS, GET, POST, DELETE" | 780 ) |
| 767 ) | 781 self.client.setHeader( |
| 768 self.client.setHeader( | 782 "Access-Control-Allow-Methods", |
| 769 "Access-Control-Allow-Methods", | 783 "HEAD, OPTIONS, GET, PUT, DELETE, PATCH" |
| 770 "HEAD, OPTIONS, GET, POST, DELETE" | 784 ) |
| 771 ) | 785 try: |
| 786 class_name, item_id = hyperdb.splitDesignator(resource_uri) | |
| 787 except hyperdb.DesignatorError, msg: | |
| 788 class_name = resource_uri | |
| 789 item_id = None | |
| 790 | |
| 791 # Call the appropriate method | |
| 792 if (class_name not in self.db.classes) or (len(uri_split) > 3): | |
| 793 output = self.error_obj(404, "Not found") | |
| 794 elif item_id is None: | |
| 795 if len(uri_split) == 2: | |
| 796 output = getattr( | |
| 797 self, "%s_collection" % method.lower() | |
| 798 )(class_name, input) | |
| 772 else: | 799 else: |
| 773 self.client.setHeader( | 800 if len(uri_split) == 2: |
| 774 "Allow", | 801 output = getattr( |
| 775 "HEAD, OPTIONS, GET, PUT, DELETE, PATCH" | 802 self, "%s_element" % method.lower() |
| 776 ) | 803 )(class_name, item_id, input) |
| 777 self.client.setHeader( | 804 else: |
| 778 "Access-Control-Allow-Methods", | |
| 779 "HEAD, OPTIONS, GET, PUT, DELETE, PATCH" | |
| 780 ) | |
| 781 | |
| 782 # Call the appropriate method | |
| 783 output = None | |
| 784 if resource_uri in self.db.classes: | |
| 785 output = getattr( | |
| 786 self, "%s_collection" % method.lower() | |
| 787 )(resource_uri, input) | |
| 788 else: | |
| 789 class_name, item_id = hyperdb.splitDesignator(resource_uri) | |
| 790 if len(uri_split) == 3: | |
| 791 output = getattr( | 805 output = getattr( |
| 792 self, "%s_attribute" % method.lower() | 806 self, "%s_attribute" % method.lower() |
| 793 )(class_name, item_id, uri_split[2], input) | 807 )(class_name, item_id, uri_split[2], input) |
| 794 else: | 808 |
| 795 output = getattr( | 809 # Format the content type |
| 796 self, "%s_element" % method.lower() | |
| 797 )(class_name, item_id, input) | |
| 798 | |
| 799 if format_output.lower() == "json": | 810 if format_output.lower() == "json": |
| 800 self.client.setHeader("Content-Type", "application/json") | 811 self.client.setHeader("Content-Type", "application/json") |
| 801 if pretty_output: | 812 if pretty_output: |
| 802 indent = 4 | 813 indent = 4 |
| 803 else: | 814 else: |
