1# -*- test-case-name: openid.test.test_services -*-
2
3from openid.yadis.filters import mkFilter
4from openid.yadis.discover import discover, DiscoveryFailure
5from openid.yadis.etxrd import parseXRDS, iterServices, XRDSError
6
7
8def getServiceEndpoints(input_url, flt=None):
9    """Perform the Yadis protocol on the input URL and return an
10    iterable of resulting endpoint objects.
11
12    @param flt: A filter object or something that is convertable to
13        a filter object (using mkFilter) that will be used to generate
14        endpoint objects. This defaults to generating BasicEndpoint
15        objects.
16
17    @param input_url: The URL on which to perform the Yadis protocol
18
19    @return: The normalized identity URL and an iterable of endpoint
20        objects generated by the filter function.
21
22    @rtype: (str, [endpoint])
23
24    @raises DiscoveryFailure: when Yadis fails to obtain an XRDS document.
25    """
26    result = discover(input_url)
27    try:
28        endpoints = applyFilter(result.normalized_uri, result.response_text,
29                                flt)
30    except XRDSError as err:
31        raise DiscoveryFailure(str(err), None)
32    return (result.normalized_uri, endpoints)
33
34
35def applyFilter(normalized_uri, xrd_data, flt=None):
36    """Generate an iterable of endpoint objects given this input data,
37    presumably from the result of performing the Yadis protocol.
38
39    @param normalized_uri: The input URL, after following redirects,
40        as in the Yadis protocol.
41
42
43    @param xrd_data: The XML text the XRDS file fetched from the
44        normalized URI.
45    @type xrd_data: str
46
47    """
48    flt = mkFilter(flt)
49    et = parseXRDS(xrd_data)
50
51    endpoints = []
52    for service_element in iterServices(et):
53        endpoints.extend(
54            flt.getServiceEndpoints(normalized_uri, service_element))
55
56    return endpoints
57