Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Return Download IDs From POST downloads/{username} #1469

@Enola-guy

Description

@Enola-guy

Hey would you be ok if I do this ?

Need

  • When clients enqueue downloads via POST /api/v0/transfers/downloads/{username}, the API currently returns only a 201 status with no body.
  • Consumers must immediately query the list endpoints to locate the created transfers, which is inefficient and ambiguous when multiple requests are issued.
  • Exposing the server-assigned download identifiers in the enqueue response will let clients track transfers without an extra round trip.

Proposed Implementation

  • Update TransfersController.EnqueueAsync to capture the List<Transfer> returned by IDownloadService.EnqueueAsync and project the non-null Transfer.Id values into the HTTP response body.
  • Return a 201 Created response with the GUID collection and a CreatedAtAction pointing to GetDownloadsAsync to keep the response RESTful.
  • Ensure the controller’s ProducesResponseType attribute documents the GUID payload so the OpenAPI surface reflects the new response shape.
+++ b/src/slskd/Transfers/API/Controllers/TransfersController.cs
@@ -201,7 +201,7 @@ namespace slskd.Transfers.API
         /// <response code="500">An unexpected error was encountered.</response>
         [HttpPost("downloads/{username}")]
         [Authorize(Policy = AuthPolicy.Any)]
-        [ProducesResponseType(201)]
+        [ProducesResponseType(typeof(IEnumerable<Guid>), 201)]
         [ProducesResponseType(typeof(string), 403)]
         [ProducesResponseType(typeof(string), 500)]
         public async Task<IActionResult> EnqueueAsync([FromRoute, Required] string username, [FromBody] IEnumerable<QueueDownloadRequest> requests)
@@ -213,8 +213,16 @@ namespace slskd.Transfers.API
 
             try
             {
-                await Transfers.Downloads.EnqueueAsync(username, requests.Select(r => (r.Filename, r.Size)));
-                return StatusCode(201);
+                var downloads = await Transfers.Downloads.EnqueueAsync(username, requests.Select(r => (r.Filename, r.Size)));
+
+                var ids = downloads == null
+                    ? Array.Empty<Guid>()
+                    : downloads
+                        .Where(transfer => transfer is not null)
+                        .Select(transfer => transfer.Id)
+                        .ToArray();
+
+                return CreatedAtAction(nameof(GetDownloadsAsync), new { username }, ids);
             }
             catch (Exception ex)
             {
@@ -458,4 +466,4 @@ namespace slskd.Transfers.API
             return Ok(upload);
         }
     }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions