Repository navigation
Expand file tree
/
Copy pathRequest.py
More file actions
86 lines (63 loc) · 2.56 KB
/
Copy pathRequest.py
File metadata and controls
86 lines (63 loc) · 2.56 KB
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
# -*- coding: utf-8 -*-
from logging import getLogger
class ResponseException(Exception):
def __init__(self, code, content=""):
Exception.__init__(self, "Server response error: %s %s" % (code, content))
self.code = code
class Request(object):
""" Abstract class to support different types of request, most methods should be overwritten """
__version__ = "0.1"
#: Class that will be instantiated and associated with the request, and if needed copied and reused
CONTEXT_CLASS = None
def __init__(self, config, context=None, options=None):
self.log = getLogger("log")
# Global config, holds some configurable parameter
self.config = config
# Create a new context if not given
if context is None and self.CONTEXT_CLASS is not None:
self.context = self.CONTEXT_CLASS()
else:
self.context = context
# Store options in dict
self.options = {} if options is None else options
# Last response code
self.code = 0
self.doAbort = False
self.initContext()
# TODO: content encoding? Could be handled globally
def initContext(self):
""" Should be used to initialize everything from given context and options """
pass
def getContext(self):
""" Retrieves complete state that is needed to copy the request context """
return self.config, self.context, self.options
def setContext(self, *args):
""" Sets request context """
self.config, self.context, self.options = args
def setOption(self, name, value):
""" Sets an option """
self.options[name] = value
def unsetOption(self, name):
""" Removes a specific option or reset everything on empty string """
if name == "":
self.options.clear()
else:
if name in self.options:
del self.options[name]
def addAuth(self, user, pwd):
""" Adds authentication information to the request """
self.options["auth"] = user + ":" + pwd
def removeAuth(self):
""" Removes authentication from the request """
self.unsetOption("auth")
def load(self, uri, *args, **kwargs):
""" Loads given resource from given uri. Args and kwargs depends on implementation"""
raise NotImplementedError
def abort(self):
self.doAbort = True
def reset(self):
""" Resets the context to initial state """
self.unsetOption("")
def close(self):
""" Close and clean everything """
pass