Пример #1
0
def update(
    id: int = typer.Argument(..., min=1),
    userId: int = typer.Option(None, help='ID of the user.'),
    title: str = typer.Option(None, help='Title of the todo.'),
    completed: bool = typer.Option(
        False,
        '--completed/--not-completed',
        '-c/-n',
        help='Whether the todo is completed',
        show_default=True,
    ),
):
    """
    Update an todo by ID, omitted fields will be left unchanged.
    """
    todo_response = rest.get(f'{ENDPOINT}/{id}')
    if todo_response.status_code != 200:
        util.panic('Failure to retrieve resource.')
    todo = todo_response.json()
    todo = {
        'userId': userId or todo['userId'],
        'title': title or todo['title'],
        'completed': completed or todo['completed']
    }
    response = rest.put(f'{ENDPOINT}/{id}', todo)
    if response.status_code != 200:
        util.panic('Failure to update resource.')
    typer.echo(view_todo(response.json()))
Пример #2
0
def get(id: int = typer.Argument(..., min=1)):
    """
    Get info about album by ID.
    """
    response: requests.Response = rest.get(f'{ENDPOINT}/{id}')
    if response.status_code != 200:
        util.panic('Failure to retrieve resource.')
    typer.echo(view_album(response.json()))
Пример #3
0
def delete(id: int = typer.Argument(..., min=1)):
    """
    Delete an album by ID.
    """
    response: requests.Response = rest.delete(f'{ENDPOINT}/{id}')
    if response.status_code != 200:
        util.panic('Failure to delete resource')
    typer.echo('Resource deleted successfully.')
Пример #4
0
def list():
    """
    List Users.
    """
    response: requests.Response = rest.get(ENDPOINT)
    if response.status_code != 200:
        util.panic(f"Request resulted in error code {response.status_code}")
    users = response.json()
    typer.echo_via_pager(map(view_user, users))
Пример #5
0
def create(name: str = typer.Option(..., help='Name of the user.'),
           username: str = typer.Option(..., help='Username of the user.'),
           email: str = typer.Option(..., help='Email of the user.'),
           street: str = typer.Option(..., help='Street of the user.'),
           suite: str = typer.Option(..., help='Suite of the user.'),
           city: str = typer.Option(..., help='City of the user.'),
           zipcode: str = typer.Option(..., help='Zipcode of the user.'),
           latitude: float = typer.Option(...,
                                          help='Latitude of the user.',
                                          min=-90.0,
                                          max=90.0,
                                          ),
           longitude: float = typer.Option(...,
                                           help='Longitude of the user.',
                                           min=-180.0,
                                           max=180.0,
                                           ),
           phone: str = typer.Option(..., help='Phone of the user.'),
           website: str = typer.Option(..., help='Website of the user.'),
           company_name: str = typer.Option(...,
                                            '--company-name',
                                            help='Company the user works at.',
                                            ),
           catch_phrase: str = typer.Option(...,
                                            help='Catch phrase of the user\'s company'),
           bs: str = typer.Option(..., help='BS of the user\'s company'),
           ):
    """
    Create a user.
    """
    user = {
        'name': name,
        'username': username,
        'email': email,
        'address': {
            'street': street,
            'suite': suite,
            'city': city,
            'zipcode': zipcode,
            'geo': {
                'lat': latitude,
                'lng': longitude
            }
        },
        'phone': phone,
        'website': website,
        'company': {
            'name': company_name,
            'catchPhrase': catch_phrase,
            'bs': bs
        }
    }
    response: requests.Response = rest.post(
        ENDPOINT, user)
    if response.status_code != 201:
        util.panic('Failure to create resource.')
    typer.echo(view_user(response.json()))
Пример #6
0
def create(
        userId: int = typer.Option(..., help='ID of the user.', min=1),
        title: str = typer.Option(..., help='Title of the album.'),
):
    """
    Create a new album.
    """
    album = {'userId': userId, 'title': title}
    response: requests.Response = rest.post(ENDPOINT, album)
    if response.status_code != 201:
        util.panic('Failure to create resource.')
    typer.echo(view_album(response.json()))
Пример #7
0
def create(
        userId: int = typer.Option(..., help='ID of the user.', min=1),
        title: str = typer.Option(..., help='Title of the post.'),
        body: str = typer.Option(..., help='Body of the post.'),
):
    """
    Create a new post.
    """
    post = {'userId': userId, 'title': title, 'body': body}
    response: requests.Response = rest.post(ENDPOINT, post)
    if response.status_code != 201:
        util.panic('Failure to create resource.')
    typer.echo(view_post(response.json()))
