Skip to content

Commit

Permalink
Add python version and platform to user agent header
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Jan 29, 2020
1 parent 090c5d1 commit 66d643d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 6 additions & 1 deletion openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import pathlib
import shutil
import sys
import warnings
from typing import Dict, List
from urllib.parse import urljoin
Expand Down Expand Up @@ -55,7 +56,11 @@ def __init__(self, root_url: str, auth: AuthBase = None, session: requests.Sessi
self.session = session or requests.Session()
self.auth = auth or NullAuth()
self.default_headers = {
"User-Agent": "openeo-python-client/{v}".format(v=openeo.client_version())
"User-Agent": "openeo-python-client/{cv} {py}/{pv} {pl}".format(
cv=openeo.client_version(),
py=sys.implementation.name, pv=".".join(map(str,sys.version_info[:3])),
pl=sys.platform
)
}

def build_url(self, path: str):
Expand Down
9 changes: 7 additions & 2 deletions tests/rest/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import re
import unittest.mock as mock

import pytest
import requests_mock
from openeo.rest import OpenEoClientException

from openeo.rest import OpenEoClientException
from openeo.rest.auth.auth import NullAuth, BearerAuth
from openeo.rest.connection import Connection, RestApiConnection, connect, OpenEoApiError

Expand Down Expand Up @@ -40,7 +41,11 @@ def test_rest_api_headers():
conn = RestApiConnection(API_URL)
with requests_mock.Mocker() as m:
def text(request, context):
assert request.headers["User-Agent"].startswith("openeo-python-client")
assert re.match(
r"^openeo-python-client/[0-9a-z.-]+ .*python/3.* (linux|win|darwin)",
request.headers["User-Agent"],
re.I
)
assert request.headers["X-Openeo-Bar"] == "XY123"

m.get("/foo", text=text)
Expand Down

0 comments on commit 66d643d

Please sign in to comment.