Пример #1
0
    def get_url(self, util, segments=[], settings={}):
        """Get a requested URL, with any additional segments or settings.

        Parameters
        ----------
        util : str
            Which utility to get the URL for.
        segments : list of str, optional
            Any additional segments to add to the URL.
        settings : dict, optional
            Any additional settings to add to the URL.

        Returns
        -------
        full_url : str
            The requested URL, with any extra segments and settings added.
        """

        if not util in self.utils.keys():
            self.build_url(util)

        url = self.urls[util]
        settings_join = '?' if not '?' in url else '&'

        full_url = url + make_segments(segments) + make_settings(
            settings, settings_join)

        return full_url
Пример #2
0
    def build_url(self, util, segments=[], settings=[]):
        """Build the URL for a specified utility, with provided settings.

        Parameters
        ----------
        util : str
            Which utility to build the URL for.
        segments : list of str
            Segments to add to the URL.
        settings : dict or list of str
            Settings to use to build the URL.
            If list, the settings values are taken from the objects settings attribute.
        """

        self._check_util(util)

        if isinstance(settings, list):
            if not all(el in self.settings.keys() for el in settings):
                raise ValueError(
                    'Not all requested settings available - can not proceed.')
            settings = {
                ke: va
                for ke, va in self.settings.items() if ke in settings
            }

        url = self.base + make_segments([self.utils[util]] +
                                        segments) + make_settings(settings)

        if self.authenticated:
            url = self.authenticate(url)

        self.urls[util] = url
Пример #3
0
    def build_url(self, util, segments=None, settings=None):
        """Build the URL for a specified utility, with provided settings.

        Parameters
        ----------
        util : str
            Which utility to build the URL for.
        segments : list of str, optional
            Segments to add to the URL.
        settings : dict or list of str, optional
            Settings to use to build the URL.
            If list, the settings values are taken from the objects settings attribute.

        Examples
        --------
        Build the url for the Github API to search for a repository search:

        >>> urls = URLs('https://api.github.com', {'search_repos': "search/repositories"})
        >>> urls.fill_settings(q='lisc', sort='stars', order='desc')
        >>> urls.build_url('search_repos', settings=['q', 'sort', 'order'])
        """

        self._check_util(util)

        if isinstance(settings, list):
            if not all(el in self.settings.keys() for el in settings):
                raise ValueError(
                    'Not all requested settings available - can not proceed.')
            settings = {
                ke: va
                for ke, va in self.settings.items() if ke in settings
            }

        url = self.base + make_segments([self.utils[util]] + check_none(segments, [])) + \
            make_settings(check_none(settings, {}))

        if self.authenticated:
            url = self.authenticate(url)

        self.urls[util] = url
Пример #4
0
    def get_url(self, util, segments=None, settings=None):
        """Get a requested URL, with any additional segments or settings.

        Parameters
        ----------
        util : str
            Which utility to get the URL for.
        segments : list of str, optional
            Any additional segments to add to the URL.
        settings : dict, optional
            Any additional settings to add to the URL.

        Returns
        -------
        full_url : str
            The requested URL, with any extra segments and settings added.

        Examples
        --------
        Get the url built for a Github repository search:

        >>> urls = URLs('https://api.github.com', {'search_repos': "search/repositories"})
        >>> urls.fill_settings(q='lisc', sort='stars', order='desc')
        >>> urls.build_url('search_repos', settings=['q', 'sort', 'order'])
        >>> urls.get_url('search_repos')
        'https://api.github.com/search/repositories?q=lisc&sort=stars&order=desc'
        """

        if not util in self.utils.keys():
            self.build_url(util)

        url = self.urls[util]
        settings_join = '?' if not '?' in url else '&'

        full_url = url + make_segments(check_none(segments, [])) + \
            make_settings(check_none(settings, {}), settings_join)

        return full_url