tus-php icon indicating copy to clipboard operation
tus-php copied to clipboard

Unable to create resource.

Open ChrisMP05 opened this issue 1 year ago • 0 comments
trafficstars

Buenos dias, tengo un inconveniente sobre la carga de arcivos, especificamente en el Client

    `$baseUri = 'https://api-sire.sunat.gob.pe/v1/contribuyente/migeigv/libros/rvierce/receptorpropuesta/web/propuesta/upload';

    $options = [
        'headers' => [
            'authorization' => 'Bearer ' . $token,
            'Content-Type' => 'application/x-www-form-urlencoded',
        ],
    ];

    $client = new TusClient($baseUri, $options);

    $archivo = SITE_ROOT . "/Assets/LE2060518421020231000140400021112.zip";

    try {

        $client->setApiPath('/v1/contribuyente/migeigv/libros/rvierce/receptorpropuesta/web/propuesta/upload');

        // Configura los parámetros del archivo y realiza la carga
        $client->setKey('clave6663'); // Clave personalizada

        $client->setKey('clave666')->file($archivo, 'LE2060518421020231000140400021112.zip')
            ->addMetadata('filename', 'LE2060518421020231000140400021112.zip')
            ->addMetadata('filetype', 'zip')
            ->addMetadata('numRuc', '20605184210')
            ->addMetadata('perTributario', '202310')
            ->addMetadata('codOrigenEnvio', '2') // Código de origen de envío: 2 Servicio web (Obligatorio)
            ->addMetadata('codProceso', '3') // Código del indicador de carga masiva: 3.Reemplazo de la Propuesta
            ->addMetadata('codTipoCorrelativo', '01') // Tipo de correlativo: 01: Tipo envíos masivos
            ->addMetadata('nomArchivoImportacion', 'LE2060518421020231000140400021112.txt')
            ->addMetadata('codLibro', '140000'); // Código de libro: 140000 RVIE (Obligatorio);

        // Realiza la carga del archivo
        $client->upload();

        // Obtiene el número de ticket de envío
        $numTicket = $client->getResponse()->getHeader('numTicket');

        // Muestra el resultado
        echo 'Número de ticket de envío: ' . $numTicket . PHP_EOL;

    } catch (\Exception $e) {
        // Maneja cualquier excepción que pueda ocurrir
        echo 'Error: ' . $e->getMessage() . PHP_EOL;
    }`

y en la parte de upload, cuando llega a este metodo :
`public function createWithUpload(string $key, int $bytes = -1): array {

    $bytes = $bytes < 0 ? $this->fileSize : $bytes;

    $headers = $this->headers + [
        'Upload-Length' => $this->fileSize,
        'Upload-Key' => $key,
        'Upload-Checksum' => $this->getUploadChecksumHeader(),
        'Upload-Metadata' => $this->getUploadMetadataHeader(),
    ];

    $data = '';
    if ($bytes > 0) {
        $data = $this->getData(0, $bytes);

        $headers += [
            'Content-Type' => self::HEADER_CONTENT_TYPE,
            'Content-Length' => \strlen($data),
        ];
    }

    if ($this->isPartial()) {
        $headers += ['Upload-Concat' => 'partial'];
    }

    try {

        $response = $this->getClient()->post($this->apiPath, [
            'body' => $data,
            'headers' => $headers,
        ]);

    } catch (ClientException $e) {
        $response = $e->getResponse();
    }

    $statusCode = $response->getStatusCode();

    if (HttpResponse::HTTP_CREATED !== $statusCode) {
        throw new FileException('Unable to create resource.');
    }

    $uploadOffset   = $bytes > 0 ? current($response->getHeader('upload-offset')) : 0;
    $uploadLocation = current($response->getHeader('location'));

    $this->getCache()->set($this->getKey(), [
        'location' => $uploadLocation,
        'expires_at' => Carbon::now()->addSeconds($this->getCache()->getTtl())->format($this->getCache()::RFC_7231),
    ]);

    return [
        'location' => $uploadLocation,
        'offset' => $uploadOffset,
    ];
}`

, el status code me devuelve 200, pero segun la logica de la libreria deberia ser 201, para que pueda continuar, sera que el apiPath que lo seteo este mal? , o aque se debera , ESTO ES LA RESPUESTA QUE ME DEVUELVE :

`GuzzleHttp\Psr7\Response Object ( [reasonPhrase:GuzzleHttp\Psr7\Response:private] => OK [statusCode:GuzzleHttp\Psr7\Response:private] => 200 [headers:GuzzleHttp\Psr7\Response:private] => Array ( [Date] => Array ( [0] => Wed, 29 Nov 2023 14:13:59 GMT )

        [Content-Type] => Array
            (
                [0] => text/plain
            )

        [Content-Length] => Array
            (
                [0] => 0
            )

        [Connection] => Array
            (
                [0] => keep-alive
            )

        [Tus-Resumable] => Array
            (
                [0] => 1.0.0
            )

        [Location] => Array
            (
                [0] => /v1/contribuyente/migeigv/libros/rvierce/receptorpropuesta/web/propuesta/upload/faa6992c-9508-4860-baa8-787e4a2d7356
            )

        [X-ServiceName] => Array
            (
                [0] => migeigv-libros-receptorpropuesta-web-backend
            )

        [X-ServiceEnvironment] => Array
            (
                [0] => LOCAL_DEV
            )

        [X-ServiceVersion] => Array
            (
                [0] => 1.8.3-4
            )

        [X-ServiceInstanceId] => Array
            (
                [0] => FWMBC
            )

        [Access-Control-Expose-Headers] => Array
            (
                [0] => Location, Upload-Offset
            )

    )`

ChrisMP05 avatar Nov 29 '23 14:11 ChrisMP05