api.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 GENERATION 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 arguments == 2:
  133. # ------- BOARDS LIST START -----------
  134. userid = sys.argv[2]
  135. boards = users + s + userid + s + bs
  136. if sys.argv[1] == 'boards':
  137. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  138. #post_data = {'userId': '{}'.format(userid)}
  139. body = requests.get(boards, headers=headers)
  140. print("=== BOARDS ===\n")
  141. data2 = body.text.replace('}',"}\n")
  142. print(data2)
  143. # ------- BOARDS LIST END -----------
  144. if sys.argv[1] == 'board':
  145. # ------- BOARD INFO START -----------
  146. boardid = sys.argv[2]
  147. board = wekanurl + apiboards + boardid
  148. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  149. body = requests.get(board, headers=headers)
  150. print("=== BOARD ===\n")
  151. data2 = body.text.replace('}',"}\n")
  152. print(data2)
  153. # ------- BOARD INFO END -----------
  154. if sys.argv[1] == 'swimlanes':
  155. boardid = sys.argv[2]
  156. swimlanes = wekanurl + apiboards + boardid + s + sws
  157. # ------- SWIMLANES OF BOARD START -----------
  158. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  159. print("=== SWIMLANES ===\n")
  160. body = requests.get(swimlanes, headers=headers)
  161. data2 = body.text.replace('}',"}\n")
  162. print(data2)
  163. # ------- SWIMLANES OF BOARD END -----------
  164. if sys.argv[1] == 'lists':
  165. # ------- LISTS OF BOARD START -----------
  166. boardid = sys.argv[2]
  167. attachments = wekanurl + apiboards + boardid
  168. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  169. print("=== LISTS ===\n")
  170. body = requests.get(lists, headers=headers)
  171. data2 = body.text.replace('}',"}\n")
  172. print(data2)
  173. # ------- LISTS OF BOARD END -----------
  174. if sys.argv[1] == 'listattachments':
  175. # ------- LISTS OF ATTACHMENTS START -----------
  176. boardid = sys.argv[2]
  177. listattachments = wekanurl + apiboards + boardid + s + ats
  178. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  179. print("=== LIST OF ATTACHMENTS ===\n")
  180. body = requests.get(listattachments, headers=headers)
  181. data2 = body.text.replace('}',"}\n")
  182. print(data2)
  183. # ------- LISTS OF ATTACHMENTS END -----------
  184. if arguments == 1:
  185. if sys.argv[1] == 'users':
  186. # ------- LIST OF USERS START -----------
  187. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  188. print(users)
  189. print("=== USERS ===\n")
  190. body = requests.get(users, headers=headers)
  191. data2 = body.text.replace('}',"}\n")
  192. print(data2)
  193. # ------- LIST OF USERS END -----------