# Path: cap_client/api/metadata_api.py
# class MetadataAPI(CapAPI):
#     """Interface for CAP metadata methods."""
# 
#     def get(self, pid, field=None):
#         """Get metadata for analysis with given PID.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param field: get specific field, eg. obj.nested_arr.0
#         :type field: str, optional
#         :return: metadata field's object|value
#         :rtype: JSON serializable object
#         """
#         metadata = self._make_request(
#             url=urljoin('deposits/', pid),
#             headers={'Accept': 'application/basic+json'},
#         )['metadata']
# 
#         fields = field.split('.') if field else []
#         for x in fields:
#             try:
#                 metadata = metadata[int(x) if x.isdigit() else x]
#             except IndexError:
#                 raise UsageError(
#                     'The index you are trying to access does not exist.')
#             except (TypeError, KeyError):
#                 raise UsageError(
#                     'The field {} does not exist. Try a different field.'.
#                     format(x))
# 
#         return metadata
# 
#     def set(self, pid, value, field=None):
#         """Update analysis metadata.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param value: value to set
#         :type value: JSON serializable object
#         :param field: set specific field, eg. obj.nested_arr.0
#         :type field: str, optional
#         :return: updated analysis metadata
#         :rtype: dict
#         """
#         if field:  # use JSON patch to patch fields
#             res = self._make_request(
#                 url=urljoin('deposits/', pid),
#                 method='patch',
#                 headers={
#                     'Content-Type': 'application/json-patch+json',
#                     'Accept': 'application/basic+json'
#                 },
#                 data=json.dumps([{
#                     "op": "replace",
#                     "path": '/' + field.replace('.', '/'),
#                     "value": value,
#                 }]))
#         else:  # use PUT request to update the whole object
#             if not isinstance(value, dict):
#                 raise UsageError('Not a JSON object.')
# 
#             res = self._make_request(
#                 url=urljoin('deposits/', pid),
#                 method='put',
#                 headers={
#                     'Content-Type': 'application/json',
#                     'Accept': 'application/basic+json'
#                 },
#                 data=json.dumps(value),
#             )
# 
#         return res
# 
#     def remove(self, pid, field):
#         """Remove metadata field for analysis with given PID.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param field: field name, eg. obj.nested_arr.0
#         :type field: str, optional
#         :return: updated analysis metadata
#         :rtype: dict
#         """
#         response = self._make_request(
#             url=urljoin('deposits/', pid),
#             method='patch',
#             data=json.dumps([
#                 {
#                     "op": "remove",
#                     "path": '/' + field.replace('.', '/')
#                 },
#             ]),
#             headers={
#                 'Content-Type': 'application/json-patch+json',  # noqa
#                 'Accept': 'application/basic+json'
#             })
# 
#         return response['metadata']
# 
# Path: cap_client/utils.py
# class ColoredGroup(HelpColorsGroup):
# class NotRequiredIf(click.Option):
# class MultipleMutuallyExclusiveOptions(click.Option):
#     def __init__(self, *args, **kwargs):
# def make_tarfile(output_filename, source_dir):
# def validate_version(ctx, param, version_value):
# def load_json_from_file(ctx, param, value):
# def load_json(ctx, param, value):
#     def __init__(self, *args, **kwargs):
#     def handle_parse_result(self, ctx, opts, args):
#     def __init__(self, *args, **kwargs):
#     def handle_parse_result(self, ctx, opts, args):
# def logger(fun):
#     def wrapper(*args, **kwargs):
# def pid_option(required):
#     def inner_pid_option(fun):
#         def wrapper(*args, **kwargs):


import click
from cap_client.api.metadata_api import MetadataAPI
from cap_client.utils import (
    ColoredGroup, NotRequiredIf, json_dumps, load_json,
    load_json_from_file, logger, pid_option
)
    """Update analysis metadata."""
    res = api.set(
        pid=pid,
        value=jsonfile if json is None else json,
        field=field,
    )

    click.echo(json_dumps(res))


@metadata.command()
@pid_option(required=True)
@click.option(
    '--field',
    required=True,
    help="Specify field, eg. object.nested_array.0",
)
@pass_api
@logger
def remove(api, pid, field):
    """Remove from analysis metadata."""
    res = api.remove(
        pid=pid,
        field=field,
    )

    click.echo(json_dumps(res))


@metadata.command()

