def _baseControl(self, opt): resource = 'channel' if (opt.has_key(Channel.CHANNEL_ID)): if (opt[Channel.CHANNEL_ID] is not None and opt[Channel.METHOD] not in self._method_channel_in_body): resource = opt[Channel.CHANNEL_ID] del opt[Channel.CHANNEL_ID] host = opt[Channel.HOST] del opt[Channel.HOST] url = 'http://' + host + '/rest/2.0/' + Channel.PRODUCT + '/' url += resource http_method = 'POST' opt[Channel.SIGN] = self._genSign(http_method, url, opt) request = RequestCore(url) headers = dict() headers['Content-Type'] = 'application/x-www-form-urlencoded' headers['User-Agent'] = 'Baidu Channel Service Pythonsdk Client' for (headerKey, headerValue) in headers.items(): headerValue = headerValue.replace('\r', '') headerValue = headerValue.replace('\n', '') if (headerValue is not None): request.add_header(headerKey, headerValue) request.set_method(http_method) request.set_body(urllib.urlencode(opt)) if (isinstance(self._curlOpts, dict)): request.set_curlopts(self._curlOpts) request.handle_request() return ResponseCore(request.get_response_header(), request.get_response_body(), request.get_response_code())
def _baseControl(self, opt): resource = 'channel' if( opt.has_key(Channel.CHANNEL_ID) ): if(opt[Channel.CHANNEL_ID] is not None and opt[Channel.METHOD] not in self._method_channel_in_body): resource = opt[Channel.CHANNEL_ID] del opt[Channel.CHANNEL_ID] host = opt[Channel.HOST] del opt[Channel.HOST] url = 'http://' + host + '/rest/2.0/' + Channel.PRODUCT + '/' url += resource http_method = 'POST' opt[Channel.SIGN] = self._genSign(http_method, url, opt) request = RequestCore(url) headers = dict() headers['Content-Type'] = 'application/x-www-form-urlencoded' headers['User-Agent'] = 'Baidu Channel Service Pythonsdk Client' for (headerKey , headerValue) in headers.items(): headerValue = headerValue.replace('\r', '') headerValue = headerValue.replace('\n', '') if (headerValue is not None): request.add_header(headerKey, headerValue) request.set_method(http_method) request.set_body(urllib.urlencode(opt)) if(isinstance(self._curlOpts, dict)): request.set_curlopts(self._curlOpts) request.handle_request() return ResponseCore(request.get_response_header(), request.get_response_body(), request.get_response_code())
def send_request1(self, opt): request = RequestCore("http://channel.api.duapp.com/rest/2.0/channel/channel") headers = dict() headers['Content-Type'] = 'application/x-www-form-urlencoded' headers['User-Agent'] = 'Baidu Channel Service Pythonsdk Client' for (headerKey , headerValue) in headers.items(): headerValue = headerValue.replace('\r', '') headerValue = headerValue.replace('\n', '') if (headerValue is not None): request.add_header(headerKey, headerValue) request.set_method('POST') request.set_body(urllib.urlencode(opt)) if(isinstance(self._curlOpts, dict)): request.set_curlopts(self._curlOpts) request.handle_request() ret = ResponseCore(request.get_response_header(),request.get_response_body(),request.get_response_code()) if( ret is None): raise ChannelException('base control returned None object', Channel.CHANNEL_SDK_SYS) if(ret.isOK()): result = ret.body print result if (result is None): raise ChannelException(ret.body, Channel.CHANNEL_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR) #self._requestId = result['request_id'] return result result = json.loads(ret.body) if(result is None): raise ChannelException('ret body:' + ret.body, Channel.CHANNEL_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR) self._requestId = result['request_id'] raise ChannelException(result['error_msg'], result['error_code'])