api.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #! /usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # vi:ts=4:et
  4. # Wekan API Python CLI, originally from here, where is more details:
  5. # https://github.com/wekan/wekan/wiki/New-card-with-Python3-and-REST-API
  6. try:
  7. # python 3
  8. from urllib.parse import urlencode
  9. except ImportError:
  10. # python 2
  11. from urllib import urlencode
  12. import json
  13. import requests
  14. import sys
  15. arguments = len(sys.argv) - 1
  16. if arguments == 0:
  17. print("=== Wekan API Python CLI: Shows IDs for addcard ===")
  18. print("AUTHORID is USERID that writes card.")
  19. print("If *nix: chmod +x api.py => ./api.py users")
  20. print("Syntax:")
  21. print(" python3 api.py users # All users")
  22. print(" python3 api.py boards USERID # Boards of USERID")
  23. print(" python3 api.py board BOARDID # Info of BOARDID")
  24. print(" python3 api.py swimlanes BOARDID # Swimlanes of BOARDID")
  25. print(" python3 api.py lists BOARDID # Lists of BOARDID")
  26. print(" python3 api.py createlist BOARDID LISTTITLE # Create list")
  27. print(" python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION")
  28. print(" python3 api.py listattachments BOARDID # List attachments")
  29. # TODO:
  30. # print(" python3 api.py attachmentdownload BOARDID ATTACHMENTID # One attachment as file")
  31. # print(" python3 api.py attachmentsdownload BOARDID # All attachments as files")
  32. exit
  33. # ------- SETTINGS START -------------
  34. # Username is your Wekan username or email address.
  35. # OIDC/OAuth2 etc uses email address as username.
  36. username = 'testtest'
  37. password = 'testtest'
  38. wekanurl = 'http://localhost:4000/'
  39. # ------- SETTINGS END -------------
  40. """
  41. EXAMPLE:
  42. python3 api.py
  43. OR:
  44. chmod +x api.py
  45. ./api.py
  46. === Wekan API Python CLI: Shows IDs for addcard ===
  47. AUTHORID is USERID that writes card.
  48. Syntax:
  49. python3 api.py users # All users
  50. python3 api.py boards USERID # Boards of USERID
  51. python3 api.py board BOARDID # Info of BOARDID
  52. python3 api.py swimlanes BOARDID # Swimlanes of BOARDID
  53. python3 api.py lists BOARDID # Lists of BOARDID
  54. python3 api.py createlist BOARDID LISTTITLE # Create list
  55. python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  56. python3 api.py listattachments BOARDID # List attachments
  57. python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64
  58. python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file
  59. === USERS ===
  60. python3 api.py users
  61. => abcd1234
  62. === BOARDS ===
  63. python3 api.py boards abcd1234
  64. === SWIMLANES ===
  65. python3 api.py swimlanes dYZ
  66. [{"_id":"Jiv","title":"Default"}
  67. ]
  68. === LISTS ===
  69. python3 api.py lists dYZ
  70. []
  71. There is no lists, so create a list:
  72. === CREATE LIST ===
  73. python3 api.py createlist dYZ 'Test'
  74. {"_id":"7Kp"}
  75. # python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  76. python3 api.py addcard ppg dYZ Jiv 7Kp 'Test card' 'Test description'
  77. === LIST ATTACHMENTS WITH DOWNLOAD URLs ====
  78. python3 api.py listattachments BOARDID
  79. """
  80. # ------- API URL GENERATION START -----------
  81. loginurl = 'users/login'
  82. wekanloginurl = wekanurl + loginurl
  83. apiboards = 'api/boards/'
  84. apiattachments = 'api/attachments/'
  85. apiusers = 'api/users'
  86. e = 'export'
  87. s = '/'
  88. l = 'lists'
  89. sw = 'swimlane'
  90. sws = 'swimlanes'
  91. cs = 'cards'
  92. bs = 'boards'
  93. atl = 'attachmentslist'
  94. at = 'attachment'
  95. ats = 'attachments'
  96. users = wekanurl + apiusers
  97. # ------- API URL GERENARTION END -----------
  98. # ------- LOGIN TOKEN START -----------
  99. data = {"username": username, "password": password}
  100. body = requests.post(wekanloginurl, data=data)
  101. d = body.json()
  102. apikey = d['token']
  103. # ------- LOGIN TOKEN END -----------
  104. if arguments == 7:
  105. if sys.argv[1] == 'addcard':
  106. # ------- WRITE TO CARD START -----------
  107. authorid = sys.argv[2]
  108. boardid = sys.argv[3]
  109. swimlaneid = sys.argv[4]
  110. listid = sys.argv[5]
  111. cardtitle = sys.argv[6]
  112. carddescription = sys.argv[7]
  113. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  114. # Write to card
  115. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  116. post_data = {'authorId': '{}'.format(authorid), 'title': '{}'.format(cardtitle), 'description': '{}'.format(carddescription), 'swimlaneId': '{}'.format(swimlaneid)}
  117. body = requests.post(cardtolist, data=post_data, headers=headers)
  118. print(body.text)
  119. # ------- WRITE TO CARD END -----------
  120. if arguments == 3:
  121. if sys.argv[1] == 'createlist':
  122. # ------- CREATE LIST START -----------
  123. boardid = sys.argv[2]
  124. listtitle = sys.argv[3]
  125. list = wekanurl + apiboards + boardid + s + l
  126. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  127. post_data = {'title': '{}'.format(listtitle)}
  128. body = requests.post(list, data=post_data, headers=headers)
  129. print("=== CREATE LIST ===\n")
  130. print(body.text)
  131. # ------- CREATE LIST END -----------
  132. if sys.argv[1] == 'attachmentjson':
  133. # ------- ATTACHMENT AS JSON BASE64 START -----------
  134. boardid = sys.argv[2]
  135. attachmentid = sys.argv[3]
  136. attachmentjson = wekanurl + apiboards + boardid + s + ats + s + attachmentid
  137. print(attachmentjson)
  138. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  139. print("=== ATTACHMENT AS JSON BASE64 ===\n")
  140. body = requests.get(attachmentjson, headers=headers)
  141. data2 = body.text.replace('}',"}\n")
  142. print(data2)
  143. # ------- ATTACHMENT AS JSON BASE64 END -----------
  144. if arguments == 2:
  145. # ------- BOARDS LIST START -----------
  146. userid = sys.argv[2]
  147. boards = users + s + userid + s + bs
  148. if sys.argv[1] == 'boards':
  149. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  150. #post_data = {'userId': '{}'.format(userid)}
  151. body = requests.get(boards, headers=headers)
  152. print("=== BOARDS ===\n")
  153. data2 = body.text.replace('}',"}\n")
  154. print(data2)
  155. # ------- BOARDS LIST END -----------
  156. if sys.argv[1] == 'board':
  157. # ------- BOARD INFO START -----------
  158. boardid = sys.argv[2]
  159. board = wekanurl + apiboards + boardid
  160. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  161. body = requests.get(board, headers=headers)
  162. print("=== BOARD ===\n")
  163. data2 = body.text.replace('}',"}\n")
  164. print(data2)
  165. # ------- BOARD INFO END -----------
  166. if sys.argv[1] == 'swimlanes':
  167. boardid = sys.argv[2]
  168. swimlanes = wekanurl + apiboards + boardid + s + sws
  169. # ------- SWIMLANES OF BOARD START -----------
  170. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  171. print("=== SWIMLANES ===\n")
  172. body = requests.get(swimlanes, headers=headers)
  173. data2 = body.text.replace('}',"}\n")
  174. print(data2)
  175. # ------- SWIMLANES OF BOARD END -----------
  176. if sys.argv[1] == 'lists':
  177. # ------- LISTS OF BOARD START -----------
  178. boardid = sys.argv[2]
  179. attachments = wekanurl + apiboards + boardid
  180. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  181. print("=== LISTS ===\n")
  182. body = requests.get(lists, headers=headers)
  183. data2 = body.text.replace('}',"}\n")
  184. print(data2)
  185. # ------- LISTS OF BOARD END -----------
  186. if sys.argv[1] == 'listattachments':
  187. # ------- LISTS OF ATTACHMENTS START -----------
  188. boardid = sys.argv[2]
  189. listattachments = wekanurl + apiboards + boardid + s + ats
  190. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  191. print("=== LIST OF ATTACHMENTS ===\n")
  192. body = requests.get(listattachments, headers=headers)
  193. data2 = body.text.replace('}',"}\n")
  194. print(data2)
  195. # ------- LISTS OF ATTACHMENTS END -----------
  196. if arguments == 1:
  197. if sys.argv[1] == 'users':
  198. # ------- LIST OF USERS START -----------
  199. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  200. print(users)
  201. print("=== USERS ===\n")
  202. body = requests.get(users, headers=headers)
  203. data2 = body.text.replace('}',"}\n")
  204. print(data2)
  205. # ------- LIST OF USERS END -----------