如何使用 appsync httpdatasource 访问 logz api 搜索

How to access to logz api search using appsync httpdatasource

我正在尝试使用他们提供给我的 api-search 访问我存储在 logz.io 上的日志。

实际上,我可以使用curl命令成功访问,如下所示:

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"}}}}', 

正如https://github.com/logzio/public-api/tree/master/search所说。

但是,当我使用 AWS AppSync api 时,使用带参数的 HttpResolver 数据源 name:HttpDataSourceTest、type:HTTP 和端点:https://api.logz.io/v1/search,我定义了我的 schema.grapqhl,请求和响应模板解析器:

schema.grapgql

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

请求模板解析器:

{
    "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": "/"
}  

响应模板解析器:

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

经过几次尝试和失败后,我保持非常简单,只需在查询中询问 TimeStamp 字段并显示状态,所有内容都作为响应返回。

完成所有这些配置后,我得到了这个响应:

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

当我跳过 X-API-TOKEN 参数 header 时结果相同,就像 HttpDatasource 不发送该参数一样。 我是使用所有这些技术、AWS 服务和 Logz.io 的新手,如果我在某些地方遗漏了什么,请告诉我。

单个 http 数据源可用于我的许多解析器并命中相对于同一根的不同路径。出于这个原因,当您设置 HTTP 数据源时,将端点设置为 https://api.logz.io,然后将其用于您的请求映射模板:

{
    "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"
        }
    }
}