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()))
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()))
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))
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])
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])
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])
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()))
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()))
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()))
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()))
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()))