Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

#!/usr/bin/env python 

"""Swagger generic API client. This client handles the client- 

server communication, and is invariant across implementations. Specifics of 

the methods and models for each application are generated from the Swagger 

templates.""" 

 

import ast 

import sys 

import os 

import re 

import urllib 

import urllib2 

import httplib 

import json 

import datetime 

import mimetypes 

import random 

import string 

 

from magnum.common import utils 

from models import * 

 

 

class ApiClient(object): 

  """Generic API client for Swagger client library builds 

 

  Attributes: 

    host: The base path for the server to call 

    headerName: a header to pass when making calls to the API 

    headerValue: a header value to pass when making calls to the API 

  """ 

  def __init__(self, host=None, headerName=None, headerValue=None): 

    self.defaultHeaders = {} 

    if (headerName is not None): 

      self.defaultHeaders[headerName] = headerValue 

    self.host = host 

    self.cookie = None 

    self.boundary = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(30)) 

    # Set default User-Agent. 

    self.user_agent = 'Python-Swagger' 

 

  @property 

  def user_agent(self): 

    return self.defaultHeaders['User-Agent'] 

 

  @user_agent.setter 

  def user_agent(self, value): 

    self.defaultHeaders['User-Agent'] = value 

 

  def setDefaultHeader(self, headerName, headerValue): 

    self.defaultHeaders[headerName] = headerValue 

 

  def callAPI(self, resourcePath, method, queryParams, postData, 

              headerParams=None, files=None): 

 

    url = self.host + resourcePath 

 

    mergedHeaderParams = self.defaultHeaders.copy() 

    mergedHeaderParams.update(headerParams) 

    headers = {} 

    if mergedHeaderParams: 

      for param, value in mergedHeaderParams.iteritems(): 

        headers[param] = ApiClient.sanitizeForSerialization(value) 

 

    if self.cookie: 

      headers['Cookie'] = ApiClient.sanitizeForSerialization(self.cookie) 

 

    data = None 

 

    if queryParams: 

      # Need to remove None values, these should not be sent 

      sentQueryParams = {} 

      for param, value in queryParams.items(): 

        if value is not None: 

          sentQueryParams[param] = ApiClient.sanitizeForSerialization(value) 

      url = url + '?' + urllib.urlencode(sentQueryParams) 

 

    if method in ['GET']: 

      #Options to add statements later on and for compatibility 

      pass 

 

    elif method in ['POST', 'PUT', 'DELETE']: 

      if postData: 

        postData = ApiClient.sanitizeForSerialization(postData) 

        if 'Content-type' not in headers: 

          headers['Content-type'] = 'application/json' 

          data = json.dumps(postData) 

        elif headers['Content-type'] == 'multipart/form-data': 

          data = self.buildMultipartFormData(postData, files) 

          headers['Content-type'] = 'multipart/form-data; boundary={0}'.format(self.boundary) 

          headers['Content-length'] = str(len(data)) 

        else: 

            data = urllib.urlencode(postData) 

 

    else: 

      raise Exception('Method ' + method + ' is not recognized.') 

 

    utils.raise_exception_invalid_scheme(url) 

 

    request = MethodRequest(method=method, url=url, headers=headers, 

                            data=data) 

 

    # Make the request 

    response = urllib2.urlopen(request) #nosec 

    if 'Set-Cookie' in response.headers: 

      self.cookie = response.headers['Set-Cookie'] 

    string = response.read() 

 

    try: 

      data = json.loads(string) 

    except ValueError:  # PUT requests don't return anything 

      data = None 

 

    return data 

 

  def toPathValue(self, obj): 

    """Convert a string or object to a path-friendly value 

    Args: 

        obj -- object or string value 

    Returns: 

        string -- quoted value 

    """ 

    if type(obj) == list: 

      return ','.join(obj) 

    else: 

      return str(obj) 

 

  @staticmethod 

  def sanitizeForSerialization(obj): 

    """ 

    Sanitize an object for Request. 

 

    If obj is None, return None. 

    If obj is str, int, long, float, bool, return directly. 

    If obj is datetime.datetime, datetime.date convert to string in iso8601 format. 

    If obj is list, santize each element in the list. 

    If obj is dict, return the dict. 

    If obj is swagger model, return the properties dict. 

    """ 

    if isinstance(obj, type(None)): 

      return None 

    elif isinstance(obj, (unicode, str, int, long, float, bool, file)): 

      return obj 

    elif isinstance(obj, list): 

      return [ApiClient.sanitizeForSerialization(subObj) for subObj in obj] 

    elif isinstance(obj, (datetime.datetime, datetime.date)): 

      return obj.isoformat() 

    else: 

      if isinstance(obj, dict): 

        objDict = obj 

      else: 

        # Convert model obj to dict except attributes `swaggerTypes`, `attributeMap` 

        # and attributes which value is not None. 

        # Convert attribute name to json key in model definition for request. 

        objDict = {obj.attributeMap[key]: val 

                   for key, val in obj.__dict__.iteritems() 

                   if key != 'swaggerTypes' and key != 'attributeMap' and val is not None} 

      return {key: ApiClient.sanitizeForSerialization(val) 

              for (key, val) in objDict.iteritems()} 

 

  def buildMultipartFormData(self, postData, files): 

    def escape_quotes(s): 

      return s.replace('"', '\\"') 

 

    lines = [] 

 

    for name, value in postData.items(): 

      lines.extend(( 

        '--{0}'.format(self.boundary), 

        'Content-Disposition: form-data; name="{0}"'.format(escape_quotes(name)), 

        '', 

        str(value), 

      )) 

 

    for name, filepath in files.items(): 

      f = open(filepath, 'r') 

      filename = filepath.split('/')[-1] 

      mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' 

      lines.extend(( 

        '--{0}'.format(self.boundary), 

        'Content-Disposition: form-data; name="{0}"; filename="{1}"'.format(escape_quotes(name), escape_quotes(filename)), 

        'Content-Type: {0}'.format(mimetype), 

        '', 

        f.read() 

      )) 

 

    lines.extend(( 

      '--{0}--'.format(self.boundary), 

      '' 

    )) 

    return '\r\n'.join(lines) 

 

  def deserialize(self, obj, objClass): 

    """Derialize a JSON string into an object. 

 

    Args: 

        obj -- string or object to be deserialized 

        objClass -- class literal for deserialzied object, or string 

            of class name 

    Returns: 

        object -- deserialized object""" 

 

    # Have to accept objClass as string or actual type. Type could be a 

    # native Python type, or one of the model classes. 

    if type(objClass) == str: 

      if 'list[' in objClass: 

        match = re.match('list\[(.*)\]', objClass) 

        subClass = match.group(1) 

        return [self.deserialize(subObj, subClass) for subObj in obj] 

 

      if (objClass in ['int', 'float', 'long', 'dict', 'list', 'str', 'bool', 'datetime']): 

        objClass = ast.literal_eval(objClass) 

      else:  # not a native type, must be model class 

        objClass = ast.literal_eval(objClass + '.' + objClass) 

 

    if objClass in [int, long, float, dict, list, str, bool]: 

      return objClass(obj) 

    elif objClass == datetime: 

      return self.__parse_string_to_datetime(obj) 

 

    instance = objClass() 

 

    for attr, attrType in instance.swaggerTypes.iteritems(): 

        if obj is not None and instance.attributeMap[attr] in obj and type(obj) in [list, dict]: 

          value = obj[instance.attributeMap[attr]] 

          if attrType in ['str', 'int', 'long', 'float', 'bool']: 

            attrType = ast.literal_eval(attrType) 

            try: 

              value = attrType(value) 

            except UnicodeEncodeError: 

              value = unicode(value) 

            except TypeError: 

              value = value 

            setattr(instance, attr, value) 

          elif (attrType == 'datetime'): 

            setattr(instance, attr, self.__parse_string_to_datetime(value)) 

          elif 'list[' in attrType: 

            match = re.match('list\[(.*)\]', attrType) 

            subClass = match.group(1) 

            subValues = [] 

            if not value: 

              setattr(instance, attr, None) 

            else: 

              for subValue in value: 

                subValues.append(self.deserialize(subValue, subClass)) 

            setattr(instance, attr, subValues) 

          else: 

            setattr(instance, attr, self.deserialize(value, attrType)) 

 

    return instance 

 

  def __parse_string_to_datetime(self, string): 

    """ 

    Parse datetime in string to datetime. 

 

    The string should be in iso8601 datetime format. 

    """ 

    try: 

        from dateutil.parser import parse 

        return parse(string) 

    except ImportError: 

        return string 

 

class MethodRequest(urllib2.Request): 

  def __init__(self, *args, **kwargs): 

    """Construct a MethodRequest. Usage is the same as for 

    `urllib2.Request` except it also takes an optional `method` 

    keyword argument. If supplied, `method` will be used instead of 

    the default.""" 

 

    if 'method' in kwargs: 

      self.method = kwargs.pop('method') 

    return urllib2.Request.__init__(self, *args, **kwargs) 

 

  def get_method(self): 

    return getattr(self, 'method', urllib2.Request.get_method(self))