Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(API): import csv-license file #2292

Merged
merged 1 commit into from Sep 5, 2022

Conversation

dushimsam
Copy link
Contributor

@dushimsam dushimsam commented Aug 16, 2022

Signed-off-by: dushimsam dushsam100@gmail.com

Description

The controller function to handle the import of a csv license file.

Changes

  1. Added a new controller file and the function to handle the logic in LicenseController.
  2. Updated the file AdminLicenseFromCSV.php to be reusable by the outside callers.
  3. Updated the main file(index.php) by adding a new route POST /license/import.
  4. Updated the openapi.yaml file to introduce a new API.

How to test

  1. Design a request as follows.
  2. The delimiter property (Free to keep it as empty).
  3. The enclosure property (Free to keep it as empty).
  4. The file_input form-data property.
  5. Run the API.

Example

FIRST SCENARIO

body_request

form_data_file_request

SECOND SCENARIO

no_file_selected

Related Issue:

Fixes #2291

cc: @shaheemazmalmmd @GMishx

@dushimsam dushimsam force-pushed the dushimsam/feat/import-license branch from ecede9e to fa13f6a Compare August 16, 2022 19:12
@shaheemazmalmmd shaheemazmalmmd added needs code review needs test GSoC-22 Label to tag issues and pull request for GSOC 2022 activities labels Aug 19, 2022
src/www/ui/api/documentation/openapi.yaml Outdated Show resolved Hide resolved
src/www/ui/api/documentation/openapi.yaml Outdated Show resolved Hide resolved
@dushimsam dushimsam force-pushed the dushimsam/feat/import-license branch 2 times, most recently from fc68e23 to 766cc4d Compare August 28, 2022 18:44
@GMishx
Copy link
Member

GMishx commented Sep 2, 2022

Moving delimiter and enclosure to request body will be better than putting them in header. It can cause more CORS problems.

I have done following changes to move them to request body:

diff --git a/src/www/ui/api/Controllers/LicenseController.php b/src/www/ui/api/Controllers/LicenseController.php
index c0222c405..cb501a6ae 100644
--- a/src/www/ui/api/Controllers/LicenseController.php
+++ b/src/www/ui/api/Controllers/LicenseController.php
@@ -385,8 +385,15 @@ class LicenseController extends RestController
     $uploadedFile = $symReq->files->get($adminLicenseFromCsv->getFileInputName(),
       null);
 
-    $delimiter = $request->getHeaderLine('delimiter')  ? : ',';
-    $enclosure = $request->getHeaderLine('enclosure') ?: '"';
+    $requestBody = $request->getParsedBody();
+    $delimiter = ',';
+    $enclosure = '"';
+    if (array_key_exists("delimiter", $requestBody) && !empty($requestBody["delimiter"])) {
+      $delimiter = $requestBody["delimiter"];
+    }
+    if (array_key_exists("enclosure", $requestBody) && !empty($requestBody["enclosure"])) {
+      $enclosure = $requestBody["enclosure"];
+    }
 
     $res = $adminLicenseFromCsv->handleFileUpload($uploadedFile,$delimiter,$enclosure);
 
diff --git a/src/www/ui/api/documentation/openapi.yaml b/src/www/ui/api/documentation/openapi.yaml
index 9f96ae99f..928d99280 100644
--- a/src/www/ui/api/documentation/openapi.yaml
+++ b/src/www/ui/api/documentation/openapi.yaml
@@ -1629,19 +1629,6 @@ paths:
         default:
             $ref: '#/components/responses/defaultResponse'
   /license/import-csv:
-    parameters:
-      - name: delimiter
-        in: header
-        required: false
-        description: delimiter of the license
-        schema:
-          type: string
-      - name: enclosure
-        description: enclosure of the license
-        in: header
-        required: false
-        schema:
-          type: string
     post:
       operationId: importLicense
       tags:
@@ -1657,9 +1644,21 @@ paths:
             schema:
               type: object
               properties:
+                enclosure:
+                  type: string
+                  description: Enclosure for string in CSV
+                  default: '"'
+                  required: false
+                delimiter:
+                  type: string
+                  description: Delimiters for fields in CSV
+                  default: ','
+                  required: false
                 file_input:
                   type: string
                   format: binary
+                  description: CSV to be imported
+                  required: true
       responses:
           '400':
             description: Validation Error
diff --git a/src/www/ui/api/index.php b/src/www/ui/api/index.php
index c37fb2063..d4c9f9a13 100644
--- a/src/www/ui/api/index.php
+++ b/src/www/ui/api/index.php
@@ -225,7 +225,7 @@ $app->group('/license',
   function (\Slim\Routing\RouteCollectorProxy $app) {
     $app->get('', LicenseController::class . ':getAllLicenses');
     $app->post('', LicenseController::class . ':createLicense');
-    $app->post('/import', LicenseController::class . ':handleImportLicense');
+    $app->post('/import-csv', LicenseController::class . ':handleImportLicense');
     $app->get('/{shortname:.+}', LicenseController::class . ':getLicense');
     $app->patch('/{shortname:.+}', LicenseController::class . ':updateLicense');
     $app->any('/{params:.*}', BadRequestController::class);

@dushimsam
Copy link
Contributor Author

Done

src/www/ui/api/documentation/openapi.yaml Outdated Show resolved Hide resolved
Signed-off-by: dushimsam <dushsam@gmail.com>
Copy link
Member

@GMishx GMishx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes looks good. Tested, working as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GSoC-22 Label to tag issues and pull request for GSOC 2022 activities ready
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Import license file via REST API
3 participants