17
17
A client library for Google's discovery based APIs.
18
18
"""
19
19
from __future__ import absolute_import
20
- import six
21
20
22
21
__author__ = "[email protected] (Joe Gregorio)"
23
22
__all__ = ["build" , "build_from_document" , "fix_method_name" , "key2param" ]
24
23
25
- from six .moves import http_client
26
- from six .moves .urllib .parse import urljoin
27
-
28
-
29
24
# Standard library imports
30
25
import copy
31
26
from collections import OrderedDict
32
-
33
- try :
34
- from email .generator import BytesGenerator
35
- except ImportError :
36
- from email .generator import Generator as BytesGenerator
27
+ import collections .abc
28
+ from email .generator import BytesGenerator
37
29
from email .mime .multipart import MIMEMultipart
38
30
from email .mime .nonmultipart import MIMENonMultipart
31
+ import http .client as http_client
32
+ import io
39
33
import json
40
34
import keyword
41
35
import logging
42
36
import mimetypes
43
37
import os
44
38
import re
39
+ import urllib
45
40
46
41
# Third-party imports
47
42
import httplib2
@@ -506,7 +501,7 @@ def build_from_document(
506
501
507
502
if client_options is None :
508
503
client_options = google .api_core .client_options .ClientOptions ()
509
- if isinstance (client_options , six . moves . collections_abc .Mapping ):
504
+ if isinstance (client_options , collections . abc .Mapping ):
510
505
client_options = google .api_core .client_options .from_dict (client_options )
511
506
512
507
if http is not None :
@@ -519,9 +514,9 @@ def build_from_document(
519
514
if option is not None :
520
515
raise ValueError ("Arguments http and {} are mutually exclusive" .format (name ))
521
516
522
- if isinstance (service , six . string_types ):
517
+ if isinstance (service , str ):
523
518
service = json .loads (service )
524
- elif isinstance (service , six . binary_type ):
519
+ elif isinstance (service , bytes ):
525
520
service = json .loads (service .decode ("utf-8" ))
526
521
527
522
if "rootUrl" not in service and isinstance (http , (HttpMock , HttpMockSequence )):
@@ -534,7 +529,7 @@ def build_from_document(
534
529
raise InvalidJsonError ()
535
530
536
531
# If an API Endpoint is provided on client options, use that as the base URL
537
- base = urljoin (service ["rootUrl" ], service ["servicePath" ])
532
+ base = urllib . parse . urljoin (service ["rootUrl" ], service ["servicePath" ])
538
533
if client_options .api_endpoint :
539
534
base = client_options .api_endpoint
540
535
@@ -630,7 +625,7 @@ def build_from_document(
630
625
if "mtlsRootUrl" in service and (
631
626
not client_options or not client_options .api_endpoint
632
627
):
633
- mtls_endpoint = urljoin (service ["mtlsRootUrl" ], service ["servicePath" ])
628
+ mtls_endpoint = urllib . parse . urljoin (service ["mtlsRootUrl" ], service ["servicePath" ])
634
629
use_mtls_endpoint = os .getenv (GOOGLE_API_USE_MTLS_ENDPOINT , "auto" )
635
630
636
631
if not use_mtls_endpoint in ("never" , "auto" , "always" ):
@@ -759,7 +754,7 @@ def _fix_up_parameters(method_desc, root_desc, http_method, schema):
759
754
parameters = method_desc .setdefault ("parameters" , {})
760
755
761
756
# Add in the parameters common to all methods.
762
- for name , description in six . iteritems ( root_desc .get ("parameters" , {})):
757
+ for name , description in root_desc .get ("parameters" , {}). items ( ):
763
758
parameters [name ] = description
764
759
765
760
# Add in undocumented query parameters.
@@ -875,7 +870,7 @@ def _urljoin(base, url):
875
870
# exception here is the case of media uploads, where url will be an
876
871
# absolute url.
877
872
if url .startswith ("http://" ) or url .startswith ("https://" ):
878
- return urljoin (base , url )
873
+ return urllib . parse . urljoin (base , url )
879
874
new_base = base if base .endswith ("/" ) else base + "/"
880
875
new_url = url [1 :] if url .startswith ("/" ) else url
881
876
return new_base + new_url
@@ -943,7 +938,7 @@ def set_parameters(self, method_desc):
943
938
"""
944
939
parameters = method_desc .get ("parameters" , {})
945
940
sorted_parameters = OrderedDict (sorted (parameters .items ()))
946
- for arg , desc in six . iteritems ( sorted_parameters ):
941
+ for arg , desc in sorted_parameters . items ( ):
947
942
param = key2param (arg )
948
943
self .argmap [param ] = arg
949
944
@@ -997,9 +992,9 @@ def createMethod(methodName, methodDesc, rootDesc, schema):
997
992
def method (self , ** kwargs ):
998
993
# Don't bother with doc string, it will be over-written by createMethod.
999
994
1000
- for name in six . iterkeys ( kwargs ) :
995
+ for name in kwargs :
1001
996
if name not in parameters .argmap :
1002
- raise TypeError ('Got an unexpected keyword argument "%s"' % name )
997
+ raise TypeError ('Got an unexpected keyword argument {}' . format ( name ) )
1003
998
1004
999
# Remove args that have a value of None.
1005
1000
keys = list (kwargs .keys ())
@@ -1016,9 +1011,9 @@ def method(self, **kwargs):
1016
1011
):
1017
1012
raise TypeError ('Missing required parameter "%s"' % name )
1018
1013
1019
- for name , regex in six . iteritems ( parameters .pattern_params ):
1014
+ for name , regex in parameters .pattern_params . items ( ):
1020
1015
if name in kwargs :
1021
- if isinstance (kwargs [name ], six . string_types ):
1016
+ if isinstance (kwargs [name ], str ):
1022
1017
pvalues = [kwargs [name ]]
1023
1018
else :
1024
1019
pvalues = kwargs [name ]
@@ -1029,13 +1024,13 @@ def method(self, **kwargs):
1029
1024
% (name , pvalue , regex )
1030
1025
)
1031
1026
1032
- for name , enums in six . iteritems ( parameters .enum_params ):
1027
+ for name , enums in parameters .enum_params . items ( ):
1033
1028
if name in kwargs :
1034
1029
# We need to handle the case of a repeated enum
1035
1030
# name differently, since we want to handle both
1036
1031
# arg='value' and arg=['value1', 'value2']
1037
1032
if name in parameters .repeated_params and not isinstance (
1038
- kwargs [name ], six . string_types
1033
+ kwargs [name ], str
1039
1034
):
1040
1035
values = kwargs [name ]
1041
1036
else :
@@ -1049,7 +1044,7 @@ def method(self, **kwargs):
1049
1044
1050
1045
actual_query_params = {}
1051
1046
actual_path_params = {}
1052
- for key , value in six . iteritems ( kwargs ):
1047
+ for key , value in kwargs . items ( ):
1053
1048
to_type = parameters .param_types .get (key , "string" )
1054
1049
# For repeated parameters we cast each member of the list.
1055
1050
if key in parameters .repeated_params and type (value ) == type ([]):
@@ -1086,7 +1081,7 @@ def method(self, **kwargs):
1086
1081
1087
1082
if media_filename :
1088
1083
# Ensure we end up with a valid MediaUpload object.
1089
- if isinstance (media_filename , six . string_types ):
1084
+ if isinstance (media_filename , str ):
1090
1085
if media_mime_type is None :
1091
1086
logger .warning (
1092
1087
"media_mime_type argument not specified: trying to auto-detect for %s" ,
@@ -1144,7 +1139,7 @@ def method(self, **kwargs):
1144
1139
msgRoot .attach (msg )
1145
1140
# encode the body: note that we can't use `as_string`, because
1146
1141
# it plays games with `From ` lines.
1147
- fp = six .BytesIO ()
1142
+ fp = io .BytesIO ()
1148
1143
g = _BytesGenerator (fp , mangle_from_ = False )
1149
1144
g .flatten (msgRoot , unixfrom = False )
1150
1145
body = fp .getvalue ()
@@ -1218,7 +1213,7 @@ def method(self, **kwargs):
1218
1213
enumDesc = paramdesc .get ("enumDescriptions" , [])
1219
1214
if enum and enumDesc :
1220
1215
docs .append (" Allowed values\n " )
1221
- for (name , desc ) in six . moves . zip (enum , enumDesc ):
1216
+ for (name , desc ) in zip (enum , enumDesc ):
1222
1217
docs .append (" %s - %s\n " % (name , desc ))
1223
1218
if "response" in methodDesc :
1224
1219
if methodName .endswith ("_media" ):
@@ -1415,7 +1410,7 @@ def new_batch_http_request(callback=None):
1415
1410
1416
1411
# Add basic methods to Resource
1417
1412
if "methods" in resourceDesc :
1418
- for methodName , methodDesc in six . iteritems ( resourceDesc ["methods" ]):
1413
+ for methodName , methodDesc in resourceDesc ["methods" ]. items ( ):
1419
1414
fixedMethodName , method = createMethod (
1420
1415
methodName , methodDesc , rootDesc , schema
1421
1416
)
@@ -1463,7 +1458,7 @@ def methodResource(self):
1463
1458
1464
1459
return (methodName , methodResource )
1465
1460
1466
- for methodName , methodDesc in six . iteritems ( resourceDesc ["resources" ]):
1461
+ for methodName , methodDesc in resourceDesc ["resources" ]. items ( ):
1467
1462
fixedMethodName , method = createResourceMethod (methodName , methodDesc )
1468
1463
self ._set_dynamic_attr (
1469
1464
fixedMethodName , method .__get__ (self , self .__class__ )
@@ -1475,7 +1470,7 @@ def _add_next_methods(self, resourceDesc, schema):
1475
1470
# type either the method's request (query parameters) or request body.
1476
1471
if "methods" not in resourceDesc :
1477
1472
return
1478
- for methodName , methodDesc in six . iteritems ( resourceDesc ["methods" ]):
1473
+ for methodName , methodDesc in resourceDesc ["methods" ]. items ( ):
1479
1474
nextPageTokenName = _findPageTokenName (
1480
1475
_methodProperties (methodDesc , schema , "response" )
1481
1476
)
0 commit comments