Пример #8
0
def list(userId: int = typer.Option(
    None,
    help='Get albums only from the given user.',
    min=1,
)):
    """
    List Albums.
    """
    uri = ENDPOINT if not userId else f'{ENDPOINT}?userId={userId}'
    response: requests.Response = rest.get(uri)
    if response.status_code != 200:
        util.panic(f"Request resulted in error code {response.status_code}")
    albums = response.json()
    typer.echo_via_pager([f'{view_album(album)}\n' for album in albums])
Пример #9
0
def create(
        postId: int = typer.Option(..., help='ID of the post.', min=1),
        name: str = typer.Option(..., help='Name of the comment.'),
        email: str = typer.Option(..., help='E-mail address'),
        body: str = typer.Option(..., help='Body of the comment.'),
):
    """
    Create a comment.
    """
    comment = {'postId': postId, 'name': name, 'email': email, 'body': body}
    response: requests.Response = rest.post(ENDPOINT, comment)
    if response.status_code != 201:
        util.panic('Failure to create resource.')
    typer.echo(view_comment(response.json()))
Пример #10
0
def list(albumId: int = typer.Option(None,
                                     help='Get photos only from the given album.',
                                     min=1,)
         ):
    """
    List Photos.
    """
    uri = ENDPOINT if not albumId else f'{ENDPOINT}?albumId={albumId}'
    response: requests.Response = rest.get(uri)
    if response.status_code != 200:
        util.panic(f"Request resulted in error code {response.status_code}")
    photos = response.json()
    typer.echo_via_pager(
        [f'{view_photo(photo)}\n' for photo in photos])
Пример #11
0
def list(postId: int = typer.Option(
    None,
    help='Get comments only from the given post.',
    min=1,
)):
    """
    List Comments.
    """
    uri = ENDPOINT if not postId else f'{ENDPOINT}?postId={postId}'
    response: requests.Response = rest.get(uri)
    if response.status_code != 200:
        util.panic(f"Request resulted in error code {response.status_code}")
    comments = response.json()
    typer.echo_via_pager(
        [f'{view_comment(comment)}\n' for comment in comments])
Пример #12
0
def create(
    userId: int = typer.Option(..., help='ID of the user.', min=1),
    title: str = typer.Option(..., help='Title of the todo.'),
    completed: bool = typer.Option(
        False,
        '--completed/--not-completed',
        '-c/-n',
        help='Whether the todo is completed',
        show_default=True,
    ),
):
    """
    Create a new todo.
    """
    todo = {'userId': userId, 'title': title, 'completed': completed}
    response: requests.Response = rest.post(ENDPOINT, todo)
    if response.status_code != 201:
        util.panic('Failure to create resource.')
    typer.echo(view_todo(response.json()))
Пример #13
0
def update(
        id: int = typer.Argument(..., min=1),
        userId: int = typer.Option(None, help='ID of the user.'),
        title: str = typer.Option(None, help='Title of the album.'),
):
    """
    Update an album by ID, omitted fields will be left unchanged.
    """
    album_response = rest.get(f'{ENDPOINT}/{id}')
    if album_response.status_code != 200:
        util.panic('Failure to retrieve resource.')
    album = album_response.json()
    album = {
        'userId': userId or album['userId'],
        'title': title or album['title']
    }
    response = rest.put(f'{ENDPOINT}/{id}', album)
    if response.status_code != 200:
        util.panic('Failure to update resource.')
    typer.echo(view_album(response.json()))
Пример #14
0
def create(albumId: int = typer.Option(..., help='ID of the album.', min=1),
           title: str = typer.Option(..., help='Name of the photo.'),
           url: str = typer.Option(..., help='URL of the photo'),
           thumbnailUrl: str = typer.Option(...,
                                            help='Thumbnail URL address of the photo.'),
           ):
    """
    Create a photo.
    """
    photo = {
        'albumId': albumId,
        'title': title,
        'url': url,
        'thumbnailUrl': thumbnailUrl
    }
    response: requests.Response = rest.post(
        ENDPOINT, photo)
    if response.status_code != 201:
        util.panic('Failure to create resource.')
    typer.echo(view_photo(response.json()))
Пример #15
0
def update(
        id: int = typer.Argument(..., min=1),
        userId: int = typer.Option(None, help='ID of the user.'),
        title: str = typer.Option(None, help='Title of the post.'),
        body: str = typer.Option(None, help='Body of the post.'),
):
    """
    Update an post by ID, omitted fields will be left unchanged.
    """
    post_response = rest.get(f'{ENDPOINT}/{id}')
    if post_response.status_code != 200:
        util.panic('Failure to retrieve resource.')
    post = post_response.json()
    post = {
        'userId': userId or post['userId'],
        'title': title or post['title'],
        'body': body or post['body']
    }
    response = rest.put(f'{ENDPOINT}/{id}', post)
    if response.status_code != 200:
        util.panic('Failure to update resource.')
    typer.echo(view_post(response.json()))
