How to access to logz api search using appsync httpdatasource

474 views Asked by At

I'am trying to access to my logs stored on logz.io using api-search that they offer me.

Actually, I can access successfully using curl command as I show:

curl -X POST 'https://api.logz.io/v1/search'  
--header "X-API-TOKEN: API-TOKEN-GENERATED" 
--header "Content-Type: application/json" 
-d '{"query": {"term": {"_id": {"value": "Log Id Here"}}}}', 

just like https://github.com/logzio/public-api/tree/master/search said.

However, when I use AWS AppSync api, using HttpResolver datasource with params name:HttpDataSourceTest, type:HTTP and endpoint:https://api.logz.io/v1/search, I defined my schema.grapqhl, the request and response template resolvers:

schema.grapgql

type Query {
    GetLog(id: String): Log
} 

Request Template Resolver:

{
    "version": "2018-05-29",
    "method": "POST",
    "params": {
        "headers": {
            "Content-Type: application/json",
            "X-API-TOKEN":"API-TOKEN-GENERATED"
        },
    "body":{
        "query": {
            "term": {
                "_id": {
                    "value": "$context.arguments.id"
                }
             }
         }
    }
    },
    "resourcePath": "/"
}  

Response Template Resolver:

$utils.toJson({"TimeStamp":"$ctx.result.statusCode $ctx.result.body" })

After several attemps and fails I kept very simple, just ask for TimeStamp field on query and show status and all returned in response.

After all this configurations i get this response:

{
    "data": {
        "GetLog": {
            "TimeStamp": "403 {\"message\":\"Forbidden\"}"
        }
    }
}

The same result when I skip X-API-TOKEN param header, its like HttpDatasource dont send that params. I am new using all this techs, AWS Services and Logz.io, please tell me if I'm omitting something in some place.

1

There are 1 answers

1
mparis On BEST ANSWER

A single http data source can be used my many resolvers and hit different paths relative to that same root. For this reason when you setup your HTTP data source set the endpoint to https://api.logz.io and then use this for your request mapping template:

{
    "version": "2018-05-29",
    "method": "POST",
    ## E.G. if full path is https://api.xxxxxxxxx.com/posts then resourcePath would be /posts **
    "resourcePath": "/v1/search",
    "params":{
        "body":{
          "query": {
            "term": {
              "_id": {
                "value": "$context.arguments.id"
              }
            }
          }
        },
        "headers":{
            "Content-Type": "application/json",
            "X-API-TOKEN":"API-TOKEN-GENERATED"
        }
    }
}