Google Endpoints GET请求网址参数[Python]

时间:2018-04-11 12:58:05

标签: python python-2.7 google-app-engine google-cloud-endpoints google-cloud-endpoints-v2

我目前正在开发一个小项目,我需要编写一个GET处理程序。我正在使用端点文档中提供的Echo示例。

我有我的资源容器:

GET_EMAIL_RESOURCE = endpoints.ResourceContainer(
    message_types.VoidMessage,
    i = messages.IntegerField(1, default = 1)
)

我有我的经纪人:

@endpoints.method(
    GET_EMAIL_RESOURCE,
    EchoResponse,
    path='echo/getEmails/{i}',
    http_method='GET',
    name='echo_get_emails'
)
def echo_get_emails(self, request):
    if (request.i == 1):
        out = "default"
    else:
        out = "a"*request.i
    return EchoResponse(content=out)

要访问它,我使用的是curl:

curl --request GET --header "Content-Type: application/json" http://localhost:8080/_ah/api/echo/v1/echo/getEmails/3

现在这样可以正常工作并返回您的期望,但是如果我需要将更多信息编码到我的URL中,例如:

getEmails/value1=someValue&value2=someOtherValue

我无法弄清楚如何做到这一点,我无法在文档中找到解释或示例。

任何帮助都将不胜感激。

编辑1:

我的代码现在看起来像这样:

# main.py

GET_EMAIL_RESOURCE = endpoints.ResourceContainer(
    message_types.VoidMessage,
    i = messages.IntegerField(1, default = 1)
)

@endpoints.method(
    GET_EMAIL_RESOURCE,
    EchoResponse,
    path='echo/getEmails',
    http_method='GET',
    name='echo_get_emails'
)
def echo_get_emails(self, request):
    out = str(request.trailingDigits) + " : " +str(request.leadingDigits)
    return EchoResponse(content = out)

并在我的路径下的openapi.json中:

"/echo/v1/echo/getEmails" : {
  "get": {
    "operationId": "EchoApi_getEmails",
    "parameters" : [
      {
        "name": "trailingDigits",
        "in": "query",
        "description": "Trailing digits",
        "required": true,
        "type": "string"
      },
      {
        "name": "leadingDigits",
        "in": "query",
        "description": "Leading digits",
        "required": true,
        "type": "string"
      }
    ]
  }
}

然而,当我运行请求时     curl --request GET --header" Content-Type:application / json" http://localhost:8080/_ah/api/echo/v1/echo/getEmails?trailingDigits=2&leadingDigits=2

我收回了trailingDigits的内容,但却是leadingDigits的默认值

3 个答案:

答案 0 :(得分:0)

private void ImportDataGridViewDataToExcelSheet() { string userID = WindowsIdentity.GetCurrent().Name; var userName = userID.Remove(0, 15); //test.Text = userName; //dataGridView_ShowAllData Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; Int16 i, j; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); for (i = 0; i <= dataGridView_ShowAllData.RowCount - 2; i++) { for (j = 0; j <= dataGridView_ShowAllData.ColumnCount - 1; j++) { xlWorkSheet.Cells[i + 1, j + 1] = dataGridView_ShowAllData[j, i].Value.ToString(); } } xlWorkBook.SaveAs(@"C:\Users\" + userName + "\\Downloads\\ReportView.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); } finally { GC.Collect(); } } private void toolStripMenuItem_ImportDGVToExcel_Click(object sender, EventArgs e) { ImportDataGridViewDataToExcelSheet(); } 值中未使用的ResourceContainer中的任何字段都应成为查询参数。

答案 1 :(得分:0)

要修改参数,您需要为所需路径配置openapi.json文件。这是一个例子:

"/echo/v1/echo/test": {
      "get": {
        "operationId": "test",
        "parameters": [
          {
            "name": "paramOne",
            "in": "query",
            "description": "First parameter test",
            "required": true,
            "type": "string",
          },
          {
            "name": "paramTwo",
            "in": "query",
            "description": "Second parameter test",
            "required": true,
            "type": "string",
          }
         ]

Here一些有用的文档。

编辑1:

在openapi.json文件中编辑params之后,需要调整Python代码以使用这些新的params。

在这种情况下,您需要调整ResourceContainer以接受第二个参数。

以下是一个例子:

YOUR_RESOURCE_CONTAINER = endpoints.ResourceContainer(
message_types.VoidMessage,
parameter1=messages.IntegerField(1, default=1),
parameter2=messages.IntegerField(2, default=2))

要记住一件事:使用curl时记得添加&#34;&#34;到网址。不使用它可能会导致呼叫失灵。

答案 2 :(得分:0)

Google论坛的回答为我解决了这个问题:

  

您是否在curl命令中使用URL周围的引号?因为如果   不是,而且你是一个bash shell(如果你是这样的话很有可能   使用curl),你可能实际上并不是发送&amp; ends_at = THEEND   部分,因为&amp;是一个shell元字符,用于将命令放入   背景

修改

链接到论坛中的答案 https://groups.google.com/forum/#!msg/google-cloud-endpoints/uzFcUS_uNz0/xyG2qVxbBQAJ