response.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from typing import Optional, get_type_hints
  17. from aiohttp import ClientResponse
  18. from mautrix.types import JSON, Serializable
  19. from ..types import (
  20. CheckpointResponse,
  21. ConsentRequiredResponse,
  22. LoginErrorResponse,
  23. LoginRequiredResponse,
  24. SpamResponse,
  25. )
  26. from .base import IGError
  27. class IGChallengeWrongCodeError(IGError):
  28. pass
  29. class IGResponseError(IGError):
  30. response: ClientResponse
  31. def __init__(self, response: ClientResponse, json: JSON) -> None:
  32. prefix = f"Request {response.request_info.method} {response.request_info.url.path} failed"
  33. message = f"HTTP {response.status}"
  34. self.response = response
  35. if "message" in json:
  36. message = json["message"]
  37. type_hint = get_type_hints(type(self)).get("body", JSON)
  38. if type_hint is not JSON and issubclass(type_hint, Serializable):
  39. self.body = type_hint.deserialize(json)
  40. else:
  41. self.body = json
  42. super().__init__(f"{prefix}: {self._message_override or message}")
  43. @property
  44. def _message_override(self) -> Optional[str]:
  45. return None
  46. class IGActionSpamError(IGResponseError):
  47. body: SpamResponse
  48. @property
  49. def _message(self) -> str:
  50. return f"HTTP {self.body.message}"
  51. class IGNotFoundError(IGResponseError):
  52. pass
  53. class IGRateLimitError(IGResponseError):
  54. pass
  55. class IGCheckpointError(IGResponseError):
  56. body: CheckpointResponse
  57. @property
  58. def url(self) -> str:
  59. return self.body.challenge.api_path
  60. class IGConsentRequiredError(IGResponseError):
  61. body: ConsentRequiredResponse
  62. class IGNotLoggedInError(IGResponseError):
  63. body: LoginRequiredResponse
  64. @property
  65. def proper_message(self) -> str:
  66. return (
  67. f"{self.body.error_title or self.body.message} "
  68. f"(reason code: {self.body.logout_reason})"
  69. )
  70. class IGUserHasLoggedOutError(IGNotLoggedInError):
  71. pass
  72. class IGLoginRequiredError(IGNotLoggedInError):
  73. pass
  74. class IGPrivateUserError(IGResponseError):
  75. pass
  76. class IGSentryBlockError(IGResponseError):
  77. pass
  78. class IGInactiveUserError(IGResponseError):
  79. pass
  80. class IGLoginError(IGResponseError):
  81. body: LoginErrorResponse
  82. class IGLoginTwoFactorRequiredError(IGLoginError):
  83. pass
  84. class IGLoginBadPasswordError(IGLoginError):
  85. pass
  86. class IGLoginInvalidUserError(IGLoginError):
  87. pass
  88. class IGFBNoContactPointFoundError(IGLoginError):
  89. pass
  90. class IGBad2FACodeError(IGResponseError):
  91. pass