當前位置: 首頁>>代碼示例>>Python>>正文


Python httpx.get方法代碼示例

本文整理匯總了Python中httpx.get方法的典型用法代碼示例。如果您正苦於以下問題:Python httpx.get方法的具體用法?Python httpx.get怎麽用?Python httpx.get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在httpx的用法示例。


在下文中一共展示了httpx.get方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _resolve_github_ref

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def _resolve_github_ref(owner: str, repo: str, ref: str) -> str:
    """
    Given a GitHub owner/repo/ref, return the sha1 at that ref.

    Raise WorkbenchModuleImportError if the HTTP request fails or GitHub says
    there is no such ref. These errors are all in English, since we assume most
    module authors can read English and it takes effort to translate technical
    messages.
    """
    try:
        # raise HTTPError
        response = httpx.get(
            "https://api.github.com/repos/%s/%s/git/ref/heads/%s" % (owner, repo, ref),
            headers=[("Accept", "application/vnd.github.v3+json")],
        )
        # raise HTTPError
        response.raise_for_status()
    except httpx.HTTPError as err:
        raise WorkbenchModuleImportError(
            "HTTP error asking GitHub to resolve ref %(ref)s: %(message)s"
            % {"ref": ref, "message": str(err)}
        )

    data = json.loads(response.text)
    return data["object"]["sha"] 
開發者ID:CJWorkbench,項目名稱:cjworkbench,代碼行數:27,代碼來源:importmodule.py

示例2: pr_status

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def pr_status():
    """Show status of PR found in requirement files."""
    for pr in search_prs():
        r = httpx.get(f"https://api.github.com/repos/{pr.org}/{pr.repo}/pulls/{pr.pr}")
        r.raise_for_status()
        state = display_state(r.json())
        click.echo(f"https://github.com/{pr.org}/{pr.repo}/pull/{pr.pr} is {state}") 
開發者ID:acsone,項目名稱:acsoo,代碼行數:9,代碼來源:pr_status.py

示例3: fetch

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def fetch(
        self,
        station: str = None,
        lat: float = None,
        lon: float = None,
        timeout: int = 10,
    ) -> str:
        """
        Fetches a report string from the service
        """
        if station:
            valid_station(station)
        elif lat is None or lon is None:
            raise ValueError("No valid fetch parameters")
        try:
            url, params = self._make_url(station, lat, lon)
            if self.method.lower() == "post":
                resp = httpx.post(
                    url, params=params, data=self._post_data(station), timeout=timeout
                )
            else:
                resp = httpx.get(url, params=params, timeout=timeout)
            if resp.status_code != 200:
                raise SourceError(
                    f"{self.__class__.__name__} server returned {resp.status_code}"
                )
        except (httpx.ConnectTimeout, httpx.ReadTimeout):
            raise TimeoutError(f"Timeout from {self.__class__.__name__} server")
        except gaierror:
            raise ConnectionError(
                f"Unable to connect to {self.__class__.__name__} server"
            )
        report = self._extract(resp.text, station)
        return self._clean_report(report) 
開發者ID:avwx-rest,項目名稱:avwx-engine,代碼行數:36,代碼來源:service.py

示例4: async_fetch

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def async_fetch(
        self,
        station: str = None,
        lat: float = None,
        lon: float = None,
        timeout: int = 10,
    ) -> str:
        """
        Asynchronously fetch a report string from the service
        """
        if station:
            valid_station(station)
        elif lat is None or lon is None:
            raise ValueError("No valid fetch parameters")
        url, params = self._make_url(station, lat, lon)
        try:
            async with httpx.AsyncClient(timeout=timeout) as client:
                if self.method.lower() == "post":
                    resp = await client.post(
                        url, params=params, data=self._post_data(station)
                    )
                else:
                    resp = await client.get(url, params=params)
                if resp.status_code != 200:
                    raise SourceError(
                        f"{self.__class__.__name__} server returned {resp.status_code}"
                    )
        except (httpx.ConnectTimeout, httpx.ReadTimeout):
            raise TimeoutError(f"Timeout from {self.__class__.__name__} server")
        except gaierror:
            raise ConnectionError(
                f"Unable to connect to {self.__class__.__name__} server"
            )
        report = self._extract(resp.text, station)
        return self._clean_report(report)


