Python例子

示例:创建手机客户端烧伤

代码

import json
import requests
import pprint

pp = pprint.PrettyPrinter(indent=4)

url = "//api.sanyili.com/api/background_burns"

api_key = "your_api_key"
user_id = "your_user_id"

payload = {
  "url": "//bonanzapublic.s3.amazonaws.com/images/background_burner/colorful_bag.jpg",
  "key": api_key,
  "user_id": user_id,
}

create_response = requests.post(url, data=payload)
create_result = create_response.json()
pp.pprint(create_result)

数据

上面最终线的结果将是:

{   u'access_token': u'abc',
    u'action': u'create',
    u'burn_user_id': 1,
    u'complete': False,
    u'final_result_url': None,
    u'id': 5141,
    u'masks': [],
    u'message': u'Successfully queued',
    u'position_in_queue': 27,
    u'review_complete': False,
    u'selected_mask_id': None,
    u'source_image_url': u'http://bonanzaimagestest.s3.amazonaws.com/uploads/burnees/1477070571-12394-0024-0318.jpg',
    u'url': u'//burner.sanyili.com/background_burns/5141',
    u'user_id': u'1'}

样本:显示手机客户端燃烧

构建上面的代码以创建手机客户端刻录,您可以从创建结果中提取Burn ID:

burn_id = create_result["id"]

使用此ID,您可以获得手机客户端刻录的当前状态:

show_url = "{url}/{burn_id}?key={api_key}&user_id={user_id}".format(**locals())
show_response = requests.get(show_url)
pp.pprint(show_response.json())

数据

打印的结果是:

{   u'access_token': u'abc',
    u'action': u'show',
    u'burn_user_id': 1,
    u'complete': False,
    u'final_result_url': None,
    u'id': 5141,
    u'masks': [],
    u'message': u'Found existing burn for user 18 with id: 5141',
    u'position_in_queue': 27,
    u'review_complete': False,
    u'selected_mask_id': None,
    u'source_image_url': u'http://bonanzaimagestest.s3.amazonaws.com/uploads/burnees/1477070571-12394-0024-0318.jpg',
    u'url': u'//burner.sanyili.com/background_burns/5141',
    u'user_id': u'1'}

样本:删除手机客户端燃烧

归档(有效地删除)手机客户端刻录,在上一示例中构建:

delete_url = "{url}/{burn_id}?key={api_key}&user_id={user_id}".format(**locals())
delete_response = requests.delete(delete_url)
pp.pprint(delete_response.json())

数据

上面打印删除_Response的结果将是:

{   u'access_token': u'abc',
    u'action': u'destroy',
    u'burn_user_id': 1,
    u'complete': False,
    u'final_result_url': None,
    u'id': 5141,
    u'masks': [],
    u'message': u'Archived burn with id: 5141',
    u'position_in_queue': 27,
    u'review_complete': False,
    u'selected_mask_id': None,
    u'source_image_url': u'http://bonanzaimagestest.s3.amazonaws.com/uploads/burnees/1477070571-12394-0024-0318.jpg',
    u'url': u'//burner.sanyili.com/background_burns/5141',
    u'user_id': u'1'}

样本:列出手机客户端烧伤

代码

要获取所提交的手机客户端刻录列表:

index_url = "{url}?key={api_key}&user_id={user_id}".format(**locals())
index_response = requests.get(index_url)
pp.pprint(index_response.json())

数据

上述打印索引_Response的结果将是:

{   u'access_token': u'abc',
    u'action': u'index',
    u'background_burns': [   {   u'complete': False,
                                 u'final_result_url': None,
                                 u'id': 5139,
                                 u'masks': [],
                                 u'position_in_queue': 25,
                                 u'review_complete': False,
                                 u'selected_mask_id': None,
                                 u'source_image_url': u'http://bonanzaimagestest.s3.amazonaws.com/uploads/burnees/1477070268-12394-0022-1662.jpg',
                                 u'url': u'//burner.sanyili.com/background_burns/5139'},
                             {   u'complete': False,
                                 u'final_result_url': None,
                                 u'id': 5138,
                                 u'masks': [],
                                 u'position_in_queue': 24,
                                 u'review_complete': False,
                                 u'selected_mask_id': None,
                                 u'source_image_url': u'http://bonanzaimagestest.s3.amazonaws.com/uploads/burnees/1477070221-12394-0021-4561.jpg',
                                 u'url': u'//burner.sanyili.com/background_burns/5138'},
    u'burn_user_id': 1,
    u'count': 2,
    u'message': u'Success',
    u'user_id': u'1'}

示例:更新手机客户端刻录

代码

鉴于具有掩码结果的后台刻录,从索引或显示动作返回,例如:

u'masks': [   {   
  u'composite_url': u'//burner.sanyili.com/background_masks/53487714.png?1477071771&composite=true&no_crop=false&transparent=false',
  u'id': 53487714,
  u'quality_score': 506,
  u'url': u'//burner.sanyili.com/background_masks/53487714.png?',
  u'variation': u'flooded'}]

您可以选择最终面具:

update_payload = {
  "key": api_key,
  "user_id": user_id,
  "selected_mask_id": 53487714
}

update_url = "{url}/{burn_id}".format(**locals())
update_response = requests.put(update_url, update_payload)
pp.pprint(update_response.json())

数据

上面的打印update_response的结果是:

{   u'access_token': u'abv',
    u'action': u'update',
    u'burn_user_id': 1,
    u'complete': True,
    u'final_result_url': u'//images.bonanzastatic.com/uploads/burns/16599244.jpg?1477071758',
    u'id': 16599244,
    u'masks': [   {   u'composite_url': u'//burner.sanyili.com/background_masks/53487685.png?1477072100&composite=true&no_crop=false&transparent=false',
                      u'id': 53487685,
                      u'quality_score': 506,
                      u'url': u'//burner.sanyili.com/background_masks/53487685.png?',
                      u'variation': u'flooded'},
                  {   u'composite_url': u'//burner.sanyili.com/background_masks/53487702.png?1477072100&composite=true&no_crop=false&transparent=false',
                      u'id': 53487702,
                      u'quality_score': 495,
                      u'url': u'//burner.sanyili.com/background_masks/53487702.png?',
                      u'variation': u'adjacent-probable'},
                  {   u'composite_url': u'//burner.sanyili.com/background_masks/53487703.png?1477072100&composite=true&no_crop=false&transparent=false',
                      u'id': 53487703,
                      u'quality_score': 481,
                      u'url': u'//burner.sanyili.com/background_masks/53487703.png?',
                      u'variation': u'raw-probable'},
                  {   u'composite_url': u'//burner.sanyili.com/background_masks/53487704.png?1477072100&composite=true&no_crop=false&transparent=false',
                      u'id': 53487704,
                      u'quality_score': 435,
                      u'url': u'//burner.sanyili.com/background_masks/53487704.png?',
                      u'variation': u'side_burn-probable'},
                  {   u'composite_url': u'//burner.sanyili.com/background_masks/53487705.png?1477072100&composite=true&no_crop=false&transparent=false',
                      u'id': 53487705,
                      u'quality_score': 434,
                      u'url': u'//burner.sanyili.com/background_masks/53487705.png?',
                      u'variation': u'bg-probable'}],
    u'message': u'Saved final mask for burn with id: 16599244',
    u'position_in_queue': 0,
    u'review_complete': False,
    u'selected_mask_id': 53487714,
    u'source_image_url': u'//images.bonanzastatic.com/uploads/burnees/1477071734-3877314-0001-7229.jpg',
    u'url': u'//burner.sanyili.com/background_burns/16599244',
    u'user_id': u'1'}