pythonrestjama

Python API JAMA: Put attachment to item


I'm facing issues with API Jama to attach an attachment to an item.

I need to automatise my jobs and use python to create items and attach zipfile to them. With the console I can create my attachment with my zip file and attach to it to my item.

But with the API Jama I don't know how I can create my attachment..

In the documentation it seems that I must use this function:

def put_attachments_file(self, attachment_id, file_path):
    """
    Upload a file to a jama attachment
    :param attachment_id: the integer ID of the attachment item to which we are uploading the file
    :param file_path: the file path of the file to be uploaded
    :return: returns the status code of the call
    """
    resource_path = 'attachments/' + str(attachment_id) + '/file'
    with open(file_path, 'rb') as f:
        files = {'file': f}
        try:
            response = self.__core.put(resource_path, files=files)
        except CoreException as err:
            py_jama_rest_client_logger.error(err)
            raise APIException(str(err))
    self.__handle_response_status(response)
    return response.status_code

But I don't know how I can create the attachment_id then associate my zip file to it.

Thanks for your help !


Solution

  • SOLUTION

    # Connect yo your client
    client = JamaClient('your_url', credentials=('your_login', 'your_pwd'))
    
    # Post the attachment to your project and get the attachment_id
    client.post_project_attachment(your_projectID, 'attachment_name', 'description')
    
    # Load the file to attach
    client.put_attachments_file(attachment_id, path_to_your_file)
    
    # Link your attachment to your item
    client.post_item_attachment(item_id, attachment_id)
    

    I hope can be usefull !