# Multiple sources for NOAA data 
開發者ID:avwx-rest,項目名稱:avwx-engine,代碼行數:40,代碼來源:service.py

示例5: __init__

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def __init__(self, request_type: str):
        super().__init__(self._rtype_map.get(request_type, request_type)) 
開發者ID:avwx-rest,項目名稱:avwx-engine,代碼行數:4,代碼來源:service.py

示例6: get_service

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def get_service(station: str, country_code: str) -> Service:
    """
    Returns the preferred service for a given station
    """
    for prefix in PREFERRED:
        if station.startswith(prefix):
            return PREFERRED[prefix]
    return BY_COUNTRY.get(country_code, NOAA)


# Specialty Services 
開發者ID:avwx-rest,項目名稱:avwx-engine,代碼行數:13,代碼來源:service.py

示例7: get_content_from_url

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def get_content_from_url(self, url):
        """
        Fetch url and try to get the title and description from the response
        """
        title = None
        description = None
        timeout = httpx.Timeout(10.0, connect_timeout=2.0, read_timeout=5.0)
        try:
            r = httpx.get(url, timeout=timeout)
        except Exception as e:
            self.logger.error(f"Failed fetching url {url}. Error: {e}")
            return (title, description)

        if r.status_code != 200:
            self.logger.info(f"Failed fetching url {url}. Status code: {r.status_code}")
            return (title, description)

        # try parse and get the title
        try:
            soup = BeautifulSoup(r.text, "html.parser")
            if soup.head and soup.head.title:
                title = soup.head.title.string.strip()
            descr_tag = soup.find("meta", attrs={"name": "description"})
            if descr_tag:
                description = descr_tag.get("content", None)
        except Exception as e:
            self.logger.error(f"Failed parsing response from url {url}. Error: {e}")
            return (title, description)

        # Issue 63 patch - Title should not contain newlines or tabs
        if title is not None:
            assert isinstance(title, str)
            title = title.replace('\n', '')
            title = title.replace('\t', '')
        return (title, description) 
開發者ID:vranki,項目名稱:hemppa,代碼行數:37,代碼來源:url.py

示例8: set_settings

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def set_settings(self, data):
        super().set_settings(data)
        if data.get("status"):
            self.status = data["status"]
        if data.get("type"):
            self.type = data["type"] 
開發者ID:vranki,項目名稱:hemppa,代碼行數:8,代碼來源:url.py

示例9: help

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def help(self):
        return "If I see a url in a message I will try to get the title from the page and spit it out" 
開發者ID:vranki,項目名稱:hemppa,代碼行數:4,代碼來源:url.py

示例10: user_profile

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def user_profile(access_token: str, domain: str) -> Dict[str, Any]:
    """Returns user profile."""

    headers = {
        "Authorization": f"Bearer {access_token}"
    }

    resp = httpx.get(
        f"https://api.amazon.{domain}/user/profile", headers=headers
    )
    resp.raise_for_status()

    return resp.json() 
開發者ID:mkb79,項目名稱:Audible,代碼行數:15,代碼來源:auth.py

示例11: autodetect_locale

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def autodetect_locale(domain: str) -> Dict[str, str]:
    """
    Try to automatically detect correct settings for marketplace.

    Needs the top level domain of the audible page to continue with
    (e.g. co.uk, co.jp) and returns results found.

    """
    domain = domain.lstrip(".")
    site = f"https://www.audible.{domain}"
    params = {
        "ipRedirectOverride": True,
        "overrideBaseCountry": True
    }

    try:
        resp = httpx.get(site, params=params)
    except ConnectError as e:
        logger.warning(f"site {site} doesn\'t exists or Network Error occours")
        raise e

    soup = BeautifulSoup(resp.text, "html.parser")

    login_link = soup.find("a", class_="ui-it-sign-in-link")["href"]
    parsed_link = urlparse(login_link)
    query_string = parse_qs(parsed_link.query)
    market_place_id = query_string['marketPlaceId'][0]
    country_code = query_string['pageId'][0].split("_")[-1]

    return {
        "country_code": country_code,
        "domain": domain,
        "market_place_id": market_place_id
    } 
