|
6 | 6 | import requests |
7 | 7 | from requests.exceptions import HTTPError |
8 | 8 |
|
9 | | -from .utils import extract_file_extension_from_url, extract_name_from_url |
| 9 | +from .utils import ( |
| 10 | + extract_file_extension_from_url, |
| 11 | + extract_name_from_url, |
| 12 | + get_user_agent, |
| 13 | +) |
10 | 14 |
|
11 | 15 |
|
12 | 16 | class MusicAiClient: |
13 | | - def __init__(self, api_key, job_monitor_interval=2, save_output_to_folder=True): |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + api_key, |
| 20 | + job_monitor_interval=2, |
| 21 | + save_output_to_folder=True, |
| 22 | + user_agent=None, |
| 23 | + ): |
14 | 24 | self.api_key = api_key |
15 | 25 | self.base_url = "https://api.music.ai/api" |
16 | 26 | self.job_monitor_interval = job_monitor_interval |
17 | 27 | self.save_output_to_folder = save_output_to_folder |
| 28 | + self.user_agent = user_agent or get_user_agent() |
18 | 29 |
|
19 | 30 | def get_headers(self): |
20 | | - return {"Authorization": self.api_key} |
| 31 | + return { |
| 32 | + "Authorization": self.api_key, |
| 33 | + "User-Agent": self.user_agent, |
| 34 | + } |
21 | 35 |
|
22 | 36 | def upload_file(self, file_path): |
23 | 37 | response = requests.get(f"{self.base_url}/upload", headers=self.get_headers()) |
@@ -79,8 +93,19 @@ def list_jobs(self, filters=None): |
79 | 93 |
|
80 | 94 | return response.json() |
81 | 95 |
|
82 | | - def add_job(self, job_name, workflow_slug, params): |
83 | | - data = {"name": job_name, "workflow": workflow_slug, "params": params} |
| 96 | + def add_job(self, job_name, workflow_slug, params, **options): |
| 97 | + data = { |
| 98 | + "name": job_name, |
| 99 | + "workflow": workflow_slug, |
| 100 | + "params": params, |
| 101 | + } |
| 102 | + |
| 103 | + if "metadata" in options: |
| 104 | + data["metadata"] = options["metadata"] |
| 105 | + |
| 106 | + if "copy_results_to" in options: |
| 107 | + data["copyResultsTo"] = options["copy_results_to"] |
| 108 | + |
84 | 109 | response = requests.post( |
85 | 110 | f"{self.base_url}/job", headers=self.get_headers(), json=data |
86 | 111 | ) |
|
0 commit comments