How do i format the headers field in step function api invoke - amazon-web-services

I am trying to execute a AWS api via step function. I need to pass the authorizationToken value in the header field.
{
"ApiEndpoint": "ccqk9ijm0h.execute-api.ap-southeast-2.amazonaws.com",
"Method": "POST",
"Headers": {
"authorizationToken.$": [
"$.InputToken"
]
},
"Stage": "test",
"Path": "/",
"RequestBody": {
"productType": [],
"xxx.$": "$.xxx",
"yyy.$": "$.yyy",
"zzz.$": "$.zzz"
},
"AuthType": "IAM_ROLE"
}
I am getting the following error -
The value for the field 'authorizationToken.$' must be a STRING that contains a JSONPath but was an ARRAY (at /States/GetDeclarations/Parameters)
This is the default syntax for the API invoke -
"Headers": {
"Header1": [
"HeaderValue1"
],
"Header2": [
"HeaderValue2",
"HeaderValue3"
]
}
When i modify this to
"Headers": {
"authorizationToken": [
"1234"
],
"Header2": [
"HeaderValue2",
"HeaderValue3"
]
}
It works fine.
I need to make the value of "authorizationToken" a variable that takes its value from the input.
My Input data looks like this
{
"xxx": "123",
"yyy": "123",
"zzz": "123",
"InputToken": "123",
"aaa": "123"
}

You need to use the States.Array Intrinsic Function as I've shown below. This allows you to inject an array into a node in your Parameters block. In this case, you just want a single item in the array, but you can include multiple items as well (e.g., States.Array($.item1,$.item2,$.item3)).
Check out the other Intrinsic Functions as well, as they are handy for overcoming challenges like this.
{
"ApiEndpoint": "ccqk9ijm0h.execute-api.ap-southeast-2.amazonaws.com",
"Method": "POST",
"Headers": {
"authorizationToken.$": "States.Array($.InputToken)"
},
"Stage": "test",
"Path": "/",
"RequestBody": {
"productType": [],
"xxx.$": "$.xxx",
"yyy.$": "$.yyy",
"zzz.$": "$.zzz"
},
"AuthType": "IAM_ROLE"
}

Related

Cannot add variable from context on QueryParameters on Step Function API Gateway call

I'm trying two consecutive API Gateway calls from a Step Function, using values from the first result on the second. I tried already on a POST call and it worked, but now I can't do it on a GET call. This is my step function:
{
"Comment": "A Hello World example of the Amazon States Language using Pass states",
"StartAt": "Api Call",
"States": {
"Api Call": {
"Type": "Task",
"Resource": "arn:aws:states:::apigateway:invoke",
"Parameters": {
"ApiEndpoint": "***************.us-east-1.amazonaws.com",
"Method": "GET",
"Path": "universities",
"RequestBody": {},
"AuthType": "NO_AUTH"
},
"ResultSelector": {
"logWord.$": "$.StatusText"
},
"Next": "Api Call 2"
},
"Api Call 2": {
"Type": "Task",
"Resource": "arn:aws:states:::apigateway:invoke",
"Parameters": {
"ApiEndpoint": "***************.us-east-1.amazonaws.com",
"Method": "GET",
"Path": "logging",
"Headers": {
"Content-Type": ["application/json"]
},
"QueryParameters": {
"logWord.$": "$.logWord"
},
"AuthType": "NO_AUTH"
},
"End": true
}
}
}
The error that I get is the following:
{
"error": "States.Runtime",
"cause": "An error occurred while executing the state 'Api Call 2' (entered at the event id #7). The Parameters '{\"ApiEndpoint\":\"***************.us-east-1.amazonaws.com\",\"Method\":\"GET\",\"Path\":\"logging\",\"Headers\":{\"Content-Type\":[\"application/json\"]},\"QueryParameters\":{\"logWord\":\"OK\"},\"AuthType\":\"NO_AUTH\"}' could not be used to start the Task: [The value of the field 'QueryParameters' has an invalid format]"
}
According to the documentation, query parameters should be between brackets, but if I'm using a context variable I can't put the brackets. The console doesn't allow me to even save (The value for the field 'logWord.$' must be a STRING that contains a JSONPath but was an ARRAY).
If anyone knows how to make GET call using context variables would be greatly appreciated.
Looks like QueryParameters wants a list of strings. Use the States.Array intrinsic function to convert your interpolated string to a [string] list.
"QueryParameters": {
"logWord.$": "States.Array($.logWord)" // ["OK"]
},

How to skip particular set of test cases in a collection in terminal using newman?

