Changeset 1504:83638760290c
- Timestamp:
- 03/09/10 21:16:01 (5 months ago)
- Author:
- Fritz
- Branch:
- default
- Message:
-
REST : start replacing print by logging feature. Move /account/user/auth to /account/auth
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r1503
|
r1504
|
|
| 88 | 88 | self._log.info("Rest Server initialisation...") |
| 89 | 89 | self._log.debug("locale : %s %s" % locale.getdefaultlocale()) |
| | 90 | # logging data manipulation initialization |
| | 91 | log_dm = logger.Logger('REST-DM') |
| | 92 | self._log_dm = log_dm.get_logger() |
| | 93 | self._log_dm.info("#Rest Server Data Manipulation...") |
| | 94 | self._log_dm.debug("locale : %s %s" % locale.getdefaultlocale()) |
| 90 | 95 | # DB Helper |
| 91 | 96 | self._db = DbHelper() |
| … |
… |
|
| 110 | 115 | self.server_ip = server_ip |
| 111 | 116 | self.server_port = server_port |
| | 117 | self._log.info("Configuration : ip:port = %s:%s" % (self.server_ip, self.server_port)) |
| 112 | 118 | |
| 113 | 119 | |
| 114 | 120 | # Queues config |
| | 121 | self._log.debug("Get queues configuration") |
| 115 | 122 | self._config = Query(self._myxpl) |
| 116 | 123 | res = xPLResult() |
| … |
… |
|
| 142 | 149 | |
| 143 | 150 | # define listeners for queues |
| | 151 | self._log.debug("Create listeners") |
| 144 | 152 | Listener(self._add_to_queue_system_list, self._myxpl, \ |
| 145 | 153 | {'schema': 'domogik.system', |
| … |
… |
|
| 163 | 171 | 'host' : gethostname()}) |
| 164 | 172 | |
| | 173 | self._log.info("Initialisation OK") |
| | 174 | |
| 165 | 175 | |
| 166 | 176 | def _add_to_queue_system_list(self, message): |
| … |
… |
|
| 189 | 199 | @param nb_rec : internal parameter (do not use it for first call). Used to check recursivity VS queue size |
| 190 | 200 | """ |
| | 201 | self._log.debug("Get from queue : %s (recursivity deepth : %s)" % (str(my_queue), nb_rec)) |
| 191 | 202 | # check if recursivity doesn't exceed queue size |
| 192 | 203 | if nb_rec > self._queue_size: |
| 193 | | print "_get_from_queue : number of call exceed queue size (%s) : return None" % self._queue_size |
| | 204 | self._log.warning("Get from queue %s : number of call exceed queue size (%s) : return None" % (str(my_queue), self._queue_size)) |
| 194 | 205 | # we raise an "Empty" exception because we consider that if we don't find |
| 195 | 206 | # the good data, it is as if it was "empty" |
| … |
… |
|
| 202 | 213 | # no filter defined |
| 203 | 214 | if filter == None: |
| | 215 | self._log.debug("Get from queue %s : return %s" % (str(my_queue), str(message))) |
| 204 | 216 | return message |
| 205 | 217 | |
| … |
… |
|
| 214 | 226 | len_data = len(filter_data) - 1 |
| 215 | 227 | if msg_data[0:len_data] != filter_data[0:-1]: |
| 216 | | print "Bad data!" |
| 217 | 228 | keep_data = False |
| 218 | 229 | # normal search |
| 219 | 230 | else: |
| 220 | 231 | if message.data[key] != filter[key]: |
| 221 | | print "Bad data!" |
| 222 | 232 | keep_data = False |
| 223 | 233 | |
| 224 | 234 | # if message is ok for us, return it |
| 225 | 235 | if keep_data == True: |
| | 236 | self._log.debug("Get from queue %s : return %s" % (str(my_queue), str(message))) |
| 226 | 237 | return message |
| 227 | 238 | |
| 228 | 239 | # else, message get back in queue and get another one |
| 229 | 240 | else: |
| | 241 | self._log.debug("Get from queue %s : bad data, check another one..." % (str(my_queue))) |
| 230 | 242 | self._put_in_queue(my_queue, message) |
| 231 | 243 | return self._get_from_queue(my_queue, filter, nb_rec + 1) |
| … |
… |
|
| 233 | 245 | # if message too old : get an other message |
| 234 | 246 | else: |
| | 247 | self._log.debug("Get from queue %s : data too old, check another one..." % (str(my_queue))) |
| 235 | 248 | return self._get_from_queue(my_queue, filter, nb_rec + 1) |
| 236 | 249 | |
| 237 | 250 | def _put_in_queue(self, my_queue, message): |
| | 251 | self._log.debug("Put in queue %s : %s" % (str(my_queue), str(message))) |
| 238 | 252 | my_queue.put((time.time(), message), True, self._queue_timeout) |
| 239 | 253 | |
| … |
… |
|
| 244 | 258 | """ |
| 245 | 259 | # Start HTTP server |
| | 260 | self._log.info("Start HTTP Server on %s:%s..." % (self.server_ip, self.server_port)) |
| 246 | 261 | server = HTTPServerWithParam((self.server_ip, int(self.server_port)), RestHandler, \ |
| 247 | 262 | handler_params = [self]) |
| 248 | | print 'Start REST server on %s:%s...' % (self.server_ip, self.server_port) |
| 249 | 263 | server.serve_forever() |
| 250 | 264 | |
| … |
… |
|
| 286 | 300 | Call directly .do_for_all_methods() |
| 287 | 301 | """ |
| 288 | | print "==== GET ============================================" |
| 289 | 302 | self.do_for_all_methods() |
| 290 | 303 | |
| … |
… |
|
| 293 | 306 | Call directly .do_for_all_methods() |
| 294 | 307 | """ |
| 295 | | print "==== POST ===========================================" |
| 296 | 308 | self.do_for_all_methods() |
| 297 | 309 | |
| … |
… |
|
| 300 | 312 | Call directly .do_for_all_methods() |
| 301 | 313 | """ |
| 302 | | print "==== OPTIONS ===========================================" |
| 303 | 314 | self.do_for_all_methods() |
| 304 | 315 | |
| … |
… |
|
| 307 | 318 | the REST url |
| 308 | 319 | """ |
| 309 | | |
| 310 | 320 | # dirty issue to force HTTP/1.1 |
| 311 | 321 | self.protocol_version = 'HTTP/1.1' |
| 312 | 322 | self.request_version = 'HTTP/1.1' |
| 313 | 323 | |
| 314 | | |
| | 324 | # TODO : create a thread here |
| 315 | 325 | request = ProcessRequest(self.server.handler_params, self.path, \ |
| 316 | 326 | self.send_http_response_ok, \ |
| 317 | 327 | self.send_http_response_error) |
| 318 | 328 | request.do_for_all_methods() |
| | 329 | |
| 319 | 330 | |
| 320 | 331 | |
| … |
… |
|
| 331 | 342 | @param data : json data to display |
| 332 | 343 | """ |
| | 344 | # TODO : log!! |
| | 345 | #self._log.debug("Send HTTP header for OK") |
| 333 | 346 | self.send_response(200) |
| 334 | 347 | self.send_header('Content-type', 'application/json') |
| … |
… |
|
| 338 | 351 | self.end_headers() |
| 339 | 352 | if data: |
| | 353 | # TODO : log!! |
| | 354 | #self._log.debug("Send HTTP data : %s" % data.encode("utf-8")) |
| 340 | 355 | self.wfile.write(data.encode("utf-8")) |
| 341 | 356 | |
| … |
… |
|
| 352 | 367 | in jsonp format |
| 353 | 368 | """ |
| | 369 | # TODO : log!! |
| | 370 | #self._log.debug("Send HTTP header for ERROR : code=%s ; msg=%s" % (err_code, err_msg)) |
| 354 | 371 | self.send_response(200) |
| 355 | 372 | self.send_header('Content-type', 'text/html') |
| … |
… |
|
| 358 | 375 | json_data.set_jsonp(jsonp, jsonp_cb) |
| 359 | 376 | self.wfile.write(json_data.get()) |
| | 377 | # TODO : log!! |
| | 378 | #self._log.warning("Error reply : %s" % json_data.get()) |
| 360 | 379 | |
| 361 | 380 | |
| … |
… |
|
| 395 | 414 | self._xml_directory = self.handler_params[0]._xml_directory |
| 396 | 415 | |
| | 416 | self._log.debug("Process request : init") |
| | 417 | |
| 397 | 418 | self._queue_timeout = self.handler_params[0]._queue_timeout |
| 398 | 419 | self._queue_size = self.handler_params[0]._queue_size |
| … |
… |
|
| 412 | 433 | |
| 413 | 434 | # url processing |
| 414 | | print type(self.path).__name__ |
| 415 | 435 | print self.path |
| 416 | | #self.path = str(urllib.unquote(self.path)) |
| 417 | 436 | self.path = urllib.unquote(unicode(self.path)) |
| 418 | | print type(self.path).__name__ |
| 419 | | print self.path |
| 420 | | #self.path = unicode(self.path, "utf-8") |
| | 437 | self._log.info("Request : %s" % self.path) |
| | 438 | |
| | 439 | # TODO log data manipulation here |
| 421 | 440 | |
| 422 | 441 | tab_url = self.path.split("?") |
| … |
… |
|
| 428 | 447 | if self.path[-1:] == "/": |
| 429 | 448 | self.path = self.path[0:len(self.path)-1] |
| 430 | | print "PATH : " + self.path |
| 431 | 449 | tab_path = self.path.split("/") |
| 432 | 450 | |
| … |
… |
|
| 441 | 459 | else: |
| 442 | 460 | self.rest_request = [] |
| 443 | | print "TYPE : " + self.rest_type |
| 444 | | print "Request : " + str(self.rest_request) |
| 445 | | |
| 446 | | |
| 447 | | |
| 448 | | |
| 449 | | |
| 450 | 461 | |
| 451 | 462 | |
| … |
… |
|
| 477 | 488 | """ Process parameters : ...?param1=val1¶m2=val2&.... |
| 478 | 489 | """ |
| | 490 | self._log.debug("Parse request options") |
| 479 | 491 | |
| 480 | 492 | # for each debug option |
| … |
… |
|
| 490 | 502 | # call json specific options |
| 491 | 503 | if opt_key == "callback" and opt_value != None: |
| | 504 | self._log.debug("Option : jsonp mode") |
| 492 | 505 | self.jsonp = True |
| 493 | 506 | self.jsonp_cb = opt_value |
| … |
… |
|
| 502 | 515 | """ Sleep process for 15 seconds |
| 503 | 516 | """ |
| 504 | | print "DEBUG : start sleeping for " + str(duration) |
| | 517 | self._log.debug("Start sleeping for " + str(duration)) |
| 505 | 518 | time.sleep(float(duration)) |
| 506 | | print "DEBUG : end sleeping" |
| | 519 | self._log.debug("End sleeping") |
| 507 | 520 | |
| 508 | 521 | |
| … |
… |
|
| 521 | 534 | json_data.add_data(("Version : 0.1.x", "Status : UP")) |
| 522 | 535 | self.send_http_response_ok(json_data.get()) |
| 523 | | |
| 524 | | |
| 525 | | |
| 526 | | |
| 527 | 536 | |
| 528 | 537 | |
| … |
… |
|
| 538 | 547 | - send appropriate xPL message on network |
| 539 | 548 | """ |
| 540 | | print "Call rest_command" |
| | 549 | self._log.debug("Process command") |
| 541 | 550 | |
| 542 | 551 | # parse data in URL |
| … |
… |
|
| 554 | 563 | self.send_http_response_ok(json_data.get()) |
| 555 | 564 | return |
| 556 | | print "Techno : %s" % techno |
| 557 | | print "Address : %s" % address |
| 558 | | print "Order : %s" % order |
| 559 | | print "Others : %s" % str(others) |
| | 565 | self._log.debug("Techno : %s" % techno) |
| | 566 | self._log.debug("Address : %s" % address) |
| | 567 | self._log.debug("Order : %s" % order) |
| | 568 | self._log.debug("Others : %s" % str(others)) |
| 560 | 569 | |
| 561 | 570 | # open xml file |
| … |
… |
|
| 567 | 576 | return |
| 568 | 577 | |
| 569 | | print "Send message : %s" % message |
| | 578 | self._log.debug("Process command > send message : %s" % str(message)) |
| 570 | 579 | self._myxpl.send(message) |
| 571 | 580 | |
| … |
… |
|
| 576 | 585 | |
| 577 | 586 | |
| 578 | | |
| 579 | | |
| 580 | | |
| 581 | 587 | |
| 582 | 588 | |
| … |
… |
|
| 704 | 710 | - Send message |
| 705 | 711 | """ |
| 706 | | |
| 707 | | print "Call rest_xpl_cmnd" |
| | 712 | self._log.debug("Send xpl message") |
| | 713 | |
| 708 | 714 | if len(self.rest_request) == 0: |
| 709 | 715 | self.send_http_response_error(999, "Schema not given", self.jsonp, self.jsonp_cb) |
| … |
… |
|
| 740 | 746 | return |
| 741 | 747 | |
| 742 | | print "Send message : %s" % message |
| | 748 | self._log.debug("Send message : %s" % str(message)) |
| 743 | 749 | self._myxpl.send(message) |
| 744 | 750 | |
| … |
… |
|
| 760 | 766 | - call the good fonction to get data from database |
| 761 | 767 | """ |
| 762 | | print "Call rest_base_get" |
| | 768 | self._log.debug("Process base request") |
| 763 | 769 | # parameters initialisation |
| 764 | 770 | self.parameters = {} |
| … |
… |
|
| 1328 | 1334 | month = int(date[4:6]) |
| 1329 | 1335 | day = int(date[6:8]) |
| 1330 | | my_date = datetime.date(year, month, day) |
| | 1336 | try: |
| | 1337 | my_date = datetime.date(year, month, day) |
| | 1338 | except: |
| | 1339 | self.send_http_response_error(999, str(sys.exc_info()[1].replace('"', "'")), self.jsonp, self.jsonp_cb) |
| 1331 | 1340 | return my_date |
| 1332 | 1341 | |
| 1333 | 1342 | |
| 1334 | 1343 | |
| 1335 | | |
| | 1344 | ### TODO j'en suis ici pour les _log !! |
| 1336 | 1345 | |
| 1337 | 1346 | ###### |
| … |
… |
|
| 2346 | 2355 | self.parameters = {} |
| 2347 | 2356 | |
| | 2357 | ### auth ##################################### |
| | 2358 | if self.rest_request[0] == "auth": |
| | 2359 | if len(self.rest_request) == 3: |
| | 2360 | self._rest_account_auth(self.rest_request[1], self.rest_request[2]) |
| | 2361 | else: |
| | 2362 | self.send_http_response_error(999, "Wrong syntax for " + self.rest_request[0], \ |
| | 2363 | self.jsonp, self.jsonp_cb) |
| | 2364 | return |
| | 2365 | |
| 2348 | 2366 | ### user ##################################### |
| 2349 | 2367 | if self.rest_request[0] == "user": |
| … |
… |
|
| 2362 | 2380 | self.send_http_response_error(999, "Wrong syntax for " + self.rest_request[1], \ |
| 2363 | 2381 | self.jsonp, self.jsonp_cb) |
| 2364 | | |
| 2365 | | |
| 2366 | | ### auth |
| 2367 | | elif self.rest_request[1] == "auth": |
| 2368 | | if len(self.rest_request) == 4: |
| 2369 | | self._rest_account_auth(self.rest_request[2], self.rest_request[3]) |
| 2370 | | else: |
| 2371 | | self.send_http_response_error(999, "Wrong syntax for " + self.rest_request[1], \ |
| 2372 | | self.jsonp, self.jsonp_cb) |
| 2373 | | return |
| 2374 | 2382 | |
| 2375 | 2383 | ### add |
| … |
… |
|
| 2390 | 2398 | |
| 2391 | 2399 | ### password |
| 2392 | | elif self.res2_request[1] == "password": |
| | 2400 | elif self.rest_request[1] == "password": |
| 2393 | 2401 | offset = 2 |
| 2394 | 2402 | if self.set_parameters(offset): |