1-upload.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Zenodo.
  4. # Copyright (C) 2015, 2016 CERN.
  5. #
  6. # Zenodo is free software; you can redistribute it
  7. # and/or modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # Zenodo is distributed in the hope that it will be
  12. # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Zenodo; if not, write to the
  18. # Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. # MA 02111-1307, USA.
  20. #
  21. # In applying this license, CERN does not
  22. # waive the privileges and immunities granted to it by virtue of its status
  23. # as an Intergovernmental Organization or submit itself to any jurisdiction.
  24. """Simple script to make an upload via the REST API."""
  25. from __future__ import absolute_import, print_function, unicode_literals
  26. import json
  27. from time import sleep
  28. import requests
  29. from six import BytesIO
  30. def upload(token, metadata, files, publish=True):
  31. """Make an upload."""
  32. base_url = 'http://localhost:5000/api/deposit/depositions'
  33. auth = {
  34. 'Authorization': 'Bearer {0}'.format(token)
  35. }
  36. auth_json = {
  37. 'Content-Type': 'application/json',
  38. 'Accept': 'application/json',
  39. }
  40. auth_json.update(auth)
  41. r = requests.post(base_url, data='{}', headers=auth_json)
  42. assert r.status_code == 201
  43. links = r.json()['links']
  44. print('Create deposit:')
  45. print(r.json())
  46. # Wait for ES to index.
  47. sleep(1)
  48. for filename, stream in files:
  49. r = requests.post(
  50. links['files'],
  51. data=dict(filename=filename),
  52. files=dict(file=stream),
  53. headers=auth)
  54. assert r.status_code == 201
  55. print('Upload file:')
  56. print(r.json())
  57. r = requests.put(
  58. links['self'],
  59. data=json.dumps(dict(metadata=metadata)),
  60. headers=auth_json
  61. )
  62. assert r.status_code == 200
  63. print('Update metadata:')
  64. print(r.json())
  65. if publish:
  66. r = requests.post(links['publish'], headers=auth)
  67. assert r.status_code == 202
  68. print('Publish:')
  69. print(r.json())
  70. return r.json()['id']
  71. def upload_test(token, publish=True):
  72. """Test upload."""
  73. metadata = {
  74. 'title': 'My first upload',
  75. 'upload_type': 'publication',
  76. 'publication_type': 'book',
  77. 'description': 'This is my first upload',
  78. 'access_right': 'open',
  79. 'license': 'cc-by',
  80. 'creators': [{'name': 'Doe, John', 'affiliation': 'Zenodo'}]
  81. }
  82. files = [('test.txt', BytesIO(b'My first test upload.'))]
  83. return upload(token, metadata, files, publish=publish)