I am having a postman collection which consists of request and test cases for each requests. I have two test case for each request. one for validating status code and other for validating response time. I need to execute status code test case frequently and response time test case results occasionally.How to achieve it without modifying the collection for every run and is it achievable in providing any option in terminal?
collection.json
{
"name": "Metadata",
"item": [
{
"name": "info",
"event": [
{
"listen": "test",
"script": {
"id": "32cf67e7-5d42-4231-86fe-e7fffa32c855",
"exec": [
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"pm.test(\"Response time is less than 300ms\", function () {",
" pm.expect(pm.response.responseTime).to.be.below(300);",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{tokenAdmin}}",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"url": {
"raw": "{{url}}/api/m0/metadata/info",
"host": [
"{{url}}"
],
"path": [
"api",
"m0",
"metadata",
"info"
]
}
},
"response": []
}
],
"protocolProfileBehavior": {},
"_postman_isSubFolder": true
}
For a very basic flow, you can use moment to check which day it currently is and if that matches the condition, it will run the responseTime test.
let moment = require('moment'),
date = moment().format('dddd');
// Runs on each request
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// Only runs on a Friday
if (date === 'Friday') {
pm.test("Response time is less than 1000ms", function () {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
}
Moment has lots of different options available to you and might work if you want to only run that check at the end of the sprint or on a given day.

Validating request path parameter of AWS API gateway?

Let's say I have an api with paths / and /{pets} and /{pets}/pet. Now I'm trying to validate the path {pets} parameter so that only path having alphanumerical characters of length 6 will be validated and processed to the backend lambda all others will be rejected. I tried the following swagger schema specifying format and type for the parameter. I even tried using pattern in the schema but it seems to be not working. May I know how can we limit only path parameters of certain IDs.
{
......
"/{pets}/pet": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [{
"name": "pets",
"in": "path",
"required": true,
"schema": {
"type": "string",
"pattern": "[A-Za-z0-9]{6}"
}
}],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
}
},
"x-amazon-apigateway-request-validator": "Validate query string parameters and headers",
"x-amazon-apigateway-integration": {
"responses": {
"default": {
"statusCode": "200"
}
},
"requestTemplates": {
"application/json": "## See ...."
},
"uri": "........",
"passthroughBehavior": "when_no_templates",
"httpMethod": "POST",
"contentHandling": "CONVERT_TO_TEXT",
"type": "aws"
}
}
}
.....
}
What I'm trying to achieve:
https://api.example.com/aacc77/pet -- Accept
https://api.example.com/aacc77s/pet -- Reject
https://api.example.com/aacc7/pet -- Reject
https://api.example.com/aacc_7/pet -- Reject
Basically, I want to use this regex pattern for the path [A-Za-z0-9]{6}.
"pets": {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Pets Schema",
"type": "string",
"pattern": "[A-Za-z0-9]{6}"
}
I see that there is a related question here, I wonder whether there is a solution available for this.

how to remove outer array in django rest json response in readonlymodelview

I getting data in array like
[
{
"category_id": "Glass_Door_Handle",
"category_name": "Glass Door Handle",
"product_name": [
{
"product_id": "SP-001",
"name": "RENUALT-SOLID-MD",
"image": "http://127.0.0.1:8000/media/1-1_aIzfcnG.jpg",
"size": [
"http://127.0.0.1:8000/api/sizemattcp/7/"
],
"timestamp": "2016-01-14T05:33:44.107117Z",
"updated": "2016-01-14T05:33:44.107142Z"
}
]
}
]
I want to data in
{
"category_id": "Glass_Door_Handle",
"category_name": "Glass Door Handle",
"product_name": [
{
"product_id": "SP-001",
"name": "RENUALT-SOLID-MD",
}
]
}
I am using readonlyViewModel
It seems you are calling your api like:
/api/models/?filter=value
And it returns your a list of objects, which contains only one element. To get a single object, just append its primary key to the url:
/api/models/1234/
If you want to get models not by id but by some other field, use the ViewSet.lookup_field parameter to specify the name of that field.

How do you handle large relationship data attributes and compound documents?

If an article has several comments (think thousands over time). Should data.relationships.comments return with a limit?
{
"data": [
{
"type": "articles",
"id": 1,
"attributes": {
"title": "Some title",
},
"relationships": {
"comments": {
"links": {
"related": "https://www.foo.com/api/v1/articles/1/comments"
},
"data": [
{ "type": "comment", "id": "1" }
...
{ "type": "comment", "id": "2000" }
]
}
}
}
],
"included": [
{
"type": "comments",
"id": 1,
"attributes": {
"body": "Lorem ipusm",
}
},
.....
{
"type": "comments",
"id": 2000,
"attributes": {
"body": "Lorem ipusm",
}
},
]
}
This starts to feel concerning, when you think of compound documents (http://jsonapi.org/format/#document-compound-documents). Which means, the included section will list all comments as well, making the JSON payload quite large.
If you want to limit the number of records you get at a time from a long list use pagination (JSON API spec).
I would load the comments separately with store.query (ember docs), like so -
store.query('comments', { author_id: <author_id>, page: 3 });
which will return the relevant subset of comments.
If you don't initially want to make two requests per author, you could include the first 'page' in the authors request as you're doing now.
You may also want to look into an addon like Ember Infinity (untested), which will provide an infinite scrolling list and automatically make pagination requests.