生成身份令牌 Returns: token 字符串 Raises: EmailServiceError: 生成失败
(self)
| 79 | self._created_emails: Dict[str, Dict[str, Any]] = {} |
| 80 | |
| 81 | def _generate_token(self) -> str: |
| 82 | """ |
| 83 | 生成身份令牌 |
| 84 | |
| 85 | Returns: |
| 86 | token 字符串 |
| 87 | |
| 88 | Raises: |
| 89 | EmailServiceError: 生成失败 |
| 90 | """ |
| 91 | url = f"{self.config['base_url']}/api/public/genToken" |
| 92 | payload = { |
| 93 | "email": self.config["admin_email"], |
| 94 | "password": self.config["admin_password"] |
| 95 | } |
| 96 | |
| 97 | try: |
| 98 | response = self.session.post( |
| 99 | url, |
| 100 | json=payload, |
| 101 | timeout=self.config["timeout"] |
| 102 | ) |
| 103 | |
| 104 | if response.status_code >= 400: |
| 105 | error_msg = f"生成 token 失败: {response.status_code}" |
| 106 | try: |
| 107 | error_data = response.json() |
| 108 | error_msg = f"{error_msg} - {error_data}" |
| 109 | except Exception: |
| 110 | error_msg = f"{error_msg} - {response.text[:200]}" |
| 111 | raise EmailServiceError(error_msg) |
| 112 | |
| 113 | data = response.json() |
| 114 | if data.get("code") != 200: |
| 115 | raise EmailServiceError(f"生成 token 失败: {data.get('message', 'Unknown error')}") |
| 116 | |
| 117 | token = data.get("data", {}).get("token") |
| 118 | if not token: |
| 119 | raise EmailServiceError("生成 token 失败: 未返回 token") |
| 120 | |
| 121 | return token |
| 122 | |
| 123 | except requests.RequestException as e: |
| 124 | self.update_status(False, e) |
| 125 | raise EmailServiceError(f"生成 token 失败: {e}") |
| 126 | except Exception as e: |
| 127 | self.update_status(False, e) |
| 128 | if isinstance(e, EmailServiceError): |
| 129 | raise |
| 130 | raise EmailServiceError(f"生成 token 失败: {e}") |
| 131 | |
| 132 | def _get_token(self, force_refresh: bool = False) -> str: |
| 133 | """ |
no test coverage detected