bmSearch  0.0.4
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
requestToELK.requestToELK Class Reference

Public Member Functions

def __init__ (self)
 
def retrieveBmsearchId (self, msgid)
 
def sendData (self, data)
 
def setParam (self, kwargs)
 

Public Attributes

 dates
 
 headers
 
 params
 
 timeInterval
 

Static Public Attributes

 APIURL = config.APIURL
 
 res
 

Detailed Description

requestToELK:
        Class to send HTTP request (get/post) to an ELK platform

Constructor & Destructor Documentation

def requestToELK.requestToELK.__init__ (   self)
__init__ :
       Set default headers
       Set a (dict) dates from bmDate
35  def __init__(self):
36  """
37  __init__ :
38  Set default headers
39  Set a (dict) dates from bmDate
40  """
41 
42  self.params = defaultdict(dict)
43 
44  self.headers = {'Content-Type': "application/json; charset=utf-8", 'Accept': "application/json"}
45  self.params['headers'] = self.headers
46 
47  if hasattr(config,'user') and hasattr(config,'password'):
48  self.params['auth'] = []
49  self.params['auth'][0] = config.user
50  self.params['auth'][1] = config.password
51 
52  if hasattr(config,'verifyPeer'):
53  self.params['verify'] = config.verifyPeer
54 
55 
56  if hasattr(config,'serverCert'):
57  self.params['cert'] = config.serverCert
58 
59 
60 
61 
62 
63  """
64  DATE
65  """
66  self.dates = bmDate().getDates()
67 
68 
69 
Definition: bmDate.py:1

Member Function Documentation

def requestToELK.requestToELK.retrieveBmsearchId (   self,
  msgid 
)
retrieveBmsearchId :
        Try to retrieve Elasticsearch storage elements/informations (_id, _index and _type) from (str) msgid search request. 
126  def retrieveBmsearchId(self, msgid):
127  """
128  retrieveBmsearchId :
129  Try to retrieve Elasticsearch storage elements/informations (_id, _index and _type) from (str) msgid search request.
130  """
131  self._id = ""
132  self._type = ""
133  self._index = ""
134 
135  url = self.APIURL + "/bmsearch-*/_search"
136  #dataS = dataS ='{"_source": "false", "query" : { "constant_score" : { "filter" : { "term" : { "msgid": "' + msgid + '" }}}}}'
137  dataS='{ "_source": "false", "query": { "match_phrase": { "msgid": { "query": "' + msgid + '"}}}}'
138  try:
139  res = requests.post(url, dataS, **self.params).json()
140 
141  if res['hits']['total'] == 1 :
142  header=dict(res['hits']['hits'][0])
143  self._id = header['_id']
144  self._type = header['_type']
145  self._index = header['_index']
146 
147  except Exception as error:
148  pass
149 
150 
151 
152  def replaceDSNRecip(self, dataU):
153  """
154  replaceDSNRecip:
155  Try to update the Elasticsearch entry with data stored in dict dataU
156  """
157  if len(self._id) > 0 :
158 
159 
160  if len(dataU['bouncedRecip']) or len(dataU['deferredRecip']):
161  defList = ', '.join(dataU['deferredRecip'])
162  bounList = ', '.join(dataU['bouncedRecip'])
163  script = '{ "script" : "ctx._source.deferredRecip = [\'' + defList + '\']; ctx._source.bouncedRecip = [\'' + bounList + '\']"}'
164 
165  url = self.APIURL + "/" + self._index + "/" + self._type + "/" + self._id + "/_update"
166 
167  try:
168  r = requests.post(url, script, **self.params)
169 
170  except Exception as error:
171  pass
172 
173 
174 
175  def updateDeliveredRecip(self, deliveredRecip):
176  """
177  updateDeliveredRecip :
178  Try to update an entry in Elasticsearch instance with (str) deliveredRecip var.
179  All elements needed for connection are taken from retrieveBmsearchId() called before.
180 
181  """
182 
183  if len(self._id) > 0 :
184  url = self.APIURL + "/" + self._index + "/" + self._type + "/" + self._id + "/_update"
185  dataU='{"script" : "if (ctx._source.containsKey(\'deliveredRecip\')) { ctx._source.deliveredRecip.add(\'' + deliveredRecip + '\') } else { ctx._source.deliveredRecip = [\'' + deliveredRecip + '\'] }"}'
186  time.sleep(self.timeInterval)
187  try:
188  r = requests.post(url, dataU, **self.params)
189 
190  except Exception as error:
191  pass
192 
193 
194 
195 
196 
def requestToELK.requestToELK.sendData (   self,
  data 
)
sendData :
        Post (dict) data to an ELK platform
        data have not to be JSON formated
109  def sendData(self, data):
110  """
111  sendData :
112  Post (dict) data to an ELK platform
113  data have not to be JSON formated
114  """
115 
116  url = self.APIURL + "/bmsearch-" + self.dates['todayNumELK'] + "/message"
def requestToELK.requestToELK.setParam (   self,
  kwargs 
)
setParam:
        overload params with these in **kwargs.
        Params available are :
            timeInterval
        For details have a look in /etc/bmSearch/config.py
70  def setParam(self, **kwargs):
71  """
72  setParam:
73  overload params with these in **kwargs.
74  Params available are :
75  timeInterval
76  For details have a look in /etc/bmSearch/config.py
77  """
78  self.timeInterval = config.timeInterval
79  if "timeInterval" in kwargs.keys():
80  self.timeInterval = kwargs['timeInterval']
81 
82 
83  if "headers" in kwargs.keys():
84  self.params['headers'] = kwargs['headers']
85 
86  if "user" in kwargs.keys():
87  try :
88  self.params['auth'][0] = kwargs['user']
89  except:
90  self.params['auth'] = []
91  self.params['auth'][0] = kwargs['user']
92 
93  if "password" in kwargs.keys():
94  try :
95  self.params['auth'][1] = kwargs['password']
96  except:
97  self.params['auth'] = []
98  self.params['auth'][1] = kwargs['password']
99 
100 
101  if "verifyPeer" in kwargs.keys():
102  self.params['verify'] = kwargs['verifyPeer']
103 
104 
105  if "serverCert" in kwargs.keys():
106  self.params['cert'] = kwargs['serverCert']
107 
108 

Member Data Documentation

requestToELK.requestToELK.APIURL = config.APIURL
static
requestToELK.requestToELK.dates
requestToELK.requestToELK.headers
requestToELK.requestToELK.params
requestToELK.requestToELK.res
static
requestToELK.requestToELK.timeInterval

The documentation for this class was generated from the following file: