Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add user agent to API requests #205

Merged
merged 2 commits into from
Oct 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gcloud/connection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pkg_resources import get_distribution
import httplib2


Expand All @@ -17,6 +18,9 @@ class Connection(object):
_EMPTY = object()
"""A pointer to represent an empty value for default arguments."""

USER_AGENT = "gcloud-python/{0}".format(get_distribution('gcloud').version)
"""The user agent for gcloud-python requests."""

def __init__(self, credentials=None):
""":type credentials: :class:`gcloud.credentials.Credentials`
:param credentials: The OAuth2 Credentials to use for this connection.
Expand Down
1 change: 1 addition & 0 deletions gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def _request(self, dataset_id, method, data):
headers = {
'Content-Type': 'application/x-protobuf',
'Content-Length': str(len(data)),
'User-Agent': self.USER_AGENT,
}
headers, content = self.http.request(
uri=self.build_api_url(dataset_id=dataset_id, method=method),
Expand Down
16 changes: 16 additions & 0 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def test__request_w_200(self):
'method': 'POST',
'headers': {'Content-Type': 'application/x-protobuf',
'Content-Length': '4',
'User-Agent': conn.USER_AGENT,
},
'body': DATA,
})
Expand Down Expand Up @@ -115,6 +116,7 @@ def FromString(cls, pb):
'method': 'POST',
'headers': {'Content-Type': 'application/x-protobuf',
'Content-Length': '5',
'User-Agent': conn.USER_AGENT,
},
'body': b'REQPB',
})
Expand Down Expand Up @@ -218,6 +220,7 @@ def test_begin_transaction_default_serialize(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '2',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.BeginTransactionRequest
request = rq_class()
Expand Down Expand Up @@ -248,6 +251,7 @@ def test_begin_transaction_explicit_serialize(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '2',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.BeginTransactionRequest
request = rq_class()
Expand Down Expand Up @@ -299,6 +303,7 @@ def id(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '6',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.RollbackRequest
request = rq_class()
Expand Down Expand Up @@ -333,6 +338,7 @@ def test_run_query_wo_namespace_empty_result(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '14',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.RunQueryRequest
request = rq_class()
Expand Down Expand Up @@ -369,6 +375,7 @@ def test_run_query_w_namespace_nonempty_result(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '16',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.RunQueryRequest
request = rq_class()
Expand Down Expand Up @@ -401,6 +408,7 @@ def test_lookup_single_key_empty_response(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '26',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.LookupRequest
request = rq_class()
Expand Down Expand Up @@ -439,6 +447,7 @@ def test_lookup_single_key_nonempty_response(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '26',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.LookupRequest
request = rq_class()
Expand Down Expand Up @@ -474,6 +483,7 @@ def test_lookup_multiple_keys_empty_response(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '52',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.LookupRequest
request = rq_class()
Expand Down Expand Up @@ -516,6 +526,7 @@ def test_commit_wo_transaction(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '47',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.CommitRequest
request = rq_class()
Expand Down Expand Up @@ -561,6 +572,7 @@ def id(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '53',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.CommitRequest
request = rq_class()
Expand Down Expand Up @@ -595,6 +607,7 @@ def test_save_entity_wo_transaction_w_upsert(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '47',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.CommitRequest
request = rq_class()
Expand Down Expand Up @@ -645,6 +658,7 @@ def test_save_entity_wo_transaction_w_auto_id(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '44',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.CommitRequest
request = rq_class()
Expand Down Expand Up @@ -720,6 +734,7 @@ def test_delete_entities_wo_transaction(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '30',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.CommitRequest
request = rq_class()
Expand Down Expand Up @@ -791,6 +806,7 @@ def test_delete_entity_wo_transaction(self):
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '30',
'User-Agent': conn.USER_AGENT,
})
rq_class = datastore_pb.CommitRequest
request = rq_class()
Expand Down
2 changes: 2 additions & 0 deletions gcloud/storage/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def make_request(self, method, url, data=None, content_type=None,
if content_type:
headers['Content-Type'] = content_type

headers['User-Agent'] = self.USER_AGENT

return self.http.request(uri=url, method=method, headers=headers,
body=data)

Expand Down
6 changes: 6 additions & 0 deletions gcloud/storage/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def test_make_request_no_data_no_content_type_no_headers(self):
self.assertEqual(http._called_with['headers'],
{'Accept-Encoding': 'gzip',
'Content-Length': 0,
'User-Agent': conn.USER_AGENT,
})

def test_make_request_w_data_no_extra_headers(self):
Expand All @@ -189,6 +190,7 @@ def test_make_request_w_data_no_extra_headers(self):
{'Accept-Encoding': 'gzip',
'Content-Length': 0,
'Content-Type': 'application/json',
'User-Agent': conn.USER_AGENT,
})

def test_make_request_w_extra_headers(self):
Expand All @@ -208,6 +210,7 @@ def test_make_request_w_extra_headers(self):
{'Accept-Encoding': 'gzip',
'Content-Length': 0,
'X-Foo': 'foo',
'User-Agent': conn.USER_AGENT,
})

def test_api_request_defaults(self):
Expand All @@ -228,6 +231,7 @@ def test_api_request_defaults(self):
self.assertEqual(http._called_with['headers'],
{'Accept-Encoding': 'gzip',
'Content-Length': 0,
'User-Agent': conn.USER_AGENT,
})

def test_api_request_w_non_json_response(self):
Expand Down Expand Up @@ -287,6 +291,7 @@ def test_api_request_w_query_params(self):
self.assertEqual(http._called_with['headers'],
{'Accept-Encoding': 'gzip',
'Content-Length': 0,
'User-Agent': conn.USER_AGENT,
})

def test_api_request_w_data(self):
Expand All @@ -312,6 +317,7 @@ def test_api_request_w_data(self):
{'Accept-Encoding': 'gzip',
'Content-Length': len(DATAJ),
'Content-Type': 'application/json',
'User-Agent': conn.USER_AGENT,
})

def test_api_request_w_404(self):
Expand Down
6 changes: 6 additions & 0 deletions gcloud/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ def authorize(self, http):
conn = self._makeOne(creds)
self.assertTrue(conn.http is authorized)
self.assertTrue(isinstance(creds._called_with, Http))

def test_user_agent_format(self):
from pkg_resources import get_distribution
expected_ua = 'gcloud-python/{0}'.format(get_distribution('gcloud').version)
conn = self._makeOne()
self.assertEqual(conn.USER_AGENT, expected_ua)