Best Python code snippet using localstack_python
apigatewaysetup.py
Source:apigatewaysetup.py  
...74                    "application/json": "Empty"75                }76            )77            # # Put a 200 integration response in the POST method78            # api_gateway.put_integration_response(79            #     restApiId=rest_api_id,80            #     resourceId=rest_api_root_id,81            #     httpMethod="POST",82            #     statusCode="200",83            #     responseParameters={84            #         "method.response.header.Set-Cookie": (85            #             "integration.response.body.Set-Cookie"),86            #         "method.response.header.Access-Control-Allow-Credentials": (87            #             "\'true\'"),88            #         "method.response.header.Access-Control-Allow-Origin": (89            #             "\'https://s3.amazonaws.com\'")90            #     },91            #     responseTemplates={92            #         "application/json": "$input.path('$.body')"93            #     }94            # )95            # Put a 400 method response in the POST method96            api_gateway.put_method_response(97                restApiId=rest_api_id,98                resourceId=rest_api_root_id,99                httpMethod="POST",100                statusCode="400",101                responseParameters={102                    "method.response.header.Access-Control-Allow-Credentials": False,103                    "method.response.header.Access-Control-Allow-Origin": False104                },105                responseModels={106                    "application/json": "Empty"107                }108            )109            # # Put a 400 integration response in the POST method110            # api_gateway.put_integration_response(111            #     restApiId=rest_api_id,112            #     resourceId=rest_api_root_id,113            #     httpMethod="POST",114            #     statusCode="400",115            #     selectionPattern=".*'status': 400.*",116            #     responseParameters={117            #         "method.response.header.Access-Control-Allow-Credentials": (118            #             "\'true\'"),119            #         "method.response.header.Access-Control-Allow-Origin": (120            #             "\'https://s3.amazonaws.com\'")121            #     },122            #     responseTemplates={123            #         "application/json": "$input.path('$.errorMessage')"124            #     }125            # )126            # print "POST method added"127        except botocore.exceptions.ClientError as e:128            print e.response["Error"]["Code"]129            print e.response["Error"]["Message"]130            sys.exit()131        self.create_api_permissions_uri()132        self.create_api_url()133    @staticmethod134    def create_get_method(self, api_gateway, rest_api_id, rest_api_root_id):135        """136               Creates the get method and links it to the lambda function.137               :param api_gateway: api gateway reference from boto3.client("apigateway")138               :param rest_api_id: id of API gateway139               :param rest_api_root_id: id of API parent path140        """141        print "Adding GET method to rest api"142        try:143            # Add a POST method to the rest api144            api_gateway.put_method(145                restApiId=rest_api_id,146                resourceId=rest_api_root_id,147                httpMethod="GET",148                authorizationType="NONE",149                requestParameters={150                    "method.request.header.Cookie": False151                }152            )153            # Put integration in the GET method154            api_gateway.put_integration(155                restApiId=rest_api_id,156                resourceId=rest_api_root_id,157                httpMethod="GET",158                type="AWS_PROXY",159                passthroughBehavior="NEVER",160                integrationHttpMethod="POST",161                uri=self.constants["API_INVOCATION_URI"]162                # requestTemplates={163                #     "application/json": (164                #         "#if($input.params(\"Cookie\") && $input.params(\"Cookie\") != \"\") "165                #         "{"166                #         "\"body\": $input.body, "167                #         "\"token\": \"$input.params(\"Cookie\")\", "168                #         "\"headers\": { #foreach($header in $input.params().header.keySet()) \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end #end}, "169                #         "\"method\": \"$context.httpMethod\",\"params\": { #foreach($param in $input.params().path.keySet()) \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end #end }, "170                #         "\"query\": { #foreach($queryParam in $input.params().querystring.keySet()) \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\" #if($foreach.hasNext),#end #end}"171                #         "}"172                #         "#else"173                #         "{"174                #         "\"body\": $input.body, "175                #         "\"headers\": { #foreach($header in $input.params().header.keySet()) \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end #end}, "176                #         "\"method\": \"$context.httpMethod\",\"params\": { #foreach($param in $input.params().path.keySet()) \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end #end }, "177                #         "\"query\": { #foreach($queryParam in $input.params().querystring.keySet()) \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\"#if($foreach.hasNext),#end #end}"178                #         "}"179                #         "#end"180                #     )181                # }182            )183            # Put a 200 method response in the POST method184            api_gateway.put_method_response(185                restApiId=rest_api_id,186                resourceId=rest_api_root_id,187                httpMethod="GET",188                statusCode="200",189                responseParameters={190                    "method.response.header.Set-Cookie": False,191                    "method.response.header.Access-Control-Allow-Credentials": False,192                    "method.response.header.Access-Control-Allow-Origin": False193                },194                responseModels={195                    "application/json": "Empty"196                }197            )198            # # Put a 200 integration response in the GET method199            # api_gateway.put_integration_response(200            #     restApiId=rest_api_id,201            #     resourceId=rest_api_root_id,202            #     httpMethod="GET",203            #     statusCode="200",204            #     responseParameters={205            #         "method.response.header.Set-Cookie": (206            #             "integration.response.body.Set-Cookie"),207            #         "method.response.header.Access-Control-Allow-Credentials": (208            #             "\'true\'"),209            #         "method.response.header.Access-Control-Allow-Origin": (210            #             "\'https://s3.amazonaws.com\'")211            #     },212            #     responseTemplates={213            #         "application/json": "$input.path('$.body')"214            #     }215            # )216            # Put a 400 method response in the GET method217            api_gateway.put_method_response(218                restApiId=rest_api_id,219                resourceId=rest_api_root_id,220                httpMethod="GET",221                statusCode="400",222                responseParameters={223                    "method.response.header.Access-Control-Allow-Credentials": False,224                    "method.response.header.Access-Control-Allow-Origin": False225                },226                responseModels={227                    "application/json": "Empty"228                }229            )230            # # Put a 400 integration response in the GET method231            # api_gateway.put_integration_response(232            #     restApiId=rest_api_id,233            #     resourceId=rest_api_root_id,234            #     httpMethod="GET",235            #     statusCode="400",236            #     selectionPattern=".*'status': 400.*",237            #     responseParameters={238            #         "method.response.header.Access-Control-Allow-Credentials": (239            #             "\'true\'"),240            #         "method.response.header.Access-Control-Allow-Origin": (241            #             "\'https://s3.amazonaws.com\'")242            #     },243            #     responseTemplates={244            #         "application/json": "$input.path('$.errorMessage')"245            #     }246            # )247            print "GET method added"248        except botocore.exceptions.ClientError as e:249            print e.response["Error"]["Code"]250            print e.response["Error"]["Message"]251            sys.exit()252        self.create_api_permissions_uri()253        self.create_api_url()254    @staticmethod255    def create_delete_method(self, api_gateway, rest_api_id, rest_api_root_id):256        """257               Creates the delete method and links it to the lambda function.258               :param api_gateway: api gateway reference from boto3.client("apigateway")259               :param rest_api_id: id of API gateway260               :param rest_api_root_id: id of API parent path261        """262        print "Adding DELETE method to rest api"263        try:264            # Add a DELETE method to the rest api265            api_gateway.put_method(266                restApiId=rest_api_id,267                resourceId=rest_api_root_id,268                httpMethod="DELETE",269                authorizationType="NONE",270                requestParameters={271                    "method.request.header.Cookie": False272                }273            )274            # Put integration in the POST method275            api_gateway.put_integration(276                restApiId=rest_api_id,277                resourceId=rest_api_root_id,278                httpMethod="DELETE",279                type="AWS_PROXY",280                passthroughBehavior="NEVER",281                integrationHttpMethod="POST",282                uri=self.constants["API_INVOCATION_URI"]283                # requestTemplates={284                #     "application/json": (285                #         "#if($input.params(\"Cookie\") && $input.params(\"Cookie\") != \"\") "286                #         "{"287                #         "\"body\": $input.body, "288                #         "\"token\": \"$input.params(\"Cookie\")\", "289                #         "\"headers\": { #foreach($header in $input.params().header.keySet()) \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end #end}, "290                #         "\"method\": \"$context.httpMethod\",\"params\": { #foreach($param in $input.params().path.keySet()) \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end #end }, "291                #         "\"query\": { #foreach($queryParam in $input.params().querystring.keySet()) \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\" #if($foreach.hasNext),#end #end}"292                #         "}"293                #         "#else"294                #         "{"295                #         "\"body\": $input.body, "296                #         "\"headers\": { #foreach($header in $input.params().header.keySet()) \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end #end}, "297                #         "\"method\": \"$context.httpMethod\",\"params\": { #foreach($param in $input.params().path.keySet()) \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end #end }, "298                #         "\"query\": { #foreach($queryParam in $input.params().querystring.keySet()) \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\"#if($foreach.hasNext),#end #end}"299                #         "}"300                #         "#end"301                #     )302                # }303            )304            # Put a 200 method response in the DELETE method305            api_gateway.put_method_response(306                restApiId=rest_api_id,307                resourceId=rest_api_root_id,308                httpMethod="DELETE",309                statusCode="200",310                responseParameters={311                    "method.response.header.Set-Cookie": False,312                    "method.response.header.Access-Control-Allow-Credentials": False,313                    "method.response.header.Access-Control-Allow-Origin": False314                },315                responseModels={316                    "application/json": "Empty"317                }318            )319            # # Put a 200 integration response in the DELETE method320            # api_gateway.put_integration_response(321            #     restApiId=rest_api_id,322            #     resourceId=rest_api_root_id,323            #     httpMethod="DELETE",324            #     statusCode="200",325            #     responseParameters={326            #         "method.response.header.Set-Cookie": (327            #             "integration.response.body.Set-Cookie"),328            #         "method.response.header.Access-Control-Allow-Credentials": (329            #             "\'true\'"),330            #         "method.response.header.Access-Control-Allow-Origin": (331            #             "\'https://s3.amazonaws.com\'")332            #     },333            #     responseTemplates={334            #         "application/json": "$input.path('$.body')"335            #     }336            # )337            # Put a 400 method response in the DELETE method338            api_gateway.put_method_response(339                restApiId=rest_api_id,340                resourceId=rest_api_root_id,341                httpMethod="DELETE",342                statusCode="400",343                responseParameters={344                    "method.response.header.Access-Control-Allow-Credentials": False,345                    "method.response.header.Access-Control-Allow-Origin": False346                },347                responseModels={348                    "application/json": "Empty"349                }350            )351            # # Put a 400 integration response in the DELETE method352            # api_gateway.put_integration_response(353            #     restApiId=rest_api_id,354            #     resourceId=rest_api_root_id,355            #     httpMethod="DELETE",356            #     statusCode="400",357            #     selectionPattern=".*\"status\": 400.*",358            #     responseParameters={359            #         "method.response.header.Access-Control-Allow-Credentials": (360            #             "\'true\'"),361            #         "method.response.header.Access-Control-Allow-Origin": (362            #             "\'https://s3.amazonaws.com\'")363            #     },364            #     responseTemplates={365            #         "application/json": "$input.path('$.errorMessage')"366            #     }367            # )368            print "DELETE method added"369        except botocore.exceptions.ClientError as e:370            print e.response["Error"]["Code"]371            print e.response["Error"]["Message"]372            sys.exit()373        self.create_api_permissions_uri()374        self.create_api_url()375    @staticmethod376    def create_put_method(self, api_gateway, rest_api_id, rest_api_root_id):377        """378               Creates the put method and links it to the lambda function.379               :param api_gateway: api gateway reference from boto3.client("apigateway")380               :param rest_api_id: id of API gateway381               :param rest_api_root_id: id of API parent path382        """383        print "Adding PUT method to rest api"384        try:385            # Add a PUT method to the rest api386            api_gateway.put_method(387                restApiId=rest_api_id,388                resourceId=rest_api_root_id,389                httpMethod="PUT",390                authorizationType="NONE",391                requestParameters={392                    "method.request.header.Cookie": False393                }394            )395            # Put integration in the PUT method396            api_gateway.put_integration(397                restApiId=rest_api_id,398                resourceId=rest_api_root_id,399                httpMethod="PUT",400                type="AWS_PROXY",401                passthroughBehavior="NEVER",402                integrationHttpMethod="POST",403                uri=self.constants["API_INVOCATION_URI"]404                # requestTemplates={405                #     "application/json": (406                #         "#if($input.params(\"Cookie\") && $input.params(\"Cookie\") != \"\") "407                #         "{"408                #         "\"body\": $input.body, "409                #         "\"token\": \"$input.params(\"Cookie\")\", "410                #         "\"headers\": { #foreach($header in $input.params().header.keySet()) \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end #end}, "411                #         "\"method\": \"$context.httpMethod\",\"params\": { #foreach($param in $input.params().path.keySet()) \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end #end }, "412                #         "\"query\": { #foreach($queryParam in $input.params().querystring.keySet()) \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\" #if($foreach.hasNext),#end #end}"413                #         "}"414                #         "#else"415                #         "{"416                #         "\"body\": $input.body, "417                #         "\"headers\": { #foreach($header in $input.params().header.keySet()) \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end #end}, "418                #         "\"method\": \"$context.httpMethod\",\"params\": { #foreach($param in $input.params().path.keySet()) \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end #end }, "419                #         "\"query\": { #foreach($queryParam in $input.params().querystring.keySet()) \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\"#if($foreach.hasNext),#end #end}"420                #         "}"421                #         "#end"422                #     )423                # }424            )425            # Put a 200 method response in the PUT method426            api_gateway.put_method_response(427                restApiId=rest_api_id,428                resourceId=rest_api_root_id,429                httpMethod="PUT",430                statusCode="200",431                responseParameters={432                    "method.response.header.Set-Cookie": False,433                    "method.response.header.Access-Control-Allow-Credentials": False,434                    "method.response.header.Access-Control-Allow-Origin": False435                },436                responseModels={437                    "application/json": "Empty"438                }439            )440            # # Put a 200 integration response in the PUT method441            # api_gateway.put_integration_response(442            #     restApiId=rest_api_id,443            #     resourceId=rest_api_root_id,444            #     httpMethod="PUT",445            #     statusCode="200",446            #     responseParameters={447            #         "method.response.header.Set-Cookie": (448            #             "integration.response.body.Set-Cookie"),449            #         "method.response.header.Access-Control-Allow-Credentials": (450            #             "\'true\'"),451            #         "method.response.header.Access-Control-Allow-Origin": (452            #             "\'https://s3.amazonaws.com\'")453            #     },454            #     responseTemplates={455            #         "application/json": "$input.path('$.body')"456            #     }457            # )458            # Put a 400 method response in the PUT method459            api_gateway.put_method_response(460                restApiId=rest_api_id,461                resourceId=rest_api_root_id,462                httpMethod="PUT",463                statusCode="400",464                responseParameters={465                    "method.response.header.Access-Control-Allow-Credentials": False,466                    "method.response.header.Access-Control-Allow-Origin": False467                },468                responseModels={469                    "application/json": "Empty"470                }471            )472            # # Put a 400 integration response in the PUT method473            # api_gateway.put_integration_response(474            #     restApiId=rest_api_id,475            #     resourceId=rest_api_root_id,476            #     httpMethod="PUT",477            #     statusCode="400",478            #     selectionPattern=".*\"status\": 400.*",479            #     responseParameters={480            #         "method.response.header.Access-Control-Allow-Credentials": (481            #             "\'true\'"),482            #         "method.response.header.Access-Control-Allow-Origin": (483            #             "\'https://s3.amazonaws.com\'")484            #     },485            #     responseTemplates={486            #         "application/json": "$input.path('$.errorMessage')"487            #     }488            # )489            print "PUT method added"490        except botocore.exceptions.ClientError as e:491            print e.response["Error"]["Code"]492            print e.response["Error"]["Message"]493            sys.exit()494        self.create_api_permissions_uri()495        self.create_api_url()496    @staticmethod497    def create_options_method(self, api_gateway, rest_api_id, rest_api_root_id):498        """499               Creates the option method and links it to the lambda function.500               :param api_gateway: api gateway reference from boto3.client("apigateway")501               :param rest_api_id: id of API gateway502               :param rest_api_root_id: id of API parent path503        """504        try:505            print "Adding OPTIONS method to rest api"506            # Add an options method to the rest api507            api_gateway.put_method(508                restApiId=rest_api_id,509                resourceId=rest_api_root_id,510                httpMethod="OPTIONS",511                authorizationType="NONE"512            )513            # Set the put integration of the OPTIONS method514            api_gateway.put_integration(515                restApiId=rest_api_id,516                resourceId=rest_api_root_id,517                httpMethod="OPTIONS",518                type="MOCK",519                requestTemplates={520                    "application/json": "{\"statusCode\": 200}"521                }522            )523            # Set the put method response of the OPTIONS method524            api_gateway.put_method_response(525                restApiId=rest_api_id,526                resourceId=rest_api_root_id,527                httpMethod="OPTIONS",528                statusCode="200",529                responseParameters={530                    "method.response.header.Access-Control-Allow-Headers": False,531                    "method.response.header.Access-Control-Allow-Origin": False,532                    "method.response.header.Access-Control-Allow-Credentials": False,533                    "method.response.header.Access-Control-Allow-Methods": False534                },535                responseModels={536                    "application/json": "Empty"537                }538            )539            # Set the put integration response of the OPTIONS method540            api_gateway.put_integration_response(541                restApiId=rest_api_id,542                resourceId=rest_api_root_id,543                httpMethod="OPTIONS",544                statusCode="200",545                responseParameters={546                    "method.response.header.Access-Control-Allow-Headers": (547                        "\'Content-Type,X-Amz-Date,Authorization,X-Api-Key,"548                        "X-Amz-Security-Token,Cookie,Accept,"549                        "Access-Control-Allow-Origin\'"550                    ),551                    "method.response.header.Access-Control-Allow-Origin": (552                        "\'https://s3.amazonaws.com\'"),553                    "method.response.header.Access-Control-Allow-Credentials": (554                        "\'true\'"),...api_helper.py
Source:api_helper.py  
...136        httpMethod='POST',137        statusCode='200',138        responseModels={'application/json': 'Empty'}139    )140    put_integration_response = api_client.put_integration_response(141        restApiId=rest_api_id,142        resourceId=api_resource_id,143        httpMethod='POST',144        statusCode='200',145        responseTemplates={"application/json": ""}146    )147    put_integration_response = api_client.put_integration_response(148        restApiId=rest_api_id,149        resourceId=api_resource_id,150        httpMethod='POST',151        statusCode='400',152        selectionPattern='Invalid*',153        responseTemplates={"application/json": ""}154    )155        156    put_integration_response = api_client.put_integration_response(157        restApiId=rest_api_id,158        resourceId=api_resource_id,159        httpMethod='POST',160        statusCode='500',161        selectionPattern='Internal*',162        responseTemplates={"application/json": ""}163    )164    165    # Deploy to a stage166    deployment = api_client.create_deployment(167        restApiId=rest_api_id,168        stageName='dev',169      )170    # API gateway needs permissions to invoke the lambda function...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
