Netflix的精灵API要么采用JSON或者多一个部分request
中application/json
和任意数量的attachment
零件application/octet-stream
。
请求使普通的JSON POST非常简单:
requests.post(
url=self.host + self.endpoint,
json={
"version" : "1.0",
"user" : "genie",
"name" : "List * ... Directories bash job",
"description" : "Genie 3 Test Job",
"configs" : [ "/home/travis/build/Netflix/genie/genie-web/build/resources/test/com/netflix/genie/web/controllers/JobRestControllerIntegrationTests/job/config1" ],
"dependencies" : [ "/home/travis/build/Netflix/genie/genie-web/build/resources/test/com/netflix/genie/web/controllers/JobRestControllerIntegrationTests/job/dep1" ],
"setupFile" : "/home/travis/build/Netflix/genie/genie-web/build/resources/test/com/netflix/genie/web/controllers/JobRestControllerIntegrationTests/job/jobsetupfile",
"commandArgs" : "-c 'echo hello world'",
"clusterCriterias" : [ {
"tags" : [ "localhost" ]
} ],
"commandCriteria" : [ "bash" ],
},
)
该命令是受限制的,因此,如果要发送大命令(查询),则最好使用附件。
使用请求,请求多部分也不难:
requests.post(
url=self.host + self.endpoint,
json={
"version" : "1.0",
"user" : "genie",
"name" : "List * ... Directories bash job",
"description" : "Genie 3 Test Job",
"configs" : [ "/home/travis/build/Netflix/genie/genie-web/build/resources/test/com/netflix/genie/web/controllers/JobRestControllerIntegrationTests/job/config1" ],
"dependencies" : [ "/home/travis/build/Netflix/genie/genie-web/build/resources/test/com/netflix/genie/web/controllers/JobRestControllerIntegrationTests/job/dep1" ],
"setupFile" : "/home/travis/build/Netflix/genie/genie-web/build/resources/test/com/netflix/genie/web/controllers/JobRestControllerIntegrationTests/job/jobsetupfile",
"commandArgs" : "-c 'cat query.sql'",
"clusterCriterias" : [ {
"tags" : [ "localhost" ]
} ],
"commandCriteria" : [ "bash" ],
},
files={
"attachment": (
'query.sql',
'select count(1) from small_table;',
'application/octet-stream'
),
},
)
除非files
存在,否则它将忽略json
,如果我更改json
为data
表单。我可以将JSON dict移到files
dict上,但是它似乎没有作为JSON处理,现在我需要使用该包对其进行编码?
我问是因为requests
在参数和响应对象中处理json时,我怀疑它也会在多部分表单的某个地方处理它,否则我json
只是为了json.dumps(...)
另外:
attachment
,如果您需要多个附件,api将允许/期望该名称。[正如我的评论,可以通过更改files
为列表名称与文件对的列表来完成]。Content-Disposition: form-data; name=request
,Content-Disposition: form-data; name=attachment
而请求包似乎会生成Content-Disposition: form-data; name="attachment"
。“我可以将JSON dict移到文件dict中,但它似乎没有作为JSON处理”
- 您可以将字典转储到磁盘上的JSON文件中。
tempfile.TemporaryFile
可以使用。转储,请求,清理并重复
“否则,我只为json.dumps(...)引入json”
- 如果需要保留dict并在运行期间建立请求(可以忽略此用例的1),则可以这样做。但是,请记住将转储转换为io.BytesIO对象,以便请求可以计算内容长度标头。
另外,请记住将文件的内容类型传递为“ application / octet-stream”而不是“ plain / text”
“示例请求显示零件标题具有未引用的名称”
我认为这不重要。RFC 2183文档指出,长度<78但包含tspecials的参数值应表示为带引号的字符串。
尽管name参数的值不包含tspecials,但对于短值IMO而言,这是更健壮的处理方式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句