Selaa lähdekoodia

Start implementing api client

matthias 4 vuotta sitten
vanhempi
commit
ad2fbb37ce

+ 8 - 0
build.gradle

@@ -13,6 +13,14 @@ repositories {
13 13
 dependencies {
14 14
     testCompile group: 'junit', name: 'junit', version: '4.12'
15 15
     compile fileTree(dir: 'libs', include: '*.jar')
16
+    compile 'io.github.openfeign:feign-core:11.1'
17
+    compile 'io.github.openfeign:feign-httpclient:11.1'
18
+    compile 'io.github.openfeign:feign-gson:11.1'
19
+
20
+    compileOnly 'org.projectlombok:lombok:1.18.20'
21
+    annotationProcessor 'org.projectlombok:lombok:1.18.20'
22
+    testCompileOnly 'org.projectlombok:lombok:1.18.20'
23
+    testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
16 24
 }
17 25
 
18 26
 // See https://github.com/JetBrains/gradle-intellij-plugin/

+ 56 - 10
src/main/java/com/armstrongconsulting/acprotasks/AcproRepository.java

@@ -2,25 +2,34 @@ package com.armstrongconsulting.acprotasks;
2 2
 
3 3
 import org.jetbrains.annotations.NotNull;
4 4
 import org.jetbrains.annotations.Nullable;
5
+import org.slf4j.Logger;
6
+import org.slf4j.LoggerFactory;
5 7
 
8
+import com.armstrongconsulting.acprotasks.client.AcproConnector;
6 9
 import com.intellij.openapi.progress.ProgressIndicator;
7 10
 import com.intellij.tasks.Task;
8 11
 import com.intellij.tasks.TaskRepositoryType;
9 12
 import com.intellij.tasks.impl.BaseRepository;
10 13
 import com.intellij.util.xmlb.annotations.Tag;
11 14
 
15
+import feign.FeignException;
16
+
12 17
 @Tag(AcproRepositoryType.NAME)
13 18
 public class AcproRepository extends BaseRepository
14 19
 {
20
+    private final Logger logger = LoggerFactory.getLogger(AcproRepository.class);
21
+
22
+    private String apiKey = null;
15 23
 
16 24
     public AcproRepository(TaskRepositoryType type)
17 25
     {
18 26
         super(type);
19 27
     }
20 28
 
21
-    public AcproRepository(BaseRepository other)
29
+    public AcproRepository(AcproRepository other)
22 30
     {
23 31
         super(other);
32
+        this.apiKey = other.apiKey;
24 33
     }
25 34
 
26 35
     public AcproRepository()
@@ -30,7 +39,7 @@ public class AcproRepository extends BaseRepository
30 39
     @Override
31 40
     public @Nullable Task findTask(@NotNull String id) throws Exception
32 41
     {
33
-        return new AcproTask();
42
+        return null;
34 43
     }
35 44
 
36 45
     @Override
@@ -40,18 +49,55 @@ public class AcproRepository extends BaseRepository
40 49
     }
41 50
 
42 51
     @Override
43
-    public Task[] getIssues(@Nullable String query, int offset, int limit, boolean withClosed)
44
-        throws Exception
52
+    public Task[] getIssues(@Nullable String query, int offset, int limit, boolean withClosed,
53
+        @NotNull ProgressIndicator cancelled) throws Exception
45 54
     {
46
-        System.out.println("Searching for issues (1): " + query);
47
-        return new Task[]{new AcproTask()};
55
+        if (apiKey == null)
56
+        {
57
+            apiKey = AcproConnector.getClient(getUrl()).getApiKey(getUsername(), getPassword(), 28800);
58
+        }
59
+        return AcproConnector.getClient(getUrl()).getItems(query, offset, limit, apiKey).stream().peek(a -> logger.warn(a.toString())).map(AcproTask::new)
60
+            .toArray(AcproTask[]::new);
48 61
     }
49 62
 
50 63
     @Override
51
-    public Task[] getIssues(@Nullable String query, int offset, int limit, boolean withClosed,
52
-        @NotNull ProgressIndicator cancelled) throws Exception
64
+    public @Nullable CancellableConnection createCancellableConnection()
65
+    {
66
+        return new CancellableConnection()
67
+        {
68
+            @Override
69
+            protected void doTest() throws Exception
70
+            {
71
+                try
72
+                {
73
+                    if (apiKey != null)
74
+                    {
75
+                        return;
76
+                    }
77
+
78
+                    apiKey = AcproConnector.getClient(getUrl()).getApiKey(getUsername(), getPassword(), 28800);
79
+                }
80
+                catch (FeignException.Unauthorized e)
81
+                {
82
+                    throw new RuntimeException("Wrong username or password");
83
+                }
84
+                catch (FeignException.FeignClientException e)
85
+                {
86
+                    throw new RuntimeException("Connection failed");
87
+                }
88
+            }
89
+
90
+            @Override
91
+            public void cancel()
92
+            {
93
+                // not cancellable
94
+            }
95
+        };
96
+    }
97
+
98
+    @Override
99
+    public void initializeRepository()
53 100
     {
54
-        System.out.println("Searching for issues (2): " + query);
55
-        return new Task[]{new AcproTask()};
101
+        logger.info("Initialize repository");
56 102
     }
57 103
 }

