diff --git a/plugin/assets/white-pixel.png b/plugin/assets/white-pixel.png new file mode 100644 index 0000000..bf80af2 Binary files /dev/null and b/plugin/assets/white-pixel.png differ diff --git a/plugin/helpers.py b/plugin/helpers.py index 01a0ed0..5879478 100644 --- a/plugin/helpers.py +++ b/plugin/helpers.py @@ -9,6 +9,7 @@ from pathlib import Path from typing import Any, Callable, Literal, Sequence, cast +import requests import sublime from LSP.plugin.core.protocol import Position as LspPosition from LSP.plugin.core.protocol import Range as LspRange @@ -39,7 +40,6 @@ get_project_relative_path, get_view_language_id, set_copilot_setting, - simple_urlopen, ) @@ -95,10 +95,13 @@ def update_avatar(cls, username: str, *, size: int = 64) -> None: cls.clear_avatar() return - try: - data = simple_urlopen(f"https://github.com/{username}.png?size={size}") - except Exception as e: - log_error(f'Failed to fetch avatar for "{username}" because: {e}') + if (req := requests.get(f"https://github.com/{username}.png?size={size}")).ok: + data = req.content + # see https://github.com/TerminalFi/LSP-copilot/issues/218#issuecomment-2535522265 + elif req.status_code == 404: + data = sublime.load_binary_resource(f"Packages/{PACKAGE_NAME}/plugin/assets/white-pixel.png") + else: + log_error(f'Failed to fetch avatar for "{username}" with status code {req.status_code}.') cls.clear_avatar() return diff --git a/plugin/utils.py b/plugin/utils.py index 2ee44cb..46c73bb 100644 --- a/plugin/utils.py +++ b/plugin/utils.py @@ -1,11 +1,9 @@ from __future__ import annotations import contextlib -import gzip import os import sys import threading -import urllib.request from collections.abc import Callable, Generator, Iterable from functools import wraps from typing import Any, Mapping, Sequence, TypeVar, Union, cast @@ -236,27 +234,3 @@ def find_index_by_key_value(items: Sequence[Mapping[_KT, _VT]], key: _KT, value: If not found, returns `-1`. """ return first((idx for idx, item in enumerate(items) if key in item and item[key] == value), -1) - - -def simple_urlopen(url: str, *, chunk_size: int = 512 * 1024) -> bytes: - """ - Opens a URL and reads the data in chunks, optionally decompressing gzip-encoded content. - - This function opens a connection to the specified URL and reads the response data in chunks - of a specified size. If the response is gzip-encoded, it decompresses the data before returning it. - - Args: - url (str): The URL to open. - chunk_size (int, optional): The size of each chunk to read. Defaults to 512KB. - - Returns: - bytes: The raw bytes of the data read from the URL. If the content was gzip-encoded, - it is decompressed before being returned. - """ - with urllib.request.urlopen(url) as resp: - data = b"" - while chunk := resp.read(chunk_size): - data += chunk - if resp.info().get("Content-Encoding") == "gzip": - data = gzip.decompress(data) - return data