Beispiel #1
0
    def test_request_headers(self):
        mobile_str = "mobile.twitter.com"
        url = "http://www.twitter.com"
        request = urllib2.Request(url,
                                  headers={"USER_AGENT": "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/1A542a Safari/419.3"})

        content = urllib2.urlopen(request)
        self.assertTrue(mobile_str in str(content))

        request = urllib2.Request(url)
        content = urllib2.urlopen(request)
        self.assertTrue(mobile_str not in str(content))
Beispiel #2
0
def update_profile_fb(request, passenger_id, next):
    """
    Callback after passenger approved us to get his data from fb
    """
    logging.info("update facebook profile for passenger[%s]" % passenger_id)

    from ordering.models import Passenger
    passenger = Passenger.by_id(passenger_id)
    if passenger and request.GET.get('code'):
        args = {
            'client_id': settings.FACEBOOK_APP_ID,
            # redirect_uri MUST match the one passed in step1 (redirecting to fb)
            'redirect_uri': "http://%s%s" % (DEFAULT_DOMAIN, reverse(update_profile_fb, args=[passenger.id, next])),
            'client_secret': settings.FACEBOOK_APP_SECRET,
            'code': request.GET['code'],
            }

        url = 'https://graph.facebook.com/oauth/access_token?' +\
              urllib.urlencode(args)

        try:
            response = cgi.parse_qs(urllib.urlopen(url).read())
            logging.info("FB query: %s" % url)
            logging.info("FB response: %s" % response)

            access_token = response['access_token'][0]

            facebook_session = FacebookSession(access_token=access_token)
            profile = facebook_session.query('me', fields=['id', 'email', 'first_name', 'last_name', 'picture'])
            passenger.picture_url = profile['picture']['data']['url']
            passenger.fb_id = profile['id']
            passenger.save()
            logging.info("passenger picture updated: %s" % passenger.picture_url)
        except Exception, e:
            pass
Beispiel #3
0
def waze_calculate_time_and_distance(from_lon,
                                     from_lat,
                                     to_lon,
                                     to_lat,
                                     return_cities=False,
                                     return_streets=False):
    """
    Uses the Waze API (http://www.waze.co.il/RoutingManager/routingRequest)
    to calculate the time & distance between the 2 given locations.
    Note that only the 1st alternative path Waze provides is considered.
    """
    url = 'http://www.waze.co.il/RoutingManager/routingRequest?' + \
            'from=x:%s+y:%s+bd:true' % (from_lon, from_lat) + \
            '&to=x:%s+y:%s+bd:true+st_id:46317' % (to_lon, to_lat) + \
            '&returnJSON=true&returnGeometries=true&returnInstructions=false' + \
            '&timeout=60000&nPaths=1&token=%s' % settings.WAZE_API_TOKEN
    json = urllib2.urlopen(url, deadline=10).read()
    result = simplejson.loads(json)

    t, d = 0, 0
    for segment in result["response"]["results"]:
        t = t + segment["crossTime"]
        d = d + segment["length"]
    result = {"estimated_distance": d, "estimated_duration": t}

    return result
Beispiel #4
0
    def test_request_headers(self):
        mobile_str = "mobile.twitter.com"
        url = "http://www.twitter.com"
        request = urllib2.Request(
            url,
            headers={
                "USER_AGENT":
                "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/1A542a Safari/419.3"
            })

        content = urllib2.urlopen(request)
        self.assertTrue(mobile_str in str(content))

        request = urllib2.Request(url)
        content = urllib2.urlopen(request)
        self.assertTrue(mobile_str not in str(content))
Beispiel #5
0
 def fetch_request_token(self, **kwargs):
     oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_url=self.request_token_url, parameters=kwargs)
     oauth_request.sign_request(self.signature_method, self.consumer, None)
     params = oauth_request.parameters
     data = urllib.urlencode(params)
     full_url='%s?%s'%(self.request_token_url, data)
     response = urllib2.urlopen(full_url)
     return oauth.OAuthToken.from_string(response.read())
Beispiel #6
0
 def access_resource(self, url, token, **kwargs):
     oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=token, http_url=url, parameters = kwargs)
     oauth_request.sign_request(self.signature_method, self.consumer, token)
     params = oauth_request.parameters
     data = urllib.urlencode(params)
     full_url='%s?%s'%(url, data)
     response = urllib2.urlopen(full_url)
     return response
