Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to specify region because of the Hyphen #35

Closed
Vad1mo opened this issue Sep 25, 2020 · 3 comments
Closed

Not able to specify region because of the Hyphen #35

Vad1mo opened this issue Sep 25, 2020 · 3 comments

Comments

@Vad1mo
Copy link

Vad1mo commented Sep 25, 2020

Trying to list the RDB instances but I have difficulties to specify the region. That -Hyphen seems to be the problem, but I am not Python expert enough figure out how to escape is correctly.

api = API(base_url="https://api.scaleway.com", auth_token="yo")
api.make_requests_session()
instances = api.query().rdb.v1.regions.fr-par1.instances.get()
Traceback (most recent call last):
  File "executeOrder.py", line 11, in <module>
    instances = api.query().rdb.v1.regions.fr-par1.instances.get()
NameError: name 'par1' is not defined
@Alkindi42
Copy link

Alkindi42 commented Sep 26, 2020

Hello,

List rdb instances:

api = API(base_url="https://api.scaleway.com", auth_token="your_token")
instances = api.query().rdb.v1.regions.__call__('fr-par').instances.get()

The rdb product is regional, you can only use fr-par or nl-ams.

@Vad1mo Vad1mo closed this as completed Sep 28, 2020
@Vad1mo
Copy link
Author

Vad1mo commented Sep 28, 2020

adding this to improve the documentation would be nice

@brmzkw
Copy link
Contributor

brmzkw commented Sep 28, 2020

Better, you can create your own RDBAPI (until they add it in the library):

from scaleway.apis import API


REGIONS = {
    'fr-par': {
        'url': 'https://api.scaleway.com/rdb/v1/regions/fr-par/',
    },
    'nl-ams': {
        'url': 'https://api.scaleway.com/rdb/v1/regions/nl-ams/',
    }
}


class RDBAPI(API):
    def __init__(self, **kwargs):
        region = kwargs.pop('region', None)
        base_url = kwargs.pop('base_url', None)

        assert region is None or base_url is None, \
            "Specify either region or base_url, not both."

        if base_url is None:
            region = region or 'fr-par'

            assert region in REGIONS, \
                "'%s' is not a valid Scaleway region." % region

            base_url = REGIONS.get(region)['url']

        super(RDBAPI, self).__init__(base_url=base_url, **kwargs)

Then use it as any other API:

>>> rdb = RDBAPI(auth_token='xx', region='fr-par')
# or
>>> rdb = RDBAPI(auth_token='xx', region='nl-ams')

>>> rdb.query().instances.get()
{'instances': [], 'total_count': 0}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants