Don’t know how to upload a file I want to attach to my package to the download

Viewing 7 posts - 1 through 7 (of 7 total)
#190054

Kenechukwu Nkamuo
Participant

Here is my code

import requests
import uuid # Import the UUID library for generating unique keys

# Configure the API endpoint URL and your API key
api_url = ‘https://www.downloadbrandlogos.com/wp-json/wpdm/v1/packages’
api_key = ‘your API key’

# Set up headers with API key for authentication
headers = {
‘Authorization’: f’Bearer {api_key}’
}

# Define a list of dictionaries, each specifying a file with its title and file path
files_data = [
{
‘title’: ‘Kerala Blasters FC.png’,
‘path’: ‘/storage/emulated/0/Download/photo.jpg’,
},
# Add more files as needed
]

# Generate unique keys for files and associate them with file paths
file_key_mapping = {}
for file_info in files_data:
key = str(uuid.uuid4()) # Generate a unique key using UUID
file_path = file_info[‘path’]
file_key_mapping[key] = file_path

# Use the generated keys in download_params
download_params = {
‘title’: ‘Kerala Blasters FC.pdf’,
‘author’: ‘Ken’,
‘status’: ‘publish’, # Set the status to “publish”
‘files’: file_key_mapping # Use the generated keys as file identifiers
}

# Create an empty list to store file attachments
files_to_upload = []

# Iterate through the list of files and create attachments
for key, file_path in file_key_mapping.items():
files_to_upload.append((‘file[]’, (key, open(file_path, ‘rb’))))

# Make a POST request to create the downloadable files
response = requests.post(api_url, headers=headers, data=download_params, files=files_to_upload)

# Check the response
if response.status_code == 201:
print(‘Downloadable files created successfully!’)

# Extract the package ID from the API response
package_id = response.json().get(‘id’)

if package_id:
# Use the package ID to attach the file to the “Attach Files” section
attachment_url = f'{api_url}/{package_id}/attach’
attachment_data = {
‘media’: ‘1’ # Attach to the “Attach Files” section (1 for yes, 0 for no)
}
attachment_response = requests.post(attachment_url, headers=headers, data=attachment_data)

if attachment_response.status_code == 200:
print(‘File attached to “Attach Files” section successfully!’)
else:
print(‘Failed to attach file to “Attach Files” section.’)
print(attachment_response.text)
else:
print(‘Package ID not found in API response.’)
else:
print(‘Failed to create the downloadable files.’)
print(response.text)

How can I achieve this please

#190056

Nayeem Riddhi
Moderator

Hello Kenechukwu Nkamuo,

Hope you are well. Are you trying to use WPDM REST API? For that, you can check this doc too,
https://imsas.github.io/wpdm-rest-api-docs/#create-a-package
and here is our add-on,

Download Manager REST API


please check and let me know

Thank you and regards

#190057

Kenechukwu Nkamuo
Participant

I have already installed it in the plugin section of my WordPress. The problem is just the attachment of file to the package. I can create the package with no issue it is just that I can’t attach a file to the package

#190063

Nayeem Riddhi
Moderator

please follow the doc. I hope your problem should be resolved.

Thank you and regards

#190064

Kenechukwu Nkamuo
Participant

Please the doc didn’t said or give any idea about the files as aspect of creating a package, it just gave a description.

#190065

Nayeem Riddhi
Moderator

please take the example from the doc you might need to put the file name with the rest API parameters like this: your-dir/filename.ext.. please check and let me know

Thank you and regards

#190488

Nayeem Riddhi
Moderator

Hello Kenechukwu Nkamuo,

Has the problem been resolved on your side? If you have already created a package. Then you will also be able to upload/attach files for that package. If you have kept just the file name you should keep the files in wp-content/uploads/download-manager-files dir too. Thus I hope you will be able to attach the file to the packages. please check and let me know

Thank you and regards

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.