json – API: How to use Batch method to patch a group of records in Python

  Kiến thức lập trình

I am trying to use Batch API to lock (or unlock) a group parts for a specified project. The batch portion does not work. Please help to check if I build the batch in a wrong way.

Reading parts from the project ( beginning and ending portions) works well.
I tried different ways in the batch portion with no luck. Got 500 “An unexpected server error has occurred”.

Here is my code:

importrequests
importjson
auth=('**************', '******************')
base='https://api.polydyne.com/v1'
project=109
lock=True

res=requests.get('{}/projects/{}/award_parts'.format(base, project), auth=auth)
parts=json.loads(res.text)
print('Before:')
forpartinparts:
    print('t', part['partId'], part['locked'])

batch= []
forpartinparts:
    req= {
            "method": 'PATCH',
            "resource": '/projects/{}/award_parts/{}'.format(project, part['partId']),
            "body": {
                "locked": lock
            }
          }
    batch.append(req)    

res=requests.post('{}/batch'.format(base), data=json.dumps(batch), auth=auth, headers={'Content-Type': 'application/json'})

res=requests.get('{}/projects/{}/award_parts'.format(base, project), auth=auth)
parts=json.loads(res.text)
print('After:')
forpartinparts:
    print('t', part['partId'], part['locked'])

New contributor

Kreation is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT