api.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. # TODO:
  7. # addcustomfieldtoboard: There is error: Settings must be object. So adding does not work yet.
  8. try:
  9. # python 3
  10. from urllib.parse import urlencode
  11. except ImportError:
  12. # python 2
  13. from urllib import urlencode
  14. import json
  15. import requests
  16. import sys
  17. arguments = len(sys.argv) - 1
  18. syntax = """=== Wekan API Python CLI: Shows IDs for addcard ===
  19. # AUTHORID is USERID that writes card or custom field.
  20. If *nix: chmod +x api.py => ./api.py users
  21. Syntax:
  22. User API:
  23. python3 api.py user # Current user and list of current user boards
  24. python3 api.py boards USERID # Boards of USERID
  25. python3 api.py swimlanes BOARDID # Swimlanes of BOARDID
  26. python3 api.py lists BOARDID # Lists of BOARDID
  27. python3 api.py list BOARDID LISTID # Info of LISTID
  28. python3 api.py createlist BOARDID LISTTITLE # Create list
  29. python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  30. python3 api.py editcard BOARDID LISTID CARDID NEWCARDTITLE NEWCARDDESCRIPTION
  31. python3 api.py customfields BOARDID # Custom Fields of BOARDID
  32. python3 api.py customfield BOARDID CUSTOMFIELDID # Info of CUSTOMFIELDID
  33. python3 api.py addcustomfieldtoboard AUTHORID BOARDID NAME TYPE SETTINGS SHOWONCARD AUTOMATICALLYONCARD SHOWLABELONMINICARD SHOWSUMATTOPOFLIST # Add Custom Field to Board
  34. python3 api.py editcustomfield BOARDID LISTID CARDID CUSTOMFIELDID NEWCUSTOMFIELDVALUE
  35. python3 api.py listattachments BOARDID # List attachments
  36. python3 api.py cardsbyswimlane BOARDID LISTID
  37. python3 api.py getcard BOARDID LISTID CARDID
  38. python3 api.py addlabel BOARDID LISTID CARDID LABELID
  39. python3 api.py addcardwithlabel AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION LABELIDS
  40. Admin API:
  41. python3 api.py users # All users
  42. python3 api.py boards # All Public Boards
  43. python3 api.py newuser USERNAME EMAIL PASSWORD
  44. """
  45. if arguments == 0:
  46. print(syntax)
  47. exit
  48. # TODO:
  49. # print(" python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64")
  50. # print(" python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file")
  51. # print(" python3 api.py attachmentdownload BOARDID ATTACHMENTID # One attachment as file")
  52. # print(" python3 api.py attachmentsdownload BOARDID # All attachments as files")
  53. # ------- SETTINGS START -------------
  54. # Username is your Wekan username or email address.
  55. # OIDC/OAuth2 etc uses email address as username.
  56. username = 'testtest'
  57. password = 'testtest'
  58. wekanurl = 'http://localhost:4000/'
  59. # ------- SETTINGS END -------------
  60. """
  61. === ADD CUSTOM FIELD TO BOARD ===
  62. Type: text, number, date, dropdown, checkbox, currency, stringtemplate.
  63. python3 api.py addcustomfieldtoboard cmx3gmHLKwAXLqjxz LcDW4QdooAx8hsZh8 "SomeField" "date" "" true true true true
  64. === USERS ===
  65. python3 api.py users
  66. => abcd1234
  67. === BOARDS ===
  68. python3 api.py boards abcd1234
  69. === SWIMLANES ===
  70. python3 api.py swimlanes dYZ
  71. [{"_id":"Jiv","title":"Default"}
  72. ]
  73. === LISTS ===
  74. python3 api.py lists dYZ
  75. []
  76. There is no lists, so create a list:
  77. === CREATE LIST ===
  78. python3 api.py createlist dYZ 'Test'
  79. {"_id":"7Kp"}
  80. # python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  81. python3 api.py addcard ppg dYZ Jiv 7Kp 'Test card' 'Test description'
  82. === LIST ATTACHMENTS WITH DOWNLOAD URLs ====
  83. python3 api.py listattachments BOARDID
  84. """
  85. # ------- API URL GENERATION START -----------
  86. loginurl = 'users/login'
  87. wekanloginurl = wekanurl + loginurl
  88. apiboards = 'api/boards/'
  89. apiattachments = 'api/attachments/'
  90. apiusers = 'api/users'
  91. apiuser = 'api/user'
  92. apiallusers = 'api/allusers'
  93. e = 'export'
  94. s = '/'
  95. l = 'lists'
  96. sw = 'swimlane'
  97. sws = 'swimlanes'
  98. cs = 'cards'
  99. cf = 'custom-fields'
  100. bs = 'boards'
  101. apbs = 'allpublicboards'
  102. atl = 'attachmentslist'
  103. at = 'attachment'
  104. ats = 'attachments'
  105. users = wekanurl + apiusers
  106. user = wekanurl + apiuser
  107. allusers = wekanurl + apiallusers
  108. # ------- API URL GENERATION END -----------
  109. # ------- LOGIN TOKEN START -----------
  110. data = {"username": username, "password": password}
  111. body = requests.post(wekanloginurl, json=data)
  112. d = body.json()
  113. apikey = d['token']
  114. # ------- LOGIN TOKEN END -----------
  115. if arguments == 10:
  116. if sys.argv[1] == 'addcustomfieldtoboard':
  117. # ------- ADD CUSTOM FIELD TO BOARD START -----------
  118. authorid = sys.argv[2]
  119. boardid = sys.argv[3]
  120. name = sys.argv[4]
  121. type1 = sys.argv[5]
  122. settings = str(json.loads(sys.argv[6]))
  123. # There is error: Settings must be object. So this does not work yet.
  124. #settings = {'currencyCode': 'EUR'}
  125. print(type(settings))
  126. showoncard = sys.argv[7]
  127. automaticallyoncard = sys.argv[8]
  128. showlabelonminicard = sys.argv[9]
  129. showsumattopoflist = sys.argv[10]
  130. customfieldtoboard = wekanurl + apiboards + boardid + s + cf
  131. # Add Custom Field to Board
  132. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  133. post_data = {'authorId': '{}'.format(authorid), 'name': '{}'.format(name), 'type': '{}'.format(type1), 'settings': '{}'.format(settings), 'showoncard': '{}'.format(showoncard), 'automaticallyoncard': '{}'.format(automaticallyoncard), 'showlabelonminicard': '{}'.format(showlabelonminicard), 'showsumattopoflist': '{}'.format(showsumattopoflist)}
  134. body = requests.post(customfieldtoboard, data=post_data, headers=headers)
  135. print(body.text)
  136. # ------- ADD CUSTOM FIELD TO BOARD END -----------
  137. if arguments == 8:
  138. if sys.argv[1] == 'addcardwithlabel':
  139. # ------- ADD CARD WITH LABEL START -----------
  140. authorid = sys.argv[2]
  141. boardid = sys.argv[3]
  142. swimlaneid = sys.argv[4]
  143. listid = sys.argv[5]
  144. cardtitle = sys.argv[6]
  145. carddescription = sys.argv[7]
  146. labelIds = sys.argv[8] # Aggiunto labelIds
  147. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  148. # Add card
  149. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  150. post_data = {
  151. 'authorId': '{}'.format(authorid),
  152. 'title': '{}'.format(cardtitle),
  153. 'description': '{}'.format(carddescription),
  154. 'swimlaneId': '{}'.format(swimlaneid),
  155. 'labelIds': labelIds
  156. }
  157. body = requests.post(cardtolist, data=post_data, headers=headers)
  158. print(body.text)
  159. # If ok id card
  160. if body.status_code == 200:
  161. card_data = body.json()
  162. new_card_id = card_data.get('_id')
  163. # Updating card
  164. if new_card_id:
  165. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + new_card_id
  166. put_data = {'labelIds': labelIds}
  167. body = requests.put(edcard, data=put_data, headers=headers)
  168. print("=== EDIT CARD ===\n")
  169. body = requests.get(edcard, headers=headers)
  170. data2 = body.text.replace('}', "}\n")
  171. print(data2)
  172. else:
  173. print("Error obraining ID.")
  174. else:
  175. print("Error adding card.")
  176. # ------- ADD CARD WITH LABEL END -----------
  177. if arguments == 7:
  178. if sys.argv[1] == 'addcard':
  179. # ------- ADD CARD START -----------
  180. authorid = sys.argv[2]
  181. boardid = sys.argv[3]
  182. swimlaneid = sys.argv[4]
  183. listid = sys.argv[5]
  184. cardtitle = sys.argv[6]
  185. carddescription = sys.argv[7]
  186. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  187. # Add card
  188. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  189. post_data = {'authorId': '{}'.format(authorid), 'title': '{}'.format(cardtitle), 'description': '{}'.format(carddescription), 'swimlaneId': '{}'.format(swimlaneid)}
  190. body = requests.post(cardtolist, data=post_data, headers=headers)
  191. print(body.text)
  192. # ------- ADD CARD END -----------
  193. if arguments == 6:
  194. if sys.argv[1] == 'editcard':
  195. # ------- EDIT CARD START -----------
  196. boardid = sys.argv[2]
  197. listid = sys.argv[3]
  198. cardid = sys.argv[4]
  199. newcardtitle = sys.argv[5]
  200. newcarddescription = sys.argv[6]
  201. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  202. print(edcard)
  203. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  204. put_data = {'title': '{}'.format(newcardtitle), 'description': '{}'.format(newcarddescription)}
  205. body = requests.put(edcard, data=put_data, headers=headers)
  206. print("=== EDIT CARD ===\n")
  207. body = requests.get(edcard, headers=headers)
  208. data2 = body.text.replace('}',"}\n")
  209. print(data2)
  210. # ------- EDIT CARD END -----------
  211. if sys.argv[1] == 'editcustomfield':
  212. # ------- EDIT CUSTOMFIELD START -----------
  213. boardid = sys.argv[2]
  214. listid = sys.argv[3]
  215. cardid = sys.argv[4]
  216. customfieldid = sys.argv[5]
  217. newcustomfieldvalue = sys.argv[6]
  218. edfield = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid + s + 'customFields' + s + customfieldid
  219. #print(edfield)
  220. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  221. post_data = {'_id': '{}'.format(customfieldid), 'value': '{}'.format(newcustomfieldvalue)}
  222. #print(post_data)
  223. body = requests.post(edfield, data=post_data, headers=headers)
  224. print("=== EDIT CUSTOMFIELD ===\n")
  225. data2 = body.text.replace('}',"}\n")
  226. print(data2)
  227. # ------- EDIT CUSTOMFIELD END -----------
  228. if arguments == 5:
  229. if sys.argv[1] == 'addlabel':
  230. # ------- EDIT CARD ADD LABEL START -----------
  231. boardid = sys.argv[2]
  232. listid = sys.argv[3]
  233. cardid = sys.argv[4]
  234. labelIds = sys.argv[5]
  235. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  236. print(edcard)
  237. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  238. put_data = {'labelIds': labelIds}
  239. body = requests.put(edcard, data=put_data, headers=headers)
  240. print("=== ADD LABEL ===\n")
  241. body = requests.get(edcard, headers=headers)
  242. data2 = body.text.replace('}',"}\n")
  243. print(data2)
  244. # ------- EDIT CARD ADD LABEL END -----------
  245. if arguments == 4:
  246. if sys.argv[1] == 'newuser':
  247. # ------- CREATE NEW USER START -----------
  248. username = sys.argv[2]
  249. email = sys.argv[3]
  250. password = sys.argv[4]
  251. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  252. post_data = {'username': '{}'.format(username),'email': '{}'.format(email),'password': '{}'.format(password)}
  253. body = requests.post(users, data=post_data, headers=headers)
  254. print("=== CREATE NEW USER ===\n")
  255. print(body.text)
  256. # ------- CREATE NEW USER END -----------
  257. if sys.argv[1] == 'getcard':
  258. # ------- LIST OF CARD START -----------
  259. boardid = sys.argv[2]
  260. listid = sys.argv[3]
  261. cardid = sys.argv[4]
  262. listone = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  263. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  264. print("=== INFO OF ONE LIST ===\n")
  265. print("URL:", listone) # Stampa l'URL per debug
  266. try:
  267. response = requests.get(listone, headers=headers)
  268. print("=== RESPONSE ===\n")
  269. print("Status Code:", response.status_code) # Stampa il codice di stato per debug
  270. if response.status_code == 200:
  271. data2 = response.text.replace('}', "}\n")
  272. print(data2)
  273. else:
  274. print(f"Error: {response.status_code}")
  275. print(f"Response: {response.text}")
  276. except Exception as e:
  277. print(f"Error in the GET request: {e}")
  278. # ------- LISTS OF CARD END -----------
  279. if arguments == 3:
  280. if sys.argv[1] == 'createlist':
  281. # ------- CREATE LIST START -----------
  282. boardid = sys.argv[2]
  283. listtitle = sys.argv[3]
  284. list = wekanurl + apiboards + boardid + s + l
  285. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  286. post_data = {'title': '{}'.format(listtitle)}
  287. body = requests.post(list, data=post_data, headers=headers)
  288. print("=== CREATE LIST ===\n")
  289. print(body.text)
  290. # ------- CREATE LIST END -----------
  291. if sys.argv[1] == 'list':
  292. # ------- LIST OF BOARD START -----------
  293. boardid = sys.argv[2]
  294. listid = sys.argv[3]
  295. listone = wekanurl + apiboards + boardid + s + l + s + listid
  296. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  297. print("=== INFO OF ONE LIST ===\n")
  298. body = requests.get(listone, headers=headers)
  299. data2 = body.text.replace('}',"}\n")
  300. print(data2)
  301. # ------- LISTS OF BOARD END -----------
  302. if sys.argv[1] == 'customfield':
  303. # ------- INFO OF CUSTOM FIELD START -----------
  304. boardid = sys.argv[2]
  305. customfieldid = sys.argv[3]
  306. customfieldone = wekanurl + apiboards + boardid + s + cf + s + customfieldid
  307. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  308. print("=== INFO OF ONE CUSTOM FIELD ===\n")
  309. body = requests.get(customfieldone, headers=headers)
  310. data2 = body.text.replace('}',"}\n")
  311. print(data2)
  312. # ------- INFO OF CUSTOM FIELD END -----------
  313. if sys.argv[1] == 'cardsbyswimlane':
  314. # ------- RETRIEVE CARDS BY SWIMLANE ID START -----------
  315. boardid = sys.argv[2]
  316. swimlaneid = sys.argv[3]
  317. cardsbyswimlane = wekanurl + apiboards + boardid + s + sws + s + swimlaneid + s + cs
  318. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  319. print("=== CARDS BY SWIMLANE ID ===\n")
  320. print("URL:", cardsbyswimlane) # Debug
  321. try:
  322. body = requests.get(cardsbyswimlane, headers=headers)
  323. print("Status Code:", body.status_code) # Debug
  324. data = body.text.replace('}', "}\n")
  325. print("Data:", data)
  326. except Exception as e:
  327. print("Error GET:", e)
  328. # ------- RETRIEVE CARDS BY SWIMLANE ID END -----------
  329. if arguments == 2:
  330. # ------- BOARDS LIST START -----------
  331. userid = sys.argv[2]
  332. boards = users + s + userid + s + bs
  333. if sys.argv[1] == 'boards':
  334. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  335. #post_data = {'userId': '{}'.format(userid)}
  336. body = requests.get(boards, headers=headers)
  337. print("=== BOARDS ===\n")
  338. data2 = body.text.replace('}',"}\n")
  339. print(data2)
  340. # ------- BOARDS LIST END -----------
  341. if sys.argv[1] == 'board':
  342. # ------- BOARD INFO START -----------
  343. boardid = sys.argv[2]
  344. board = wekanurl + apiboards + boardid
  345. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  346. body = requests.get(board, headers=headers)
  347. print("=== BOARD ===\n")
  348. data2 = body.text.replace('}',"}\n")
  349. print(data2)
  350. # ------- BOARD INFO END -----------
  351. if sys.argv[1] == 'customfields':
  352. # ------- CUSTOM FIELDS OF BOARD START -----------
  353. boardid = sys.argv[2]
  354. boardcustomfields = wekanurl + apiboards + boardid + s + cf
  355. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  356. body = requests.get(boardcustomfields, headers=headers)
  357. print("=== CUSTOM FIELDS OF BOARD ===\n")
  358. data2 = body.text.replace('}',"}\n")
  359. print(data2)
  360. # ------- CUSTOM FIELDS OF BOARD END -----------
  361. if sys.argv[1] == 'swimlanes':
  362. boardid = sys.argv[2]
  363. swimlanes = wekanurl + apiboards + boardid + s + sws
  364. # ------- SWIMLANES OF BOARD START -----------
  365. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  366. print("=== SWIMLANES ===\n")
  367. body = requests.get(swimlanes, headers=headers)
  368. data2 = body.text.replace('}',"}\n")
  369. print(data2)
  370. # ------- SWIMLANES OF BOARD END -----------
  371. if sys.argv[1] == 'lists':
  372. # ------- LISTS OF BOARD START -----------
  373. boardid = sys.argv[2]
  374. lists = wekanurl + apiboards + boardid + s + l
  375. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  376. print("=== LISTS ===\n")
  377. body = requests.get(lists, headers=headers)
  378. data2 = body.text.replace('}',"}\n")
  379. print(data2)
  380. # ------- LISTS OF BOARD END -----------
  381. if sys.argv[1] == 'listattachments':
  382. # ------- LISTS OF ATTACHMENTS START -----------
  383. boardid = sys.argv[2]
  384. listattachments = wekanurl + apiboards + boardid + s + ats
  385. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  386. print("=== LIST OF ATTACHMENTS ===\n")
  387. body = requests.get(listattachments, headers=headers)
  388. data2 = body.text.replace('}',"}\n")
  389. print(data2)
  390. # ------- LISTS OF ATTACHMENTS END -----------
  391. if arguments == 1:
  392. if sys.argv[1] == 'users':
  393. # ------- LIST OF USERS START -----------
  394. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  395. print(users)
  396. print("=== USERS ===\n")
  397. body = requests.get(users, headers=headers)
  398. data2 = body.text.replace('}',"}\n")
  399. print(data2)
  400. # ------- LIST OF USERS END -----------
  401. if sys.argv[1] == 'user':
  402. # ------- LIST OF ALL USERS START -----------
  403. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  404. print(user)
  405. print("=== USER ===\n")
  406. body = requests.get(user, headers=headers)
  407. data2 = body.text.replace('}',"}\n")
  408. print(data2)
  409. # ------- LIST OF ALL USERS END -----------
  410. if sys.argv[1] == 'boards':
  411. # ------- LIST OF PUBLIC BOARDS START -----------
  412. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  413. print("=== PUBLIC BOARDS ===\n")
  414. listpublicboards = wekanurl + apiboards
  415. body = requests.get(listpublicboards, headers=headers)
  416. data2 = body.text.replace('}',"}\n")
  417. print(data2)
  418. # ------- LIST OF PUBLIC BOARDS END -----------