Skip to content

Commit 1779aec

Browse files
committed
Merge pull request #205 from silvolu/user-agent
feat: add user agent to API requests
2 parents c80cf50 + dca6f00 commit 1779aec

File tree

6 files changed

+35
-0
lines changed

6 files changed

+35
-0
lines changed

gcloud/connection.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pkg_resources import get_distribution
12
import httplib2
23

34

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

21+
USER_AGENT = "gcloud-python/{0}".format(get_distribution('gcloud').version)
22+
"""The user agent for gcloud-python requests."""
23+
2024
def __init__(self, credentials=None):
2125
""":type credentials: :class:`gcloud.credentials.Credentials`
2226
:param credentials: The OAuth2 Credentials to use for this connection.

gcloud/datastore/connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def _request(self, dataset_id, method, data):
5050
headers = {
5151
'Content-Type': 'application/x-protobuf',
5252
'Content-Length': str(len(data)),
53+
'User-Agent': self.USER_AGENT,
5354
}
5455
headers, content = self.http.request(
5556
uri=self.build_api_url(dataset_id=dataset_id, method=method),

gcloud/datastore/test_connection.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def test__request_w_200(self):
6565
'method': 'POST',
6666
'headers': {'Content-Type': 'application/x-protobuf',
6767
'Content-Length': '4',
68+
'User-Agent': conn.USER_AGENT,
6869
},
6970
'body': DATA,
7071
})
@@ -115,6 +116,7 @@ def FromString(cls, pb):
115116
'method': 'POST',
116117
'headers': {'Content-Type': 'application/x-protobuf',
117118
'Content-Length': '5',
119+
'User-Agent': conn.USER_AGENT,
118120
},
119121
'body': b'REQPB',
120122
})
@@ -218,6 +220,7 @@ def test_begin_transaction_default_serialize(self):
218220
self.assertEqual(cw['headers'],
219221
{'Content-Type': 'application/x-protobuf',
220222
'Content-Length': '2',
223+
'User-Agent': conn.USER_AGENT,
221224
})
222225
rq_class = datastore_pb.BeginTransactionRequest
223226
request = rq_class()
@@ -248,6 +251,7 @@ def test_begin_transaction_explicit_serialize(self):
248251
self.assertEqual(cw['headers'],
249252
{'Content-Type': 'application/x-protobuf',
250253
'Content-Length': '2',
254+
'User-Agent': conn.USER_AGENT,
251255
})
252256
rq_class = datastore_pb.BeginTransactionRequest
253257
request = rq_class()
@@ -299,6 +303,7 @@ def id(self):
299303
self.assertEqual(cw['headers'],
300304
{'Content-Type': 'application/x-protobuf',
301305
'Content-Length': '6',
306+
'User-Agent': conn.USER_AGENT,
302307
})
303308
rq_class = datastore_pb.RollbackRequest
304309
request = rq_class()
@@ -333,6 +338,7 @@ def test_run_query_wo_namespace_empty_result(self):
333338
self.assertEqual(cw['headers'],
334339
{'Content-Type': 'application/x-protobuf',
335340
'Content-Length': '14',
341+
'User-Agent': conn.USER_AGENT,
336342
})
337343
rq_class = datastore_pb.RunQueryRequest
338344
request = rq_class()
@@ -369,6 +375,7 @@ def test_run_query_w_namespace_nonempty_result(self):
369375
self.assertEqual(cw['headers'],
370376
{'Content-Type': 'application/x-protobuf',
371377
'Content-Length': '16',
378+
'User-Agent': conn.USER_AGENT,
372379
})
373380
rq_class = datastore_pb.RunQueryRequest
374381
request = rq_class()
@@ -401,6 +408,7 @@ def test_lookup_single_key_empty_response(self):
401408
self.assertEqual(cw['headers'],
402409
{'Content-Type': 'application/x-protobuf',
403410
'Content-Length': '26',
411+
'User-Agent': conn.USER_AGENT,
404412
})
405413
rq_class = datastore_pb.LookupRequest
406414
request = rq_class()
@@ -439,6 +447,7 @@ def test_lookup_single_key_nonempty_response(self):
439447
self.assertEqual(cw['headers'],
440448
{'Content-Type': 'application/x-protobuf',
441449
'Content-Length': '26',
450+
'User-Agent': conn.USER_AGENT,
442451
})
443452
rq_class = datastore_pb.LookupRequest
444453
request = rq_class()
@@ -474,6 +483,7 @@ def test_lookup_multiple_keys_empty_response(self):
474483
self.assertEqual(cw['headers'],
475484
{'Content-Type': 'application/x-protobuf',
476485
'Content-Length': '52',
486+
'User-Agent': conn.USER_AGENT,
477487
})
478488
rq_class = datastore_pb.LookupRequest
479489
request = rq_class()
@@ -516,6 +526,7 @@ def test_commit_wo_transaction(self):
516526
self.assertEqual(cw['headers'],
517527
{'Content-Type': 'application/x-protobuf',
518528
'Content-Length': '47',
529+
'User-Agent': conn.USER_AGENT,
519530
})
520531
rq_class = datastore_pb.CommitRequest
521532
request = rq_class()
@@ -561,6 +572,7 @@ def id(self):
561572
self.assertEqual(cw['headers'],
562573
{'Content-Type': 'application/x-protobuf',
563574
'Content-Length': '53',
575+
'User-Agent': conn.USER_AGENT,
564576
})
565577
rq_class = datastore_pb.CommitRequest
566578
request = rq_class()
@@ -595,6 +607,7 @@ def test_save_entity_wo_transaction_w_upsert(self):
595607
self.assertEqual(cw['headers'],
596608
{'Content-Type': 'application/x-protobuf',
597609
'Content-Length': '47',
610+
'User-Agent': conn.USER_AGENT,
598611
})
599612
rq_class = datastore_pb.CommitRequest
600613
request = rq_class()
@@ -645,6 +658,7 @@ def test_save_entity_wo_transaction_w_auto_id(self):
645658
self.assertEqual(cw['headers'],
646659
{'Content-Type': 'application/x-protobuf',
647660
'Content-Length': '44',
661+
'User-Agent': conn.USER_AGENT,
648662
})
649663
rq_class = datastore_pb.CommitRequest
650664
request = rq_class()
@@ -720,6 +734,7 @@ def test_delete_entities_wo_transaction(self):
720734
self.assertEqual(cw['headers'],
721735
{'Content-Type': 'application/x-protobuf',
722736
'Content-Length': '30',
737+
'User-Agent': conn.USER_AGENT,
723738
})
724739
rq_class = datastore_pb.CommitRequest
725740
request = rq_class()
@@ -791,6 +806,7 @@ def test_delete_entity_wo_transaction(self):
791806
self.assertEqual(cw['headers'],
792807
{'Content-Type': 'application/x-protobuf',
793808
'Content-Length': '30',
809+
'User-Agent': conn.USER_AGENT,
794810
})
795811
rq_class = datastore_pb.CommitRequest
796812
request = rq_class()

gcloud/storage/connection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ def make_request(self, method, url, data=None, content_type=None,
159159
if content_type:
160160
headers['Content-Type'] = content_type
161161

162+
headers['User-Agent'] = self.USER_AGENT
163+
162164
return self.http.request(uri=url, method=method, headers=headers,
163165
body=data)
164166

gcloud/storage/test_connection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def test_make_request_no_data_no_content_type_no_headers(self):
170170
self.assertEqual(http._called_with['headers'],
171171
{'Accept-Encoding': 'gzip',
172172
'Content-Length': 0,
173+
'User-Agent': conn.USER_AGENT,
173174
})
174175

175176
def test_make_request_w_data_no_extra_headers(self):
@@ -189,6 +190,7 @@ def test_make_request_w_data_no_extra_headers(self):
189190
{'Accept-Encoding': 'gzip',
190191
'Content-Length': 0,
191192
'Content-Type': 'application/json',
193+
'User-Agent': conn.USER_AGENT,
192194
})
193195

194196
def test_make_request_w_extra_headers(self):
@@ -208,6 +210,7 @@ def test_make_request_w_extra_headers(self):
208210
{'Accept-Encoding': 'gzip',
209211
'Content-Length': 0,
210212
'X-Foo': 'foo',
213+
'User-Agent': conn.USER_AGENT,
211214
})
212215

213216
def test_api_request_defaults(self):
@@ -228,6 +231,7 @@ def test_api_request_defaults(self):
228231
self.assertEqual(http._called_with['headers'],
229232
{'Accept-Encoding': 'gzip',
230233
'Content-Length': 0,
234+
'User-Agent': conn.USER_AGENT,
231235
})
232236

233237
def test_api_request_w_non_json_response(self):
@@ -287,6 +291,7 @@ def test_api_request_w_query_params(self):
287291
self.assertEqual(http._called_with['headers'],
288292
{'Accept-Encoding': 'gzip',
289293
'Content-Length': 0,
294+
'User-Agent': conn.USER_AGENT,
290295
})
291296

292297
def test_api_request_w_data(self):
@@ -312,6 +317,7 @@ def test_api_request_w_data(self):
312317
{'Accept-Encoding': 'gzip',
313318
'Content-Length': len(DATAJ),
314319
'Content-Type': 'application/json',
320+
'User-Agent': conn.USER_AGENT,
315321
})
316322

317323
def test_api_request_w_404(self):

gcloud/test_connection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ def authorize(self, http):
4343
conn = self._makeOne(creds)
4444
self.assertTrue(conn.http is authorized)
4545
self.assertTrue(isinstance(creds._called_with, Http))
46+
47+
def test_user_agent_format(self):
48+
from pkg_resources import get_distribution
49+
expected_ua = 'gcloud-python/{0}'.format(get_distribution('gcloud').version)
50+
conn = self._makeOne()
51+
self.assertEqual(conn.USER_AGENT, expected_ua)

0 commit comments

Comments
 (0)