o
    sij                     @   s<   d dl Z d dlZd dlZd dlmZmZ G dd deZdS )    N)BufferedReaderBytesIOc                   @   sX   e Zd ZdZdddZdddZdd	 Zdd
dZdd ZdddZ	dd Z
dd ZdS )Zenodoa  Faciliate Zenodo's REST API.

    Args:
        api_key (str): Access token generated to upload depositions.
        use_sandbox (bool): Whether to use the sandbox (default: True)
            Note that `api_key` are different in sandbox.

    All methods return the requests response.

    .. note:: A Zenodo record is something that is public and cannot be deleted.
        A Zenodo deposit has not yet been published, is private and can be
        deleted.
    NTc                 C   sp   |d u r
t dd }|d u rtd|| _|du rd| _nd| _|| _dd| j i| _dd| j d	| _d S )
NACCESS_TOKENzqNeed to set `api_key` somehow. Either through the classarguments or by setting ACCESS_TOKEN env variable in bash.Tzhttps://sandbox.zenodo.orgzhttps://zenodo.orgAuthorizationzBearer zapplication/json)zContent-Typer   )osgetenv
ValueErroruse_sandboxzenodo_addressapi_keyauth_headerheaders)selfr   r
    r   J/home/ubuntu/.local/lib/python3.10/site-packages/asteroid/models/zenodo.py__init__   s   
zZenodo.__init__c                 C   sv   t j| j di | jd}|jdkrtd|j |S |dur/t|tr/| 	|
 d |S tdt| d |S )	zCreates a new deposition.

        Args:
            metadata (dict, optional): Metadata dict to upload on the new
                deposition.
        /api/deposit/depositions)jsonr      z!Creation failed (status code: {})Nidz#Could not interpret metadata type (z), expected dict)requestspostr   r   status_codeprintformat
isinstancedictchange_metadata_in_depositionr   type)r   metadatarr   r   r   create_new_deposition+   s   
zZenodo.create_new_depositionc                 C   s0   d|i}t j| j d| t|| jd}|S )aB  Set or replace metadata in given deposition

        Args:
            dep_id (int): deposition id. You cna get it with
                `r = create_new_deposition(); dep_id = r.json()['id']`
            metadata (dict): Metadata dict.

        Examples
            >>> metadata = {
            ...     'title': 'My first upload',
            ...     'upload_type': 'poster',
            ...     'description': 'This is my first upload',
            ...     'creators': [{'name': 'Doe, John',
            ...                   'affiliation': 'Zenodo'}]
            ... }
        r    /api/deposit/depositions/)datar   )r   putr   r   dumpsr   )r   dep_idr    r$   r!   r   r   r   r   @   s   z$Zenodo.change_metadata_in_depositionc                 C   s   t |trd|i}|r|nd}n5t |trAtj|r1dttj|di}|r*|ntj|}ndt	t
|di}|r>|nd}ntdd|i}td|| tj| j d| d	| j||d
}td|j |S )a  Upload one file to existing deposition.

        Args:
            dep_id (int): deposition id. You cna get it with
                `r = create_new_deposition(); dep_id = r.json()['id']`
            file (str or io.BufferedReader): path to a file, or already opened
                file (path prefered).
            name (str, optional): name given to the uploaded file.
                Defaults to the path.

        (More: https://developers.zenodo.org/#deposition-files)
        fileUnknownrbzutf-8z,Unknown file format , expected str or Bytes namez!Submitting Data: {} and Files: {}r#   z/files)r   r$   fileszZenodo received : {})r   r   strr   pathisfileopen
expanduserbasenamer   bytesr	   r   r   r   r   r   r   content)r   r'   r(   r+   r,   filenamer$   r!   r   r   r   upload_new_file_to_depositionY   s(   

z$Zenodo.upload_new_file_to_depositionc                 C   s"   t j| j d| d| jd}|S )zPublish given deposition (Cannot be deleted)!

        Args:
            dep_id (int): deposition id. You cna get it with
                `r = create_new_deposition(); dep_id = r.json()['id']`
        r#   z/actions/publishr   )r   r   r   r   r   r'   r!   r   r   r   publish_deposition   s
   zZenodo.publish_depositionc                 C   sj   |dkrt d| d tj| j d| | jd}nt d tj| j d| jd}t d|j |S )	z@Get deposition by deposition id. Get all dep_id is -1 (default).r:   zGet deposition z from Zenodor#   r7   zGet all depositions from Zenodor   z Get Depositions: Status Code: {})r   r   getr   r   r   r   r8   r   r   r   get_deposition   s   zZenodo.get_depositionc                 C   s.   t d|  tj| j d| | jd}|S )z-Remove deposition with deposition id `dep_id`zDelete deposition number r#   r7   )r   r   deleter   r   r8   r   r   r   remove_deposition   s
   zZenodo.remove_depositionc                 C   s(   |   }| D ]	}| |d  qdS )z1Removes all unpublished deposition (not records).r   N)r<   r   r>   )r   all_depositionsdepr   r   r   remove_all_depositions   s   zZenodo.remove_all_depositions)NT)N)r:   )__name__
__module____qualname____doc__r   r"   r   r6   r9   r<   r>   rA   r   r   r   r   r      s    


'
r   )r   r   r   ior   r   objectr   r   r   r   r   <module>   s
    