+ 16 - 4
src/main/java/com/armstrongconsulting/acprotasks/AcproTask.java

@@ -7,6 +7,7 @@ import javax.swing.*;
7 7
 import org.jetbrains.annotations.NotNull;
8 8
 import org.jetbrains.annotations.Nullable;
9 9
 
10
+import com.armstrongconsulting.acprotasks.client.model.AcproItem;
10 11
 import com.intellij.tasks.Comment;
11 12
 import com.intellij.tasks.Task;
12 13
 import com.intellij.tasks.TaskType;
@@ -15,22 +16,33 @@ import icons.TasksIcons;
15 16
 
16 17
 public class AcproTask extends Task
17 18
 {
19
+    private String id;
20
+    private String summary;
21
+    private String description;
22
+
23
+    public AcproTask(AcproItem item)
24
+    {
25
+        this.id = String.valueOf(item.getId());
26
+        this.summary = item.getTitle();
27
+        this.description = item.getDescription();
28
+    }
29
+
18 30
     @Override
19 31
     public @NotNull String getId()
20 32
     {
21
-        return "ID 123";
33
+        return id;
22 34
     }
23 35
 
24 36
     @Override
25 37
     public @NotNull String getSummary()
26 38
     {
27
-        return "A new a.c.pro task";
39
+        return summary;
28 40
     }
29 41
 
30 42
     @Override
31 43
     public @Nullable String getDescription()
32 44
     {
33
-        return "a.c.pro task description";
45
+        return description;
34 46
     }
35 47
 
36 48
     @Override
@@ -78,6 +90,6 @@ public class AcproTask extends Task
78 90
     @Override
79 91
     public @Nullable String getIssueUrl()
80 92
     {
81
-        return null;
93
+        return "https://www.acpro.at/item/" + id;
82 94
     }
83 95
 }

+ 21 - 0
src/main/java/com/armstrongconsulting/acprotasks/client/AcproClient.java

@@ -0,0 +1,21 @@
1
+package com.armstrongconsulting.acprotasks.client;
2
+
3
+import java.util.List;
4
+
5
+import com.armstrongconsulting.acprotasks.client.model.AcproItem;
6
+
7
+import feign.Headers;
8
+import feign.Param;
9
+import feign.RequestLine;
10
+
11
+public interface AcproClient
12
+{
13
+    @RequestLine("GET /api_key?user={user}&password={password}&expires_after_seconds={expires_after_seconds}")
14
+    String getApiKey(@Param("user") String user, @Param("password") String password,
15
+        @Param("expires_after_seconds") Integer timeToLive);
16
+
17
+    @RequestLine("GET /items?q=title:{title}&start={offset}&count={limit}&api_key={apiKey}")
18
+    @Headers("Accept: application/json")
19
+    List<AcproItem> getItems(@Param("title") String titleContains, @Param("offset") Integer offset,
20
+        @Param("limit") Integer limit, @Param("apiKey") String apiKey);
21
+}

+ 30 - 0
src/main/java/com/armstrongconsulting/acprotasks/client/AcproConnector.java

@@ -0,0 +1,30 @@
1
+package com.armstrongconsulting.acprotasks.client;
2
+
3
+import com.sun.istack.NotNull;
4
+
5
+import feign.Feign;
6
+import feign.Logger;
7
+import feign.gson.GsonDecoder;
8
+import feign.gson.GsonEncoder;
9
+import feign.httpclient.ApacheHttpClient;
10
+
11
+public class AcproConnector
12
+{
13
+    private static AcproClient client;
14
+
15
+    public static AcproClient getClient(@NotNull String url)
16
+    {
17
+        if (client != null)
18
+        {
19
+            return client;
20
+        }
21
+
22
+        return Feign.builder()
23
+            .client(new ApacheHttpClient())
24
+            .encoder(new GsonEncoder())
25
+            .decoder(new GsonDecoder())
26
+            .logger(new Logger.JavaLogger(AcproClient.class))
27
+            .logLevel(Logger.Level.FULL)
28
+            .target(AcproClient.class, url);
29
+    }
30
+}

+ 13 - 0
src/main/java/com/armstrongconsulting/acprotasks/client/model/AcproItem.java

@@ -0,0 +1,13 @@
1
+package com.armstrongconsulting.acprotasks.client.model;
2
+
3
+import lombok.Data;
4
+import lombok.ToString;
5
+
6
+@Data
7
+@ToString
8
+public class AcproItem
9
+{
10
+    private Long id;
11
+    private String title;
12
+    private String description;
13
+}