def example_service(event): return core.callapi( path_param_schema=ExamplePathParam, response_schema=ExampleResponse, http_method=core.HttpMethod.GET, api_url='http://echo.jsontest.com/customer/{customer_id}/name/{name}', event=event)
def test_get_callapi(requests_mock): event = { 'resource': '/example', 'httpMethod': 'GET', 'pathParameters': { 'customer_id': '1', 'name': 'EPOPEIA' } } class ExamplePathParam(Schema): customer_id = fields.Str() name = fields.Str() class ExampleResponse(Schema): customer = fields.Str() name = fields.Str() api_url = 'http://echo.jsontest.com/customer/{customer_id}/name/{name}' requests_mock.get(api_url.replace('{customer_id}', '1').replace('{name}', 'EPOPEIA'), json={ "customer": "1", "name": "EPOPEIA" }) result = core.callapi(path_param_schema=ExamplePathParam, response_schema=ExampleResponse, http_method=core.HttpMethod.GET, api_url=api_url, event=event) assert result.get('name') == "EPOPEIA" assert result.get('customer') == "1"
def test_get_callapi_with_error(requests_mock): event = { 'resource': '/example', 'httpMethod': 'GET', 'pathParameters': { 'customer_id': None } } class ExamplePathParam(Schema): customer_id = fields.Str() name = fields.Str() class ExampleResponse(Schema): customer = fields.Str() name = fields.Str() with pytest.raises(exceptions.UnprocessableException): api_url = 'http://echo.jsontest.com/customer/' requests_mock.get(api_url, json={"customer": "1", "name": "EPOPEIA"}) result = core.callapi(path_param_schema=ExamplePathParam, response_schema=ExampleResponse, http_method=core.HttpMethod.GET, api_url=api_url, event=event)
def test_post_callapi_without_text_body_response_error(requests_mock): event = {'resource': '/example', 'httpMethod': 'POST', 'body': ''} class ExampleRequest(Schema): customer_id = fields.Str(required=True) name = fields.Str(required=True) class ExampleResponse(Schema): customer = fields.Str() name = fields.Str() with pytest.raises(exceptions.UnprocessableException): api_url = 'http://echo.jsontest.com/customer/' requests_mock.post(api_url, status_code=422, text='error') core.callapi(request_schema=ExampleRequest, response_schema=ExampleResponse, http_method=core.HttpMethod.POST, api_url=api_url, event=event)
def test_post_callapi_with_success(requests_mock): event = { 'resource': '/example', 'httpMethod': 'POST', 'body': '{"customer_id": "1", "name": "EPOPEIA"}' } class ExampleRequest(Schema): customer_id = fields.Str(required=True) name = fields.Str(required=True) class ExampleResponse(Schema): customer = fields.Str() name = fields.Str() api_url = 'http://echo.jsontest.com/customer/' requests_mock.post(api_url, json={"customer": "1", "name": "EPOPEIA"}) core.callapi(request_schema=ExampleRequest, response_schema=ExampleResponse, http_method=core.HttpMethod.POST, api_url=api_url, event=event)
def test_post_callapi_with_error_querystring(requests_mock): event = { 'resource': '/example', 'httpMethod': 'GET', 'queryStringParameters': {} } class ExampleRequestQueryString(Schema): id = fields.Str(required=True) class ExampleResponse(Schema): customer = fields.Str() name = fields.Str() api_url = 'http://echo.jsontest.com/customer/' requests_mock.get(api_url, json={"customer": "123", "name": "EPOPEIA"}) with pytest.raises(exceptions.UnprocessableException): core.callapi(querystring_schema=ExampleRequestQueryString, response_schema=ExampleResponse, http_method=core.HttpMethod.GET, api_url=api_url, event=event)
def test_post_callapi_with_response_not_found(requests_mock): event = { 'resource': '/example', 'httpMethod': 'POST', 'body': '{"customer_id":"1", "name": "EPOPEIA"}' } class ExampleRequest(Schema): customer_id = fields.Str(required=True) name = fields.Str(required=True) class ExampleResponse(Schema): customer = fields.Str() name = fields.Str() with pytest.raises(exceptions.NotFoundException): api_url = 'http://echo.jsontest.com/customer/' requests_mock.post(api_url, status_code=404, text='error') core.callapi(request_schema=ExampleRequest, response_schema=ExampleResponse, http_method=core.HttpMethod.POST, api_url=api_url, event=event)
def example_service(event): return core.callapi(querystring_schema=ExampleQueryString, response_schema=ExampleResponse, http_method=core.HttpMethod.GET, api_url='http://echo.jsontest.com/key/value/one/two', event=event)