Beispiel #7
0
 def authorize_token_url(self, token, callback_url=None):
     oauth_request = oauth.OAuthRequest.from_token_and_callback(token=token, callback=callback_url, http_url=self.authorization_url)
     params = oauth_request.parameters
     data = urllib.urlencode(params)
     full_url='%s?%s'%(self.authorization_url, data)
     return full_url
     response = urllib2.urlopen(full_url)
     return oauth.OAuthToken.from_string(response.read())
Beispiel #8
0
    def authenticate(self, request, user=None):
        cookie = facebook.get_user_from_cookie(request.COOKIES,
                                               FACEBOOK_APP_ID,
                                               FACEBOOK_SECRET_KEY)

        if cookie:
            uid = cookie['uid']
            access_token = cookie['access_token']
        else:
            # if cookie does not exist
            # assume logging in normal way
            params = {}
            params["client_id"] = FACEBOOK_APP_ID
            params["client_secret"] = FACEBOOK_SECRET_KEY
            params["redirect_uri"] = reverse(
                "socialauth_facebook_login_done")[1:]
            params["code"] = request.GET.get('code', '')

            url = "https://graph.facebook.com/oauth/access_token?" + urllib.urlencode(
                params)
            from cgi import parse_qs
            userdata = urllib.urlopen(url).read()
            res_parse_qs = parse_qs(userdata)
            # Could be a bot query
            if not res_parse_qs.has_key('access_token'):
                return None

            parse_data = res_parse_qs['access_token']
            uid = parse_data['uid'][-1]
            access_token = parse_data['access_token'][-1]

        try:
            fb_user = FacebookUserProfile.objects.get(facebook_uid=uid)
            return fb_user.user

        except FacebookUserProfile.DoesNotExist:

            # create new FacebookUserProfile
            graph = facebook.GraphAPI(access_token)
            fb_data = graph.get_object("me")

            if not fb_data:
                return None

            username = uid
            if not user:
                user = User.objects.create(username=username)
                user.first_name = fb_data['first_name']
                user.last_name = fb_data['last_name']
                user.save()

            fb_profile = FacebookUserProfile(facebook_uid=uid, user=user)
            fb_profile.save()

            auth_meta = AuthMeta(user=user, provider='Facebook').save()

            return user
Beispiel #9
0
    def authenticate(self, request, user=None):
        cookie = facebook.get_user_from_cookie(request.COOKIES, FACEBOOK_APP_ID, FACEBOOK_SECRET_KEY)

        if cookie:
            uid = cookie['uid']
            access_token = cookie['access_token']
        else:
            # if cookie does not exist
            # assume logging in normal way
            params = {}
            params["client_id"] = FACEBOOK_APP_ID
            params["client_secret"] = FACEBOOK_SECRET_KEY
            params["redirect_uri"] = reverse("socialauth_facebook_login_done")[1:] 
            params["code"] = request.GET.get('code', '')

            url = "https://graph.facebook.com/oauth/access_token?"+urllib.urlencode(params)
            from cgi import parse_qs
            userdata = urllib.urlopen(url).read()
            res_parse_qs = parse_qs(userdata)
            # Could be a bot query
            if not res_parse_qs.has_key('access_token'):
                return None
                
            parse_data = res_parse_qs['access_token']
            uid = parse_data['uid'][-1]
            access_token = parse_data['access_token'][-1]
            
        try:
            fb_user = FacebookUserProfile.objects.get(facebook_uid=uid)
            return fb_user.user

        except FacebookUserProfile.DoesNotExist:
            
            # create new FacebookUserProfile
            graph = facebook.GraphAPI(access_token) 
            fb_data = graph.get_object("me")

            if not fb_data:
                return None

            username = uid
            if not user:
                user = User.objects.create(username=username)
                user.first_name = fb_data['first_name']
                user.last_name = fb_data['last_name']
                user.save()
                
            fb_profile = FacebookUserProfile(facebook_uid=uid, user=user)
            fb_profile.save()
            
            auth_meta = AuthMeta(user=user, provider='Facebook').save()
                
            return user
