|
| 1 | +/* |
| 2 | + * ****************************************************************************** |
| 3 | + * Copyright 2014-2018 Spectra Logic Corporation. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use |
| 5 | + * this file except in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. |
| 10 | + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 12 | + * specific language governing permissions and limitations under the License. |
| 13 | + * **************************************************************************** |
| 14 | + */ |
| 15 | + |
| 16 | +package com.spectralogic.ds3autogen.python.generators.response; |
| 17 | + |
| 18 | +import com.spectralogic.ds3autogen.api.models.apispec.Ds3Request; |
| 19 | + |
| 20 | +import static com.spectralogic.ds3autogen.python.helpers.PythonHelper.pythonIndent; |
| 21 | + |
| 22 | +public class HeadObjectResponseGenerator extends HeadResponseGenerator { |
| 23 | + |
| 24 | + /** |
| 25 | + * Gets the python code that will parse the response payload |
| 26 | + */ |
| 27 | + @Override |
| 28 | + public String toParseResponsePayload(final Ds3Request ds3Request) { |
| 29 | + return "if self.response.status == 200:\n" + |
| 30 | + pythonIndent(3) + "self.__process_checksum_headers(response.getheaders())\n" + |
| 31 | + pythonIndent(3) + "self.result = HeadRequestStatus.EXISTS\n" + |
| 32 | + pythonIndent(2) + "elif self.response.status == 403:\n" + |
| 33 | + pythonIndent(3) + "self.result = HeadRequestStatus.NOTAUTHORIZED\n" + |
| 34 | + pythonIndent(2) + "elif self.response.status == 404:\n" + |
| 35 | + pythonIndent(3) + "self.result = HeadRequestStatus.DOESNTEXIST\n" + |
| 36 | + pythonIndent(2) + "else:\n" + |
| 37 | + pythonIndent(3) + "self.result = HeadRequestStatus.UNKNOWN"; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String toInitResponse() { |
| 42 | + return "def __init__(self, response, request):\n" + |
| 43 | + pythonIndent(2) + "self.blob_checksums = {}\n" + |
| 44 | + pythonIndent(2) + "self.blob_checksum_type = 'NONE'\n" + |
| 45 | + pythonIndent(2) + "super(HeadObjectResponse, self).__init__(response, request)\n\n" + |
| 46 | + |
| 47 | + pythonIndent(1) + "def __process_checksum_headers(self, headers):\n" + |
| 48 | + pythonIndent(2) + "\"\"\"\n" + |
| 49 | + pythonIndent(2) + "Processes the blob checksum headers.\n" + |
| 50 | + pythonIndent(2) + ":param headers: list of tuples containing the Http response headers\n" + |
| 51 | + pythonIndent(2) + "\"\"\"\n" + |
| 52 | + pythonIndent(2) + "self.__process_checksum_type(headers)\n" + |
| 53 | + pythonIndent(2) + "self.__process_blob_checksums(headers)\n\n" + |
| 54 | + |
| 55 | + pythonIndent(1) + "def __process_checksum_type(self, headers):\n" + |
| 56 | + pythonIndent(2) + "\"\"\"\n" + |
| 57 | + pythonIndent(2) + "Parses the blob checksum type header. If there is no header, the default is NONE.\n" + |
| 58 | + pythonIndent(2) + "If there are multiple headers, then an error is raised\n" + |
| 59 | + pythonIndent(2) + ":param headers: list of tuples containing the Http response headers\n" + |
| 60 | + pythonIndent(2) + "\"\"\"\n" + |
| 61 | + pythonIndent(2) + "checksum_type_header = [item for item in headers if item[0] == 'ds3-blob-checksum-type']\n" + |
| 62 | + pythonIndent(2) + "if len(checksum_type_header) == 0:\n" + |
| 63 | + pythonIndent(3) + "return\n" + |
| 64 | + pythonIndent(2) + "if len(checksum_type_header) > 1:\n" + |
| 65 | + pythonIndent(3) + "raise ValueError(\"Expected only one header with key 'ds3-blob-checksum-type' but got: \" + str(checksum_type_header))\n" + |
| 66 | + pythonIndent(2) + "self.blob_checksum_type = checksum_type_header[0][1]\n\n" + |
| 67 | + |
| 68 | + pythonIndent(1) + "def __process_blob_checksums(self, headers):\n" + |
| 69 | + pythonIndent(2) + "\"\"\"\n" + |
| 70 | + pythonIndent(2) + "Parses the blob checksum headers and adds them to a dictionary which maps\n" + |
| 71 | + pythonIndent(2) + "blob offset to blob checksum.\n" + |
| 72 | + pythonIndent(2) + ":param headers: list of tuples containing the Http response headers\n" + |
| 73 | + pythonIndent(2) + "\"\"\"\n" + |
| 74 | + pythonIndent(2) + "# Retrieves all the headers that start with 'ds3-blob-checksum-offset-'\n" + |
| 75 | + pythonIndent(2) + "# and converts the offset at the end of the header key into an integer.\n" + |
| 76 | + pythonIndent(2) + "checksum_list = [(int(key[25:]), val) for key, val in headers if key.startswith('ds3-blob-checksum-offset-')]\n" + |
| 77 | + pythonIndent(2) + "self.blob_checksums = dict(checksum_list)\n"; |
| 78 | + } |
| 79 | +} |
0 commit comments