package com.cleverCloud.cleverIdea.api;

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.Nullable;
import org.scribe.builder.ServiceBuilder;
import org.scribe.model.*;
import org.scribe.oauth.OAuthService;

public class CcApi {
  private Token accessToken;
  private OAuthService service;

  public static CcApi getInstance(Project project) {
    return ServiceManager.getService(project, CcApi.class);
  }

  public boolean login(Project project) {
    String API_KEY = "JaGomLuixI29k62K9Zf9klIlQbZHdf";
    String API_SECRET = "KRP5Ckc0CKXRBE0QsmmHX3nVG8n5Mu";
    String API_CALLBACK = "https://console.clever-cloud.com/cli-oauth";

    service = new ServiceBuilder().provider(CleverCloudApi.class).apiKey(API_KEY).apiSecret(API_SECRET).callback(API_CALLBACK).build();

    Token requestToken = service.getRequestToken();
    CcApiLogin login = new CcApiLogin(project, service.getAuthorizationUrl(requestToken));

    if (login.showAndGet()) {
      Verifier verifier = new Verifier(login.getVerifier());
      this.accessToken = service.getAccessToken(requestToken, verifier);
    }

    return true;
  }

  public boolean isValidate() {
    return this.accessToken != null;
  }

  @Nullable
  public String callApi(String url) {
    if (!isValidate()) return null;

    OAuthRequest request = new OAuthRequest(Verb.GET, CleverCloudApi.BASE_URL + url);
    service.signRequest(accessToken, request);
    return request.send().getBody();
  }

}

@@ -34,16 +34,158 @@
 a.api;%0A%0A
+import com.cleverCloud.cleverIdea.Settings;%0Aimport com.intellij.notification.Notification;%0Aimport com.intellij.notification.NotificationType;%0A
 import c
@@ -276,16 +276,58 @@
 roject;%0A
+import org.jetbrains.annotations.NotNull;%0A
 import o
@@ -431,70 +431,214 @@
 del.
-*;%0Aimport org.scribe.oauth.OAuthService;%0A%0Apublic class CcApi %7B
+OAuthRequest;%0Aimport org.scribe.model.Token;%0Aimport org.scribe.model.Verb;%0Aimport org.scribe.oauth.OAuthService;%0A%0Apublic class CcApi %7B%0A  private static CcApi ourInstance = null;%0A  private Project myProject;
 %0A  p
@@ -730,16 +730,25 @@
 nstance(
+@NotNull 
 Project 
@@ -766,114 +766,461 @@
     
-return ServiceManager.getService(project, CcApi.class);%0A  %7D%0A%0A  public boolean login(Project project) %7B%0A   
+if (ourInstance == null) %7B%0A      ourInstance = new CcApi();%0A    %7D%0A%0A    ourInstance.myProject = project;%0A    Settings settings = ServiceManager.getService(project, Settings.class);%0A    if (settings.oAuthToken != null && settings.oAuthSecret != null) %7B%0A      ourInstance.accessToken = new Token(settings.oAuthToken, settings.oAuthSecret);%0A    %7D%0A    return ourInstance;%0A  %7D%0A%0A  public boolean login() %7B%0A    @SuppressWarnings(%22SpellCheckingInspection%22) final
  Str
@@ -1267,24 +1267,75 @@
 bZHdf%22;%0A    
+@SuppressWarnings(%22SpellCheckingInspection%22) final 
 String API_S
@@ -1380,16 +1380,22 @@
 u%22;%0A    
+final 
 String A
@@ -1454,16 +1454,85 @@
 oauth%22;%0A
+    final CcApiLogin login = new CcApiLogin(myProject, API_CALLBACK);
 %0A    ser
@@ -1669,29 +1669,59 @@
     
-Token request
+if (login.showAndGet()) %7B%0A      this.access
 Token = 
 serv
@@ -1720,313 +1720,374 @@
 n = 
-service.getRequestToken();%0A    CcApiLogin login = new CcApiLogin(project, service.getAuthorizationUrl(requestToken));%0A%0A    if (login.showAndGet
+new Token(login.getToken(), login.getSecret());%0A      return true;%0A    %7D%0A%0A    loginErrorNotification();%0A    return false;%0A  %7D%0A%0A  private void loginErrorNotification
 ()
-)
  %7B%0A    
-  Verifier verifier = new Verifier(login.getVerifier());%0A      this.accessToken = service.getAccessToken(requestToken, verifier);%0A    %7D%0A%0A    return true
+Notification notification =%0A      new Notification(%22Error Report%22, %22Clever IDEA - Login error%22, %22Error while login in Clever Cloud.%22, NotificationType.ERROR);%0A    notification.notify(myProject)
 ;%0A  
@@ -2240,20 +2240,105 @@
 ()) 
-return null;
+%7B%0A      if (!login()) %7B%0A        callApiErrorNotification(url);%0A        return null;%0A      %7D%0A    %7D
 %0A%0A  
@@ -2510,10 +2510,313 @@
 );%0A  %7D%0A%0A
+  private void callApiErrorNotification(String url) %7B%0A    Notification notification =%0A      new Notification(%22Error Report%22, %22Clever IDEA - Request error%22, %22The following request cannot be executed :%3Cbr /%3E%22 + url,%0A                       NotificationType.ERROR);%0A    notification.notify(myProject);%0A  %7D%0A%0A
 %7D%0A

Use token & secret instead of verifier