Beispiel #10
0
def facebook_login(request):
    from sharing.passenger_controller import post_login_redirect

    error = None
    redirect = reverse(post_login_redirect)

    if request.user.is_authenticated():
        return HttpResponseRedirect(redirect)

    if request.GET:
        if 'code' in request.GET:
            args = {
                'client_id': settings.FACEBOOK_APP_ID,
                'redirect_uri': settings.FACEBOOK_REDIRECT_URI,
                'client_secret': settings.FACEBOOK_APP_SECRET,
                'code': request.GET['code'],
            }

            url = 'https://graph.facebook.com/oauth/access_token?' +\
                  urllib.urlencode(args)
            response = cgi.parse_qs(urllib.urlopen(url).read())

            logging.info("FB query: %s" % url)
            logging.info("FB response: %s" % response)

            access_token = response['access_token'][0]
            expires = response['expires'][0]

            facebook_session = FacebookSession.objects.get_or_create(
                access_token=access_token, )[0]

            facebook_session.expires = expires
            facebook_session.save()

            #            logging.info("saved facebook_session id=%s" % facebook_session.id)

            user = auth.authenticate(token=access_token)
            if user:
                if user.is_active:
                    auth.login(request, user)
                    return HttpResponseRedirect(redirect)
                else:
                    error = 'AUTH_DISABLED'
            else:
                error = 'AUTH_FAILED'
        elif 'error_reason' in request.GET:
            error = 'AUTH_DENIED'

    template_context = {'settings': settings, 'error': error}
    return render_to_response('facebook_login.html',
                              template_context,
                              context_instance=RequestContext(request))
Beispiel #11
0
def facebook_login(request):
    from sharing.passenger_controller import post_login_redirect

    error = None
    redirect = reverse(post_login_redirect)

    if request.user.is_authenticated():
        return HttpResponseRedirect(redirect)

    if request.GET:
        if 'code' in request.GET:
            args = {
                'client_id': settings.FACEBOOK_APP_ID,
                'redirect_uri': settings.FACEBOOK_REDIRECT_URI,
                'client_secret': settings.FACEBOOK_APP_SECRET,
                'code': request.GET['code'],
                }

            url = 'https://graph.facebook.com/oauth/access_token?' +\
                  urllib.urlencode(args)
            response = cgi.parse_qs(urllib.urlopen(url).read())

            logging.info("FB query: %s" % url)
            logging.info("FB response: %s" % response)

            access_token = response['access_token'][0]
            expires = response['expires'][0]

            facebook_session = FacebookSession.objects.get_or_create(
                access_token=access_token,
            )[0]

            facebook_session.expires = expires
            facebook_session.save()

            #            logging.info("saved facebook_session id=%s" % facebook_session.id)

            user = auth.authenticate(token=access_token)
            if user:
                if user.is_active:
                    auth.login(request, user)
                    return HttpResponseRedirect(redirect)
                else:
                    error = 'AUTH_DISABLED'
            else:
                error = 'AUTH_FAILED'
        elif 'error_reason' in request.GET:
            error = 'AUTH_DENIED'

    template_context = {'settings': settings, 'error': error}
    return render_to_response('facebook_login.html', template_context, context_instance=RequestContext(request))
Beispiel #12
0
def get_user_info(api_key, api_secret, cookies):
    user_info_params = {
                                        'method': 'Users.getInfo',
                                        'api_key': api_key,
                                        'call_id': time.time(),
                                        'v': '1.0',
                                        'uids': cookies[api_key + '_user'],
                                        'fields': 'uid,first_name,last_name,pic_small, name, current_location',
                                        'format': 'json',
                                    }

    user_info_hash = get_facebook_signature(api_key, api_secret, user_info_params)
    user_info_params['sig'] = user_info_hash            
    user_info_params = urllib.urlencode(user_info_params)
    user_info_response  = simplejson.load(urllib.urlopen(REST_SERVER, user_info_params))
    return user_info_response
Beispiel #13
0
def get_user_info(api_key, api_secret, cookies):
    user_info_params = {
        'method': 'Users.getInfo',
        'api_key': api_key,
        'call_id': time.time(),
        'v': '1.0',
        'uids': cookies[api_key + '_user'],
        'fields': 'uid,first_name,last_name,pic_small, name, current_location',
        'format': 'json',
    }

    user_info_hash = get_facebook_signature(api_key, api_secret,
                                            user_info_params)
    user_info_params['sig'] = user_info_hash
    user_info_params = urllib.urlencode(user_info_params)
    user_info_response = simplejson.load(
        urllib.urlopen(REST_SERVER, user_info_params))
    return user_info_response