Пример #16
0
def update(
        id: int = typer.Argument(..., min=1),
        postId: int = typer.Option(None, help='ID of the post.'),
        name: str = typer.Option(None, help='Name of the comment.'),
        email: str = typer.Option(None, help='E-mail address'),
        body: str = typer.Option(None, help='Body of the comment.'),
):
    """
    Update an comment by ID, omitted fields will be left unchanged.
    """
    comment_response = rest.get(f'{ENDPOINT}/{id}')
    if comment_response.status_code != 200:
        util.panic('Failure to retrieve resource.')
    comment = comment_response.json()
    comment = {
        'postId': postId or comment['postId'],
        'name': name or comment['name'],
        'email': email or comment['email'],
        'body': body or comment['body']
    }
    response = rest.put(f'{ENDPOINT}/{id}', comment)
    if response.status_code != 200:
        util.panic('Failure to update resource.')
    typer.echo(view_comment(response.json()))
Пример #17
0
def update(id: int = typer.Argument(..., min=1),
           albumId: int = typer.Option(None, help='ID of the album.'),
           title: str = typer.Option(None, help='Name of the photo.'),
           url: str = typer.Option(None, help='URL address'),
           thumbnailUrl: str = typer.Option(
               None, help='Thumbnail URL address.'),
           ):
    """
    Update an photo by ID, omitted fields will be left unchanged.
    """
    photo_response = rest.get(f'{ENDPOINT}/{id}')
    if photo_response.status_code != 200:
        util.panic('Failure to retrieve resource.')
    photo = photo_response.json()
    photo = {
        'albumId': albumId or photo['albumId'],
        'title': title or photo['title'],
        'url': url or photo['url'],
        'thumbnailUrl': thumbnailUrl or photo['thumbnailUrl']
    }
    response = rest.put(f'{ENDPOINT}/{id}', photo)
    if response.status_code != 200:
        util.panic('Failure to update resource.')
    typer.echo(view_photo(response.json()))
Пример #18
0
def update(id: int = typer.Argument(..., min=1),
           name: str = typer.Option(None, help='Name of the user.'),
           username: str = typer.Option(None, help='Username of the user.'),
           email: str = typer.Option(None, help='Email of the user.'),
           street: str = typer.Option(None, help='Street of the user.'),
           suite: str = typer.Option(None, help='Suite of the user.'),
           city: str = typer.Option(None, help='City of the user.'),
           zipcode: str = typer.Option(None, help='Zipcode of the user.'),
           latitude: float = typer.Option(None,
                                          help='Latitude of the user.',
                                          min=-90.0,
                                          max=90.0,
                                          ),
           longitude: float = typer.Option(None,
                                           help='Longitude of the user.',
                                           min=-180.0,
                                           max=180.0,
                                           ),
           phone: str = typer.Option(None, help='Phone of the user.'),
           website: str = typer.Option(None, help='Website of the user.'),
           company_name: str = typer.Option(None,
                                            '--company-name',
                                            help='Company the user works at.',
                                            ),
           catch_phrase: str = typer.Option(None,
                                            help='Catch phrase of the user\'s company'),
           bs: str = typer.Option(None, help='BS of the user\'s company'),
           ):
    """
    Update an user by ID, omitted fields will be left unchanged.
    """
    user_response = rest.get(f'{ENDPOINT}/{id}')
    if user_response.status_code != 200:
        util.panic('Failure to retrieve resource.')
    user = user_response.json()
    user = {
        'name': name or user['name'],
        'username': username or user['username'],
        'email': email or user['email'],
        'address': {
            'street': street or user['address']['street'],
            'suite': suite or user['address']['suite'],
            'city': city or user['address']['city'],
            'zipcode': zipcode or user['address']['zipcode'],
            'geo': {
                'lat': latitude or user['address']['geo']['lat'],
                'lng': longitude or user['address']['geo']['lng']
            }
        },
        'phone': phone or user['phone'],
        'website': website or user['website'],
        'company': {
            'name': company_name or user['company']['name'],
            'catchPhrase': catch_phrase or user['company']['catchPhrase'],
            'bs': bs or user['company']['bs']
        }
    }
    response = rest.put(f'{ENDPOINT}/{id}', user)
    if response.status_code != 200:
        util.panic('Failure to update resource.')
    typer.echo(view_user(response.json()))