# Path: cap_client/api/metadata_api.py
# class MetadataAPI(CapAPI):
#     """Interface for CAP metadata methods."""
# 
#     def get(self, pid, field=None):
#         """Get metadata for analysis with given PID.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param field: get specific field, eg. obj.nested_arr.0
#         :type field: str, optional
#         :return: metadata field's object|value
#         :rtype: JSON serializable object
#         """
#         metadata = self._make_request(
#             url=urljoin('deposits/', pid),
#             headers={'Accept': 'application/basic+json'},
#         )['metadata']
# 
#         fields = field.split('.') if field else []
#         for x in fields:
#             try:
#                 metadata = metadata[int(x) if x.isdigit() else x]
#             except IndexError:
#                 raise UsageError(
#                     'The index you are trying to access does not exist.')
#             except (TypeError, KeyError):
#                 raise UsageError(
#                     'The field {} does not exist. Try a different field.'.
#                     format(x))
# 
#         return metadata
# 
#     def set(self, pid, value, field=None):
#         """Update analysis metadata.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param value: value to set
#         :type value: JSON serializable object
#         :param field: set specific field, eg. obj.nested_arr.0
#         :type field: str, optional
#         :return: updated analysis metadata
#         :rtype: dict
#         """
#         if field:  # use JSON patch to patch fields
#             res = self._make_request(
#                 url=urljoin('deposits/', pid),
#                 method='patch',
#                 headers={
#                     'Content-Type': 'application/json-patch+json',
#                     'Accept': 'application/basic+json'
#                 },
#                 data=json.dumps([{
#                     "op": "replace",
#                     "path": '/' + field.replace('.', '/'),
#                     "value": value,
#                 }]))
#         else:  # use PUT request to update the whole object
#             if not isinstance(value, dict):
#                 raise UsageError('Not a JSON object.')
# 
#             res = self._make_request(
#                 url=urljoin('deposits/', pid),
#                 method='put',
#                 headers={
#                     'Content-Type': 'application/json',
#                     'Accept': 'application/basic+json'
#                 },
#                 data=json.dumps(value),
#             )
# 
#         return res
# 
#     def remove(self, pid, field):
#         """Remove metadata field for analysis with given PID.
# 
#         :param pid: analysis PID
#         :type pid: str
#         :param field: field name, eg. obj.nested_arr.0
#         :type field: str, optional
#         :return: updated analysis metadata
#         :rtype: dict
#         """
#         response = self._make_request(
#             url=urljoin('deposits/', pid),
#             method='patch',
#             data=json.dumps([
#                 {
#                     "op": "remove",
#                     "path": '/' + field.replace('.', '/')
#                 },
#             ]),
#             headers={
#                 'Content-Type': 'application/json-patch+json',  # noqa
#                 'Accept': 'application/basic+json'
#             })
# 
#         return response['metadata']
# 
# Path: cap_client/utils.py
# class ColoredGroup(HelpColorsGroup):
# class NotRequiredIf(click.Option):
# class MultipleMutuallyExclusiveOptions(click.Option):
#     def __init__(self, *args, **kwargs):
# def make_tarfile(output_filename, source_dir):
# def validate_version(ctx, param, version_value):
# def load_json_from_file(ctx, param, value):
# def load_json(ctx, param, value):
#     def __init__(self, *args, **kwargs):
#     def handle_parse_result(self, ctx, opts, args):
#     def __init__(self, *args, **kwargs):
#     def handle_parse_result(self, ctx, opts, args):
# def logger(fun):
#     def wrapper(*args, **kwargs):
# def pid_option(required):
#     def inner_pid_option(fun):
#         def wrapper(*args, **kwargs):


# Path: cap_client/cli/metadata_cli.py
import click
from cap_client.api.metadata_api import MetadataAPI
from cap_client.utils import (
    ColoredGroup, NotRequiredIf, json_dumps, load_json,
    load_json_from_file, logger, pid_option
)

    """Update analysis metadata."""
    res = api.set(
        pid=pid,
        value=jsonfile if json is None else json,
        field=field,
    )

    click.echo(json_dumps(res))


@metadata.command()
@pid_option(required=True)
@click.option(
    '--field',
    required=True,
    help="Specify field, eg. object.nested_array.0",
)
@pass_api
@logger
def remove(api, pid, field):
    """Remove from analysis metadata."""
    res = api.remove(
        pid=pid,
        field=field,
    )

    click.echo(json_dumps(res))


@metadata.command()

@pid_option(required=True)