Beispiel #14
0
def update_profile_fb(request, passenger_id, next):
    """
    Callback after passenger approved us to get his data from fb
    """
    logging.info("update facebook profile for passenger[%s]" % passenger_id)

    from ordering.models import Passenger
    passenger = Passenger.by_id(passenger_id)
    if passenger and request.GET.get('code'):
        args = {
            'client_id':
            settings.FACEBOOK_APP_ID,
            # redirect_uri MUST match the one passed in step1 (redirecting to fb)
            'redirect_uri':
            "http://%s%s" %
            (DEFAULT_DOMAIN,
             reverse(update_profile_fb, args=[passenger.id, next])),
            'client_secret':
            settings.FACEBOOK_APP_SECRET,
            'code':
            request.GET['code'],
        }

        url = 'https://graph.facebook.com/oauth/access_token?' +\
              urllib.urlencode(args)

        try:
            response = cgi.parse_qs(urllib.urlopen(url).read())
            logging.info("FB query: %s" % url)
            logging.info("FB response: %s" % response)

            access_token = response['access_token'][0]

            facebook_session = FacebookSession(access_token=access_token)
            profile = facebook_session.query(
                'me',
                fields=['id', 'email', 'first_name', 'last_name', 'picture'])
            passenger.picture_url = profile['picture']['data']['url']
            passenger.fb_id = profile['id']
            passenger.save()
            logging.info("passenger picture updated: %s" %
                         passenger.picture_url)
        except Exception, e:
            pass
Beispiel #15
0
 def _getTrans(self,word):
     if word == '':
         return u''
     if word in self._cache:
         return self._cache[word]
     dest = self._dest
     #param text contains the word to transliterate
     self._postData['text'] = word
     URL = 'http://www.google.com/transliterate/%s'%self._code[dest]
     req = urllib2.Request(URL,urllib.urlencode(self._postData))
     req.add_headers = (self._headerData.items())
     res = urllib2.urlopen(req)
     val = res.read()
     match = '"%s",\n\[\n"([^"]+)'%re.escape(self._postData['text'])
     matcher = re.compile(match)
     target = matcher.findall(val)
     if(len(target) == 0):
         raise TransliterationError, 'Unable to get transliteration of %s'%word
     self._cache[word] = self._getUnicode(target[0])
     return self._cache[word]
Beispiel #16
0
def waze_calculate_time_and_distance(from_lon, from_lat, to_lon, to_lat, return_cities=False, return_streets=False):
    """
    Uses the Waze API (http://www.waze.co.il/RoutingManager/routingRequest)
    to calculate the time & distance between the 2 given locations.
    Note that only the 1st alternative path Waze provides is considered.
    """
    url = 'http://www.waze.co.il/RoutingManager/routingRequest?' + \
            'from=x:%s+y:%s+bd:true' % (from_lon, from_lat) + \
            '&to=x:%s+y:%s+bd:true+st_id:46317' % (to_lon, to_lat) + \
            '&returnJSON=true&returnGeometries=true&returnInstructions=false' + \
            '&timeout=60000&nPaths=1&token=%s' % settings.WAZE_API_TOKEN
    json = urllib2.urlopen(url, deadline=10).read()
    result = simplejson.loads(json)

    t, d = 0, 0
    for segment in result["response"]["results"]:
        t = t + segment["crossTime"]
        d = d + segment["length"]
    result = {
        "estimated_distance":   d,
        "estimated_duration":   t
    }

    return result
Beispiel #17
0
 def test_content_read(self):
     response = urllib2.urlopen("http://www.google.com")
     content = response.read()
     self.assertTrue("google" in content)
Beispiel #18
0
 def test_fetch_url(self):
     content = urllib2.urlopen("http://www.google.com")
     self.assertTrue("google" in str(content))
Beispiel #19
0
 def request(self, url, method, body, headers):
     f = urllib2.urlopen(urllib2.Request(url, body, headers))
     return f.info(), f.read()
