1
1
package com .chuntung .plugin .gistsnippet .service ;
2
2
3
3
import com .chuntung .plugin .gistsnippet .dto .api .GistDTO ;
4
- import com .intellij .notification .*;
5
4
import com .intellij .openapi .components .ServiceManager ;
6
5
import com .intellij .openapi .diagnostic .Logger ;
7
- import com .intellij .openapi .project .Project ;
8
6
import com .intellij .util .containers .ContainerUtil ;
9
7
import org .jetbrains .plugins .github .api .GithubApiRequest ;
10
8
import org .jetbrains .plugins .github .api .GithubApiRequestExecutor ;
17
15
import java .util .Map ;
18
16
import java .util .concurrent .atomic .AtomicReference ;
19
17
18
+ /**
19
+ * API Doc: https://developer.github.com/v3/gists/
20
+ */
20
21
public class GistSnippetService {
21
22
public static final Logger logger = Logger .getInstance (GistSnippetService .class );
22
23
@@ -78,11 +79,12 @@ private List<String> putIntoCache(List<GistDTO> gistList) {
78
79
return null ;
79
80
}
80
81
81
- private List <GistDTO > decideResult (GithubAccount account , AtomicReference <List <GistDTO >> result , List <String > idList ) {
82
- if (result .get () == null && idList != null ) {
82
+ private List <GistDTO > decideResult (GithubAccount account , AtomicReference <List <GistDTO >> result , List <String > cacheList ) {
83
+ // load from cache
84
+ if (result .get () == null && cacheList != null ) {
83
85
// N + 1
84
- List <GistDTO > gistList = new ArrayList <>(idList .size ());
85
- for (String gistId : idList ) {
86
+ List <GistDTO > gistList = new ArrayList <>(cacheList .size ());
87
+ for (String gistId : cacheList ) {
86
88
gistList .add (getGistDetail (account , gistId , false ));
87
89
}
88
90
result .set (gistList );
@@ -100,7 +102,7 @@ public List<GistDTO> queryStarredGist(GithubAccount account, boolean forced) {
100
102
}
101
103
102
104
AtomicReference <List <GistDTO >> result = new AtomicReference <>();
103
- List <String > list = scopeCache .computeIfAbsent (key , (k ) -> {
105
+ List <String > cacheList = scopeCache .computeIfAbsent (key , (k ) -> {
104
106
try {
105
107
GithubApiRequest .Get .JsonList <GistDTO > request = new GithubApiRequest .Get .JsonList <>(STARRED_GISTS_URL , GistDTO .class , MIME_TYPE );
106
108
GithubApiRequestExecutor executor = GithubApiRequestExecutorManager .getInstance ().getExecutor (account );
@@ -113,7 +115,7 @@ public List<GistDTO> queryStarredGist(GithubAccount account, boolean forced) {
113
115
}
114
116
});
115
117
116
- return decideResult (account , result , list );
118
+ return decideResult (account , result , cacheList );
117
119
}
118
120
119
121
// queryPublicGist
@@ -122,15 +124,21 @@ public List<GistDTO> getPublicGist(GithubAccount account) {
122
124
return null ;
123
125
}
124
126
127
+ /**
128
+ * @param account
129
+ * @param gistId
130
+ * @param forced true to load file content from remote server
131
+ * @return
132
+ */
125
133
public GistDTO getGistDetail (GithubAccount account , String gistId , boolean forced ) {
126
134
if (forced ) {
127
135
gistCache .computeIfPresent (gistId , (k , v ) -> gistCache .remove (k ));
128
136
}
129
137
130
138
return gistCache .computeIfAbsent (gistId , (k ) -> {
131
139
String url = String .format (GIST_DETAIL_URL , gistId );
140
+ GithubApiRequest .Get .Json <GistDTO > request = new GithubApiRequest .Get .Json (url , GistDTO .class , MIME_TYPE );
132
141
try {
133
- GithubApiRequest .Get .Json <GistDTO > request = new GithubApiRequest .Get .Json (url , GistDTO .class , MIME_TYPE );
134
142
GithubApiRequestExecutor executor = GithubApiRequestExecutorManager .getInstance ().getExecutor (account );
135
143
return executor .execute (request );
136
144
} catch (IOException e ) {
@@ -139,4 +147,19 @@ public GistDTO getGistDetail(GithubAccount account, String gistId, boolean force
139
147
}
140
148
});
141
149
}
150
+
151
+ public void deleteGist (GithubAccount account , List <String > gistIds ) {
152
+ try {
153
+ GithubApiRequestExecutor executor = GithubApiRequestExecutorManager .getInstance ().getExecutor (account );
154
+ for (String gistId : gistIds ) {
155
+ String url = String .format (GIST_DETAIL_URL , gistId );
156
+ GithubApiRequest .Delete delete = new GithubApiRequest .Delete (url );
157
+ executor .execute (delete );
158
+ gistCache .remove (gistId );
159
+ }
160
+ } catch (IOException e ) {
161
+ logger .info ("Failed to delete gist, error: " + e .getMessage ());
162
+ throw new GistException (e );
163
+ }
164
+ }
142
165
}
0 commit comments