graphql-client.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import requests
  2. class GraphQLClient:
  3. # Constructeur, demande l'url de l'api
  4. def __init__(self, url):
  5. self.url = url
  6. try:
  7. requests.get(self.url)
  8. except:
  9. raise self.__BadURLException('bad url: ' + self.url)
  10. # Création d'une query
  11. def query(self, graphQlQuery, variables = None):
  12. query = self.__GraphQLQuery()
  13. return query.set(self.url, graphQlQuery, variables)
  14. def isError(self, result):
  15. query = self.__GraphQLQuery()
  16. return query.isError(result)
  17. # Class privée pour gérer les query
  18. class __GraphQLQuery:
  19. __set = False
  20. last = None
  21. url = None
  22. query = None
  23. variables = None
  24. # Verif que l'objet est bien set
  25. def __isSet(self):
  26. if(not(self.__set)):
  27. raise ValueError('Object attributes are not set')
  28. # Constructeur (le constructeur par defaut doit être gardé)
  29. def set(self, url, query, var = None):
  30. self.url = url
  31. self.query = query
  32. self.variables = var
  33. self.__set = True
  34. return self
  35. # Execute la query
  36. def execute(self, returnSelf = True):
  37. self.__isSet()
  38. # Make the HTTP Api request
  39. self.last = requests.post(self.url, json={'query': self.query, 'variables': self.variables})
  40. if returnSelf:
  41. return self
  42. else:
  43. return self.last.status_code
  44. def get(self):
  45. if self.last == None:
  46. return None
  47. elif self.last.status_code == 200:
  48. return self.data()
  49. elif self.last.status_code >= 400 and self.last.status_code < 500:
  50. return self.error()
  51. else:
  52. return self.last.text
  53. def data(self):
  54. if self.last == None or self.last.status_code != 200:
  55. return None
  56. else:
  57. return self.last.json()['data']
  58. def error(self):
  59. if self.last == None or self.last.status_code < 400 or self.last.status_code >= 500:
  60. return None
  61. else:
  62. if len(self.last.json()['errors']) == 1:
  63. return self.__GraphQLError(self.last.json()['errors'][0])
  64. errors = []
  65. for error in self.last.json()['errors']:
  66. errors.append(self.__GraphQLError(error))
  67. return errors
  68. def isError(self, result):
  69. return type(result) is self.__GraphQLError
  70. # Clas privée de query pour gérer les erreurs
  71. class __GraphQLError:
  72. # Constructeur
  73. def __init__(self, error):
  74. self.code = error['status']
  75. self.message = error['message']
  76. if error.get('locations') != None:
  77. self.line = error['locations'][0]['line']
  78. self.column = error['locations'][0]['column']
  79. else:
  80. self.line = 'Unknown'
  81. self.column = 'Unknown'
  82. # toString
  83. def __str__(self):
  84. return 'Status code: ' + str(self.code) + ', Line: ' + str(self.line) + ', Column: ' + str(self.column) + ', Message: ' + self.message
  85. # Class privée pour gérer les mauvaise url
  86. class __BadURLException(Exception):
  87. pass