1616public class APIDemoHandler implements RequestStreamHandler {
1717
1818 private JSONParser parser = new JSONParser ();
19- private static final String DYNAMODB_TABLE_NAME = System .getenv ("TABLE_NAME" );
19+ private static final String DYNAMODB_TABLE_NAME = System .getenv ("TABLE_NAME" );
2020
2121 @ Override
2222 public void handleRequest (InputStream inputStream , OutputStream outputStream , Context context ) throws IOException {
@@ -35,10 +35,8 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
3535 Person person = new Person ((String ) event .get ("body" ));
3636
3737 dynamoDb .getTable (DYNAMODB_TABLE_NAME )
38- .putItem (new PutItemSpec ().withItem (new Item ().withNumber ("id" , person .getId ())
39- .withString ("firstName" , person .getFirstName ())
40- .withString ("lastName" , person .getLastName ()).withNumber ("age" , person .getAge ())
41- .withString ("address" , person .getAddress ())));
38+ .putItem (new PutItemSpec ().withItem (new Item ().withNumber ("id" , person .getId ())
39+ .withString ("name" , person .getName ())));
4240 }
4341
4442 JSONObject responseBody = new JSONObject ();
@@ -61,8 +59,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
6159 writer .close ();
6260 }
6361
64- public void handleGetByPathParam (InputStream inputStream , OutputStream outputStream , Context context )
65- throws IOException {
62+ public void handleGetByParam (InputStream inputStream , OutputStream outputStream , Context context ) throws IOException {
6663
6764 BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream ));
6865 JSONObject responseJson = new JSONObject ();
@@ -81,61 +78,20 @@ public void handleGetByPathParam(InputStream inputStream, OutputStream outputStr
8178 if (pps .get ("id" ) != null ) {
8279
8380 int id = Integer .parseInt ((String ) pps .get ("id" ));
84- result = dynamoDb .getTable (DYNAMODB_TABLE_NAME ).getItem ("id" , id );
81+ result = dynamoDb .getTable (DYNAMODB_TABLE_NAME )
82+ .getItem ("id" , id );
8583 }
8684
87- }
88- if (result != null ) {
89-
90- Person person = new Person (result .toJSON ());
91- responseBody .put ("Person" , person );
92- responseJson .put ("statusCode" , 200 );
93- } else {
94-
95- responseBody .put ("message" , "No item found" );
96- responseJson .put ("statusCode" , 404 );
97- }
98-
99- JSONObject headerJson = new JSONObject ();
100- headerJson .put ("x-custom-header" , "my custom header value" );
101-
102- responseJson .put ("headers" , headerJson );
103- responseJson .put ("body" , responseBody .toString ());
104-
105- } catch (ParseException pex ) {
106- responseJson .put ("statusCode" , 400 );
107- responseJson .put ("exception" , pex );
108- }
109-
110- OutputStreamWriter writer = new OutputStreamWriter (outputStream , "UTF-8" );
111- writer .write (responseJson .toString ());
112- writer .close ();
113- }
114-
115- public void handleGetByQueryParam (InputStream inputStream , OutputStream outputStream , Context context )
116- throws IOException {
117-
118- BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream ));
119- JSONObject responseJson = new JSONObject ();
120-
121- AmazonDynamoDB client = AmazonDynamoDBClientBuilder .defaultClient ();
122- DynamoDB dynamoDb = new DynamoDB (client );
123-
124- Item result = null ;
125- try {
126- JSONObject event = (JSONObject ) parser .parse (reader );
127- JSONObject responseBody = new JSONObject ();
128-
129- if (event .get ("queryStringParameters" ) != null ) {
85+ } else if (event .get ("queryStringParameters" ) != null ) {
13086
13187 JSONObject qps = (JSONObject ) event .get ("queryStringParameters" );
13288 if (qps .get ("id" ) != null ) {
13389
13490 int id = Integer .parseInt ((String ) qps .get ("id" ));
135- result = dynamoDb .getTable (DYNAMODB_TABLE_NAME ).getItem ("id" , id );
91+ result = dynamoDb .getTable (DYNAMODB_TABLE_NAME )
92+ .getItem ("id" , id );
13693 }
13794 }
138-
13995 if (result != null ) {
14096
14197 Person person = new Person (result .toJSON ());
@@ -145,7 +101,7 @@ public void handleGetByQueryParam(InputStream inputStream, OutputStream outputSt
145101
146102 responseBody .put ("message" , "No item found" );
147103 responseJson .put ("statusCode" , 404 );
148- }
104+ }
149105
150106 JSONObject headerJson = new JSONObject ();
151107 headerJson .put ("x-custom-header" , "my custom header value" );
@@ -162,5 +118,4 @@ public void handleGetByQueryParam(InputStream inputStream, OutputStream outputSt
162118 writer .write (responseJson .toString ());
163119 writer .close ();
164120 }
165-
166121}
0 commit comments