Beispiel #20
0
    def wsdl(self, url, debug=False):
        "Parse Web Service Description v1.1"
        soap_ns = {
            "http://schemas.xmlsoap.org/wsdl/soap/": 'soap11',
            "http://schemas.xmlsoap.org/wsdl/soap12/": 'soap12',
        }
        wsdl_uri = "http://schemas.xmlsoap.org/wsdl/"
        xsd_uri = "http://www.w3.org/2001/XMLSchema"
        xsi_uri = "http://www.w3.org/2001/XMLSchema-instance"

        get_local_name = lambda s: str((':' in s) and s.split(':')[1] or s)

        REVERSE_TYPE_MAP = dict([(v, k) for k, v in TYPE_MAP.items()])

        # Open uri and read xml:
        f = urllib2.urlopen(url)
        xml = f.read()
        # Parse WSDL XML:
        wsdl = SimpleXMLElement(xml, namespace=wsdl_uri)

        # detect soap prefix and uri (xmlns attributes of <definitions>)
        xsd_ns = None
        soap_uris = {}
        for k, v in wsdl[:]:
            if v in soap_ns and k.startswith("xmlns:"):
                soap_uris[get_local_name(k)] = v
            if v == xsd_uri and k.startswith("xmlns:"):
                xsd_ns = get_local_name(k)

            # Extract useful data:
        self.namespace = wsdl['targetNamespace']
        self.documentation = unicode(wsdl('documentation', error=False) or '')

        services = {}
        bindings = {}  # binding_name: binding
        operations = {}  # operation_name: operation
        port_type_bindings = {}  # port_type_name: binding
        messages = {}  # message: element
        elements = {}  # element: type def

        for service in wsdl.service:
            service_name = service['name']
            if debug: print "Processing service", service_name
            serv = services.setdefault(service_name, {'ports': {}})
            serv['documentation'] = service['documentation'] or ''
            for port in service.port:
                binding_name = get_local_name(port['binding'])
                address = port('address', ns=soap_uris.values(), error=False)
                location = address and address['location'] or None
                soap_uri = address and soap_uris.get(address.get_prefix())
                soap_ver = soap_uri and soap_ns.get(soap_uri)
                bindings[binding_name] = {
                    'service_name': service_name,
                    'location': location,
                    'soap_uri': soap_uri,
                    'soap_ver': soap_ver,
                }
                serv['ports'][port['name']] = bindings[binding_name]

        for binding in wsdl.binding:
            binding_name = binding['name']
            if debug: print "Processing binding", service_name
            soap_binding = binding('binding',
                                   ns=soap_uris.values(),
                                   error=False)
            transport = soap_binding and soap_binding['transport'] or None
            port_type_name = get_local_name(binding['type'])
            bindings[binding_name].update({
                'port_type_name': port_type_name,
                'transport': transport,
                'operations': {},
            })
            port_type_bindings[port_type_name] = bindings[binding_name]
            for operation in binding.operation:
                op_name = operation['name']
                op = operation('operation', ns=soap_uris.values(), error=False)
                action = op and op['soapAction']
                d = operations.setdefault(op_name, {})
                bindings[binding_name]['operations'][op_name] = d
                d.update({'name': op_name})
                #if action: #TODO: separe operation_binding from operation
                d["action"] = action

        def process_element(element_name, children):
            if debug: print "Processing element", element_name
            for tag in children:
                d = OrderedDict()
                for e in tag.children():
                    try:
                        t = e['type'].split(":")
                        if len(t) > 1:
                            ns, type_name = t
                        else:
                            ns, type_name = xsd_ns, t[0]
                        if ns == xsd_ns:
                            # look for the type, None == any
                            fn = REVERSE_TYPE_MAP.get(unicode(type_name), None)
                        else:
                            # complex type, postprocess later
                            fn = elements.setdefault(unicode(type_name),
                                                     OrderedDict())
                        e_name = unicode(e['name'])
                        d[e_name] = fn
                        if e['maxOccurs'] == "unbounded":
                            # it's an array... TODO: compound arrays?
                            d.array = True
                    except:
                        if debug: print sys.exc_info()

                elements.setdefault(element_name, OrderedDict()).update(d)

        for element in wsdl.types("schema", ns=xsd_uri).children():
            if element.get_local_name() in ('element', 'complexType'):
                element_name = unicode(element['name'])
                if debug:
                    print "Parsing Element %s: %s" % (element.get_local_name(),
                                                      element_name)
                if element.get_local_name() == 'complexType':
                    children = element.children()
                else:
                    children = element.children()
                    if children:
                        children = children.children()
                if children:
                    process_element(element_name, children)

        def postprocess_element(elements):
            for k, v in elements.items():
                if isinstance(v, OrderedDict):
                    if v.array:
                        elements[k] = [v]  # convert arrays to python lists
                    if v != elements:  #TODO: fix recursive elements
                        postprocess_element(v)

        postprocess_element(elements)

        for message in wsdl.message:
            if debug: print "Processing message", message['name']
            try:
                part = message('part', error=False)
                element = {}
                if part:
                    element_name = get_local_name(part['element'])
                    element = {element_name: elements.get(element_name)}
                messages[message['name']] = element
            except:
                if debug: print sys.exc_info()

        for port_type in wsdl.portType:
            port_type_name = port_type['name']
            if debug: print "Processing port type", port_type_name
            binding = port_type_bindings[port_type_name]

            for operation in port_type.operation:
                try:
                    op_name = operation['name']
                    op = operations[op_name]
                    op['documentation'] = unicode(
                        operation('documentation', error=False) or '')
                    if binding['soap_ver']:
                        #TODO: separe operation_binding from operation (non SOAP?)
                        input = get_local_name(operation.input['message'])
                        output = get_local_name(operation.output['message'])
                        op['input'] = messages[input]
                        op['output'] = messages[output]
                except:
                    if debug: print sys.exc_info()

        if debug:
            import pprint

            pprint.pprint(services)

        return services
