MCPcopy
hub / github.com/Rapptz/discord.py / request

Method request

discord/webhook/sync.py:115–235  ·  view source on GitHub ↗
(
        self,
        route: Route,
        session: Session,
        *,
        payload: Optional[Dict[str, Any]] = None,
        multipart: Optional[List[Dict[str, Any]]] = None,
        files: Optional[Sequence[File]] = None,
        reason: Optional[str] = None,
        auth_token: Optional[str] = None,
        params: Optional[Dict[str, Any]] = None,
    )

Source from the content-addressed store, hash-verified

113 self._locks: weakref.WeakValueDictionary[Any, threading.Lock] = weakref.WeakValueDictionary()
114
115 def request(
116 self,
117 route: Route,
118 session: Session,
119 *,
120 payload: Optional[Dict[str, Any]] = None,
121 multipart: Optional[List[Dict[str, Any]]] = None,
122 files: Optional[Sequence[File]] = None,
123 reason: Optional[str] = None,
124 auth_token: Optional[str] = None,
125 params: Optional[Dict[str, Any]] = None,
126 ) -> Any:
127 headers: Dict[str, str] = {}
128 files = files or []
129 to_send: Optional[Union[str, bytes, Dict[str, Any]]] = None
130 bucket = (route.webhook_id, route.webhook_token)
131
132 try:
133 lock = self._locks[bucket]
134 except KeyError:
135 self._locks[bucket] = lock = threading.Lock()
136
137 if payload is not None:
138 headers['Content-Type'] = 'application/json; charset=utf-8'
139 to_send = utils._to_json(payload).encode('utf-8')
140
141 if auth_token is not None:
142 headers['Authorization'] = f'Bot {auth_token}'
143
144 if reason is not None:
145 headers['X-Audit-Log-Reason'] = urlquote(reason, safe='/ ')
146
147 response: Optional[Response] = None
148 data: Optional[Union[Dict[str, Any], str]] = None
149 file_data: Optional[Dict[str, Any]] = None
150 method = route.method
151 url = route.url
152 webhook_id = route.webhook_id
153
154 with DeferredLock(lock) as lock:
155 for attempt in range(5):
156 for file in files:
157 file.reset(seek=attempt)
158
159 if multipart:
160 file_data = {}
161 for p in multipart:
162 name = p['name']
163 if name == 'payload_json':
164 to_send = {'payload_json': p['value']}
165 else:
166 file_data[name] = (p['filename'], p['value'], p['content_type'])
167
168 try:
169 with session.request(
170 method, url, data=to_send, files=file_data, headers=headers, params=params
171 ) as response:
172 _log.debug(

Callers 10

delete_webhookMethod · 0.95
edit_webhookMethod · 0.95
execute_webhookMethod · 0.95
get_webhook_messageMethod · 0.95
edit_webhook_messageMethod · 0.95
fetch_webhookMethod · 0.95

Calls 9

DeferredLockClass · 0.85
HTTPExceptionClass · 0.85
ForbiddenClass · 0.85
NotFoundClass · 0.85
DiscordServerErrorClass · 0.85
encodeMethod · 0.80
resetMethod · 0.45
getMethod · 0.45
delay_byMethod · 0.45

Tested by

no test coverage detected