Skip to content

Commit afa81c0

Browse files
authored
Merge pull request #461 from RachelTucker/python_4_1
SA-229: 4.1 API support for python and python3
2 parents 31884c2 + 08b12df commit afa81c0

File tree

5 files changed

+173
-4
lines changed

5 files changed

+173
-4
lines changed

ds3-autogen-python/src/main/java/com/spectralogic/ds3autogen/python/PythonCodeGenerator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,10 @@ protected static ResponseModelGenerator<?> getResponseGenerator(final Ds3Request
274274
if (isGetObjectAmazonS3Request(ds3Request)) {
275275
return new GetObjectResponseGenerator();
276276
}
277-
if(isHeadBucketRequest(ds3Request) || isHeadObjectRequest(ds3Request)) {
277+
if (isHeadObjectRequest(ds3Request)) {
278+
return new HeadObjectResponseGenerator();
279+
}
280+
if(isHeadBucketRequest(ds3Request)) {
278281
return new HeadResponseGenerator();
279282
}
280283
return new BaseResponseGenerator();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

ds3-autogen-python/src/test/java/com.spectralogic.ds3autogen.python/PythonFunctionalTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,13 @@ public void headObjectRequest() throws TemplateModelException, IOException {
288288
hasRequestHandler(requestName, HttpVerb.GET, reqArgs, ImmutableList.of(), ImmutableList.of(), ds3Code);
289289
hasOperation(Operation.START_BULK_PUT, ds3Code);
290290

291+
assertTrue(ds3Code.contains("def __process_checksum_headers(self, headers):"));
292+
assertTrue(ds3Code.contains("def __process_checksum_type(self, headers):"));
293+
assertTrue(ds3Code.contains("def __process_blob_checksums(self, headers):"));
294+
291295
assertTrue(ds3Code.contains("self.__check_status_codes__([200, 403, 404])"));
292-
assertTrue(ds3Code.contains("self.status_code = self.response.status\n" +
293-
" if self.response.status == 200:\n" +
296+
assertTrue(ds3Code.contains("if self.response.status == 200:\n" +
297+
" self.__process_checksum_headers(response.getheaders())\n" +
294298
" self.result = HeadRequestStatus.EXISTS\n" +
295299
" elif self.response.status == 403:\n" +
296300
" self.result = HeadRequestStatus.NOTAUTHORIZED\n" +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 org.junit.Test;
19+
20+
import static com.spectralogic.ds3autogen.testutil.Ds3ModelFixtures.getHeadObjectRequest;
21+
import static org.hamcrest.CoreMatchers.is;
22+
import static org.junit.Assert.assertThat;
23+
24+
public class HeadObjectResponseGenerator_Test {
25+
private final static HeadObjectResponseGenerator generator = new HeadObjectResponseGenerator();
26+
27+
@Test
28+
public void toParseResponsePayloadTest() {
29+
final String expected = "if self.response.status == 200:\n" +
30+
" self.__process_checksum_headers(response.getheaders())\n" +
31+
" self.result = HeadRequestStatus.EXISTS\n" +
32+
" elif self.response.status == 403:\n" +
33+
" self.result = HeadRequestStatus.NOTAUTHORIZED\n" +
34+
" elif self.response.status == 404:\n" +
35+
" self.result = HeadRequestStatus.DOESNTEXIST\n" +
36+
" else:\n" +
37+
" self.result = HeadRequestStatus.UNKNOWN";
38+
39+
assertThat(generator.toParseResponsePayload(getHeadObjectRequest()), is(expected));
40+
}
41+
42+
@Test
43+
public void toInitResponseTest() {
44+
final String expected = "def __init__(self, response, request):\n" +
45+
" self.blob_checksums = {}\n" +
46+
" self.blob_checksum_type = 'NONE'\n" +
47+
" super(HeadObjectResponse, self).__init__(response, request)\n" +
48+
"\n" +
49+
" def __process_checksum_headers(self, headers):\n" +
50+
" \"\"\"\n" +
51+
" Processes the blob checksum headers.\n" +
52+
" :param headers: list of tuples containing the Http response headers\n" +
53+
" \"\"\"\n" +
54+
" self.__process_checksum_type(headers)\n" +
55+
" self.__process_blob_checksums(headers)\n" +
56+
"\n" +
57+
" def __process_checksum_type(self, headers):\n" +
58+
" \"\"\"\n" +
59+
" Parses the blob checksum type header. If there is no header, the default is NONE.\n" +
60+
" If there are multiple headers, then an error is raised\n" +
61+
" :param headers: list of tuples containing the Http response headers\n" +
62+
" \"\"\"\n" +
63+
" checksum_type_header = [item for item in headers if item[0] == 'ds3-blob-checksum-type']\n" +
64+
" if len(checksum_type_header) == 0:\n" +
65+
" return\n" +
66+
" if len(checksum_type_header) > 1:\n" +
67+
" raise ValueError(\"Expected only one header with key 'ds3-blob-checksum-type' but got: \" + str(checksum_type_header))\n" +
68+
" self.blob_checksum_type = checksum_type_header[0][1]\n" +
69+
"\n" +
70+
" def __process_blob_checksums(self, headers):\n" +
71+
" \"\"\"\n" +
72+
" Parses the blob checksum headers and adds them to a dictionary which maps\n" +
73+
" blob offset to blob checksum.\n" +
74+
" :param headers: list of tuples containing the Http response headers\n" +
75+
" \"\"\"\n" +
76+
" # Retrieves all the headers that start with 'ds3-blob-checksum-offset-'\n" +
77+
" # and converts the offset at the end of the header key into an integer.\n" +
78+
" checksum_list = [(int(key[25:]), val) for key, val in headers if key.startswith('ds3-blob-checksum-offset-')]\n" +
79+
" self.blob_checksums = dict(checksum_list)\n";
80+
81+
assertThat(generator.toInitResponse(), is(expected));
82+
}
83+
}

ds3-autogen-python/src/test/java/com.spectralogic.ds3autogen.python/generators/response/HeadResponseGenerator_Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void toParseResponsePayload_Test() {
4141
" else:\n" +
4242
" self.result = HeadRequestStatus.UNKNOWN";
4343
assertThat(generator.toParseResponsePayload(null), is(expected));
44-
assertThat(generator.toParseResponsePayload(getHeadObjectRequest()), is(expected));
44+
assertThat(generator.toParseResponsePayload(getHeadBucketRequest()), is(expected));
4545
}
4646

4747
@Test(expected = IllegalArgumentException.class)

0 commit comments

Comments
 (0)