
const DeveloperAPIEndpoints = require('./endpoints');
const request = require('request-promise');

module.exports = function (baseURL, clientName, clientSecret) {

    var endpoints = new DeveloperAPIEndpoints(baseURL);

    return {

      getToken: function (authorisationCode) {
          var requestOptions = {
              uri: endpoints.token,
              auth: {
                  username: clientName,
                  password: clientSecret
              },
              json: {
                  data: {
                      type: 'token',
                      attributes: {
                          grant_type: "authorization_code",
                          code: authorisationCode,
                          scope: "analysis"
                      }
                  }
              }
          };
          return request.post( requestOptions );
      },

      querySNPGenotypes: function (token, datasetId, snpNames, quality=0.80) {
          console.log('querySNPGenotypes()');
          var endpointLocation = endpoints.datasetSNPs(datasetId);
          var requestOptions = {
              uri: endpointLocation,
              json: true,
              auth: {
                  bearer: token
              },
              qs: {
                  names: snpNames.join(' '),
                  quality: quality
              }
          };
          return request.get( requestOptions );
      },

      addReportPage: function (accessToken, analysisId, title, content) {

          var requestOptions = {
              uri: endpoints.createReportPage(analysisId),
              auth: {
                  bearer: accessToken
              },
              json: {
                  data: {
                      type: 'page',
                      attributes: {
                          title: title,
                          content: content
                      }
                  }
              }
          };
          return request.post(requestOptions );
      },

      markAnalysis: function (accessToken, analysisId, state) {

          var requestOptions = {
              uri: endpoints.updateAnalysisState(analysisId),
              auth: {
                  bearer: accessToken
              },
              json: {
                  data: {
                      type: 'analysis',
                      id: analysisId,
                      attributes: {
                          status: state
                      }
                  }
              }
          };
          return request.patch( requestOptions );
      },

      markAnalysisAsFinished: function (accessToken, analysisId) {
        return this.markAnalysis(accessToken, analysisId, "ready");
      },

      markAnalysisAsError: function (accessToken, analysisId) {
        return this.markAnalysis(accessToken, analysisId, "error");
      },

      createDataset: function(accessToken, owner) {
        var requestOptions = {
          uri: endpoints.createDataset(),
          auth: {
            bearer: accessToken
          },
          json: {
            data: {
              type: 'dataset',
              attributes: {
                owner
              }
            }
          }
        };
        return request.post( requestOptions );
      },

      createLocalArchiveDatasetImport: function(accessToken, datasetId, archiveLocation) {
        var requestOptions = {
          uri: endpoints.createDatasetImport(datasetId),
          auth: {
            bearer: accessToken
          },
          json: {
            data:{
              type: "dataset",
              attributes:{
                datasourceType: "local-archive",
                datasourceAttributes:{
                  location: archiveLocation
                }
              }
            }
          }
        };
        return request.post( requestOptions );
      }

    };
};

@@ -272,22 +272,255 @@
 tionCode
-) %7B%0A  
+, attributes) %7B%0A        if (typeof attributes===%22undefined%22 %7C%7C !attributes) %7B%0A          attributes = %7B%0A            grant_type: %22authorization_code%22,%0A            code: authorisationCode,%0A            scope: %22analysis%22%0A          %7D;%0A        %7D%0A
         
@@ -544,36 +544,32 @@
 s = %7B%0A          
-    
 uri: endpoints.t
@@ -580,36 +580,32 @@
 ,%0A          
-   
- 
 auth: %7B%0A
             
@@ -584,38 +584,32 @@
         auth: %7B%0A
-      
             user
@@ -638,22 +638,16 @@
         
-  
-    
 password
@@ -663,36 +663,32 @@
 ecret%0A          
-    
 %7D,%0A             
@@ -680,36 +680,32 @@
 ,%0A          
- 
-   
 json: %7B%0A
             
@@ -684,38 +684,32 @@
         json: %7B%0A
-      
             data
@@ -722,32 +722,24 @@
             
-        
 type: 'token
@@ -751,32 +751,24 @@
             
- 
-       
 attributes: 
@@ -759,33 +759,42 @@
     attributes: 
-%7B
+attributes
 %0A               
@@ -782,36 +782,34 @@
 tes%0A            
-    
+%7D%0A
           grant_
@@ -806,74 +806,108 @@
     
-grant_type: %22authorization_code%22,%0A              
+%7D%0A        %7D;%0A        return request.post( requestOptions );%0A
       
+%7D,%0A%0A
       
-code: 
+getAdminToken: function(
 auth
@@ -911,33 +911,35 @@
 uthorisationCode
-,
+) %7B
 %0A               
@@ -935,163 +935,128 @@
     
-                  scope: %22analysis%22%0A                      %7D%0A                  %7D%0A              %7D%0A          %7D;%0A          return request.post( requestOptions 
+return this.getToken(authorisationCode, %7B%0A          grant_type: %22client_credentials%22,%0A          scope: %22admin%22%0A        %7D
 );%0A 

add new token type