Beispiel #21
0
    def wsdl(self, url, debug=False):
        "Parse Web Service Description v1.1"
        soap_ns = {
            "http://schemas.xmlsoap.org/wsdl/soap/": 'soap11',
            "http://schemas.xmlsoap.org/wsdl/soap12/": 'soap12',
        }
        wsdl_uri = "http://schemas.xmlsoap.org/wsdl/"
        xsd_uri = "http://www.w3.org/2001/XMLSchema"
        xsi_uri = "http://www.w3.org/2001/XMLSchema-instance"

        get_local_name = lambda s: str((':' in s) and s.split(':')[1] or s)

        REVERSE_TYPE_MAP = dict([(v, k) for k, v in TYPE_MAP.items()])

        # Open uri and read xml:
        f = urllib2.urlopen(url)
        xml = f.read()
        # Parse WSDL XML:
        wsdl = SimpleXMLElement(xml, namespace=wsdl_uri)

        # detect soap prefix and uri (xmlns attributes of <definitions>)
        xsd_ns = None
        soap_uris = {}
        for k, v in wsdl[:]:
            if v in soap_ns and k.startswith("xmlns:"):
                soap_uris[get_local_name(k)] = v
            if v == xsd_uri and k.startswith("xmlns:"):
                xsd_ns = get_local_name(k)

            # Extract useful data:
        self.namespace = wsdl['targetNamespace']
        self.documentation = unicode(wsdl('documentation', error=False) or '')

        services = {}
        bindings = {}           # binding_name: binding
        operations = {}         # operation_name: operation
        port_type_bindings = {} # port_type_name: binding
        messages = {}           # message: element
        elements = {}           # element: type def

        for service in wsdl.service:
            service_name = service['name']
            if debug: print "Processing service", service_name
            serv = services.setdefault(service_name, {'ports': {}})
            serv['documentation'] = service['documentation'] or ''
            for port in service.port:
                binding_name = get_local_name(port['binding'])
                address = port('address', ns=soap_uris.values(), error=False)
                location = address and address['location'] or None
                soap_uri = address and soap_uris.get(address.get_prefix())
                soap_ver = soap_uri and soap_ns.get(soap_uri)
                bindings[binding_name] = {'service_name': service_name,
                                          'location': location,
                                          'soap_uri': soap_uri, 'soap_ver': soap_ver,
                }
                serv['ports'][port['name']] = bindings[binding_name]

        for binding in wsdl.binding:
            binding_name = binding['name']
            if debug: print "Processing binding", service_name
            soap_binding = binding('binding', ns=soap_uris.values(), error=False)
            transport = soap_binding and soap_binding['transport'] or None
            port_type_name = get_local_name(binding['type'])
            bindings[binding_name].update({
                                              'port_type_name': port_type_name,
                                              'transport': transport, 'operations': {},
                                              })
            port_type_bindings[port_type_name] = bindings[binding_name]
            for operation in binding.operation:
                op_name = operation['name']
                op = operation('operation', ns=soap_uris.values(), error=False)
                action = op and op['soapAction']
                d = operations.setdefault(op_name, {})
                bindings[binding_name]['operations'][op_name] = d
                d.update({'name': op_name})
                #if action: #TODO: separe operation_binding from operation
                d["action"] = action

        def process_element(element_name, children):
            if debug: print "Processing element", element_name
            for tag in children:
                d = OrderedDict()
                for e in tag.children():
                    try:
                        t = e['type'].split(":")
                        if len(t) > 1:
                            ns, type_name = t
                        else:
                            ns, type_name = xsd_ns, t[0]
                        if ns == xsd_ns:
                        # look for the type, None == any
                            fn = REVERSE_TYPE_MAP.get(unicode(type_name), None)
                        else:
                        # complex type, postprocess later
                            fn = elements.setdefault(unicode(type_name), OrderedDict())
                        e_name = unicode(e['name'])
                        d[e_name] = fn
                        if e['maxOccurs'] == "unbounded":
                        # it's an array... TODO: compound arrays?
                            d.array = True
                    except:
                        if debug: print sys.exc_info()

                elements.setdefault(element_name, OrderedDict()).update(d)

        for element in wsdl.types("schema", ns=xsd_uri).children():
            if element.get_local_name() in ('element', 'complexType'):
                element_name = unicode(element['name'])
                if debug: print "Parsing Element %s: %s" % (element.get_local_name(), element_name)
                if element.get_local_name() == 'complexType':
                    children = element.children()
                else:
                    children = element.children()
                    if children:
                        children = children.children()
                if children:
                    process_element(element_name, children)

        def postprocess_element(elements):
            for k, v in elements.items():
                if isinstance(v, OrderedDict):
                    if v.array:
                        elements[k] = [v] # convert arrays to python lists
                    if v != elements: #TODO: fix recursive elements
                        postprocess_element(v)

        postprocess_element(elements)

        for message in wsdl.message:
            if debug: print "Processing message", message['name']
            try:
                part = message('part', error=False)
                element = {}
                if part:
                    element_name = get_local_name(part['element'])
                    element = {element_name: elements.get(element_name)}
                messages[message['name']] = element
            except:
                if debug: print sys.exc_info()

        for port_type in wsdl.portType:
            port_type_name = port_type['name']
            if debug: print "Processing port type", port_type_name
            binding = port_type_bindings[port_type_name]

            for operation in port_type.operation:
                try:
                    op_name = operation['name']
                    op = operations[op_name]
                    op['documentation'] = unicode(operation('documentation', error=False) or '')
                    if binding['soap_ver']:
                    #TODO: separe operation_binding from operation (non SOAP?)
                        input = get_local_name(operation.input['message'])
                        output = get_local_name(operation.output['message'])
                        op['input'] = messages[input]
                        op['output'] = messages[output]
                except:
                    if debug: print sys.exc_info()

        if debug:
            import pprint

            pprint.pprint(services)

        return services
Beispiel #22
0
 def request(self, url, method, body, headers):
     f = urllib2.urlopen(urllib2.Request(url, body, headers))
     return f.info(), f.read()
Beispiel #23
0
def talk_to_fb(api_key, api_secret, params):
    sig = get_facebook_signature(api_key, api_secret, params)
    params['sig'] = sig
    data = urllib.urlencode(params)
    response = simplejson.load(urllib.urlopen(REST_SERVER, data))
    return response
Beispiel #24
0
 def test_content_read(self):
     response = urllib2.urlopen("http://www.google.com")
     content = response.read()
     self.assertTrue("google" in content)
Beispiel #25
0
def talk_to_fb(api_key, api_secret, params):
    sig = get_facebook_signature(api_key, api_secret, params)
    params['sig'] = sig
    data = urllib.urlencode(params)
    response = simplejson.load(urllib.urlopen(REST_SERVER, data))
    return response
Beispiel #26
0
 def test_fetch_url(self):
     content = urllib2.urlopen("http://www.google.com")
     self.assertTrue("google" in str(content))