開發者ID:mkb79,項目名稱:Audible,代碼行數:36,代碼來源:localization.py

示例12: default_captcha_callback

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def default_captcha_callback(captcha_url: str) -> str:
    """Helper function for handling captcha."""
    # on error print captcha url instead of display captcha
    try:
        captcha = httpx.get(captcha_url).content
        f = io.BytesIO(captcha)
        img = Image.open(f)
        img.show()
    except:
        print(captcha_url)

    guess = input("Answer for CAPTCHA: ")
    return str(guess).strip().lower() 
開發者ID:mkb79,項目名稱:Audible,代碼行數:15,代碼來源:login.py

示例13: text_cb

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def text_cb(self, room, event):
        """
        Handle client callbacks for all room text events
        """
        if self.bot.should_ignore_event(event):
            return

        # no content at all?
        if len(event.body) < 1:
            return

        # are we on in this room?
        status = self.status.get(room.room_id, "OFF")
        if status not in self.STATUSES:
            return
        if status == "OFF":
            return

        # extract possible urls from message
        urls = re.findall(r"(https?://\S+)", event.body)

        # no urls, nothing to do
        if len(urls) == 0:
            return

        # fetch the urls and if we can see a title spit it out
        for url in urls:
            try:
                title, description = self.get_content_from_url(url)
            except Exception:
                # failed fetching, give up
                continue

            msg = None

            if status == "TITLE" and title is not None:
                msg = f"Title: {title}"
            elif status == "DESCRIPTION" and description is not None:
                msg = f"Description: {description}"

            elif status == "BOTH" and title is not None and description is not None:
                msg = f"Title: {title}\nDescription: {description}"

            elif status == "BOTH" and title is not None:
                msg = f"Title: {title}"
            elif status == "BOTH" and description is not None:
                msg = f"Description: {description}"

            if msg is not None:
                await self.bot.send_text(room, msg, msgtype=self.type, bot_ignore=True) 
開發者ID:vranki,項目名稱:hemppa,代碼行數:52,代碼來源:url.py

示例14: matrix_message

# 需要導入模塊: import httpx [as 別名]
# 或者: from httpx import get [as 別名]
def matrix_message(self, bot, room, event):
        """
        commands for setting what to do in this channel
        """
        bot.must_be_admin(room, event)

        args = shlex.split(event.body)
        args.pop(0)

        # save the new status
        if len(args) == 1 and self.STATUSES.get(args[0].upper()) is not None:
            self.status[room.room_id] = args[0].upper()
            bot.save_settings()
            await bot.send_text(
                room, f"Ok, {self.STATUSES.get(self.status[room.room_id])}"
            )
            return

        # show status
        elif len(args) == 1 and args[0] == "status":
            await bot.send_text(
                room, self.STATUSES.get(self.status.get(room.room_id, "OFF"))
            )
            return

        # set type to notice
        elif len(args) == 1 and args[0] == "notice":
            bot.must_be_owner(event)
            self.type = "m.notice"
            bot.save_settings()
            await bot.send_text(
                room, "Sending titles as notices from now on."
            )
            return

        # show status
        elif len(args) == 1 and args[0] == "text":
            bot.must_be_owner(event)
            self.type = "m.text"
            bot.save_settings()
            await bot.send_text(
                room, "Sending titles as text from now on."
            )
            return

        # invalid command
        await bot.send_text(
            room,
            "Sorry, I did not understand. See README for command list.",
        )

        return 
開發者ID:vranki,項目名稱:hemppa,代碼行數:54,代碼來源:url.py


注:本文中的httpx.get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。