#[1]gnikyt feed [2]gnikyt Code ramblings. Async Shopify API for Python /* Jun 29, 2020 — 3.9KB */ Introduction I maintain a fairly used [3]PHP library for Shopify API which uses Guzzle for sync/async requests. It was also recently featured as a recommended third-party library on Shopify’s dev docs. For Python, which I also use with Shopify work, I didn’t notice a library which provides async abilities. Normally I use Shopify’s Python library which provides a nice ActiveResource implementation. However, their library has no plans to support async. I decided to create a tested library that can do both, [4]basic_shopify_api. It is a loose port of my PHP version which supports sync, async, HMAC validation, rate limiting, automatic retries, REST, and GraphQL; all backed by the HTTPX package. You simply need to set up some basic options and create a session for a shop to get started. Sync REST from basic_shopify_api import Client # ... with Client(sess, opts) as client: shop = client.rest("get", "/admin/api/shop.json", {"fields": "name,email"}) print(shop.response) print(shop.body["name"]) # returns the following: # RestResult( # response=The HTTPX response object, # body=A dict of JSON response, or None if errors, # errors=A dict of error response (if possible), or None for no errors, or the exception error, # status=The HTTP status code, # link=A RestLink object of next/previous pagination info, # retries=Number of retires for the request # ) GraphQL from basic_shopify_api import Client # ... with Client(sess, opts) as client: shop = client.graphql("{ shop { name } }") print(shop.response) print(shop.body["data"]["shop"]["name"]) # returns the following: # ApiResult( # response=The HTTPX response object, # body=A dict of JSON response, or None if errors, # errors=A dict of error response (if possible), or None for no errors, or the exception error, # status=The HTTP status code, # retries=Number of retires for the request, # ) Async REST from basic_shopify_api import AsyncClient # ... async with AsyncClient(sess, opts) as client: shop = await client.rest("get", "/admin/api/shop.json", {"fields": "name,ema il"}) print(shop.response) print(shop.body["name"]) # returns the following: # RestResult( # response=The HTTPX response object, # body=A dict of JSON response, or None if errors, # errors=A dict of error response (if possible), or None for no errors, or the exception error, # status=The HTTP status code, # link=A RestLink object of next/previous pagination info, # retries=Number of retires for the request # ) GraphQL from basic_shopify_api import AsyncClient # ... async with AsyncClient(sess, opts) as client: shop = await client.graphql("{ shop { name } }") print(shop.response) print(shop.body["data"]["shop"]["name"]) # returns the following: # ApiResult( # response=The HTTPX response object, # body=A dict of JSON response, or None if errors, # errors=A dict of error response (if possible), or None for no errors, or the exception error, # status=The HTTP status code, # retries=Number of retires for the request, # ) Conclusion The above examples are brief, refer to the README of the project for full information. But I believe you will find it very helpful - especially the automatic retries for failed requests and built-in rate/cost limiting. Feel free to give it a try with your projects, with the continual rise of async within Python, I hope the library will serve some use to you. I’ve recently paired it with [5]FastAPI and it performed well. [6]MD | [7]TXT | [8]CC-4.0 This post is 4 years old and may contain outdated information. __________________________________________________________________ [9]Ty King Ty King A self-taught, seasoned, and versatile developer from Newfoundland. Crafting innovative solutions with care and expertise. See more [10]about me. [11]Github [12]LinkedIn [13]CV [14]RSS * * * * * * * * * * References Visible links: 1. /rss.xml 2. / 3. https://github.com/gnikyt/Basic-Shopify-API 4. https://github.com/gnikyt/basic_shopify_api 5. https://fastapi.tiangolo.com/ 6. /async-shopify-api-for-python/index.md 7. /async-shopify-api-for-python/index.txt 8. https://creativecommons.org/licenses/by/4.0/ 9. /about 10. /about 11. https://github.com/gnikyt 12. https://linkedin.com/in/gnikyt 13. /assets/files/cv.pdf 14. /rss.xml Hidden links: 16. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-1 17. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-2 18. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-3 19. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-4 20. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-5 21. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-6 22. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-7 23. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-8 24. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-9 25. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-10 26. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-11 27. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-12 28. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-13 29. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-14 30. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-15 31. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-16 32. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-17 33. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb1-18 34. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-1 35. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-2 36. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-3 37. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-4 38. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-5 39. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-6 40. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-7 41. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-8 42. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-9 43. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-10 44. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-11 45. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-12 46. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-13 47. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-14 48. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-15 49. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-16 50. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb2-17 51. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-1 52. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-2 53. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-3 54. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-4 55. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-5 56. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-6 57. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-7 58. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-8 59. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-9 60. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-10 61. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-11 62. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-12 63. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-13 64. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-14 65. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-15 66. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-16 67. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-17 68. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb3-18 69. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-1 70. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-2 71. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-3 72. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-4 73. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-5 74. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-6 75. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-7 76. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-8 77. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-9 78. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-10 79. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-11 80. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-12 81. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-13 82. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-14 83. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-15 84. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-16 85. localhost/tmp/lynxXXXXwoBmQ5/L761362-6481TMP.html#cb4-17