Skip to content

Commit

Permalink
Adds a new setting to provide an API key for hastebin
Browse files Browse the repository at this point in the history
  • Loading branch information
lukbukkit committed Sep 4, 2023
1 parent d1af23e commit 7b7f2a0
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 8 deletions.
5 changes: 4 additions & 1 deletion DiscordBeam/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import org.jetbrains.changelog.markdownToHTML

version = "1.20.6"
version = "1.20.7"
group = "de.lukweb.share"

plugins {
Expand Down Expand Up @@ -36,6 +36,9 @@ tasks {
patchPluginXml {
changeNotes.set(provider {
"""
1.20.7:
* Prepares for 2023.2 release
1.20.6:
* Prepares for 2023.1 release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class DiscordSettingsPage implements SearchableConfigurable {

public DiscordSettingsPage() {
this.disposable = Disposer.newDisposable();
Disposer.register(ApplicationManager.getApplication().getService(DiscordSettings.class), disposable);
Disposer.register(DiscordSettings.getInstance(), disposable);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public void onHaste(String hasteUrl) {
latch.countDown();
}

@Override
public void onAuthorizationRequired() {
theError.set(new IOException("hastebin requires an API key, configure it in the 'Haste It' settings tab"));
latch.countDown();
}

@Override
public void onFailure(Throwable ex) {
theError.set(new IOException(ex));
Expand Down
6 changes: 5 additions & 1 deletion HasteIt/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import org.jetbrains.changelog.markdownToHTML

version = "1.20.6"
version = "1.20.7"
group = "de.lukweb.share"

plugins {
Expand Down Expand Up @@ -36,6 +36,10 @@ tasks {
patchPluginXml {
changeNotes.set(provider {
"""
1.20.7:
* Prepares for 2023.2 release
* Implements a new setting to provide an API key for hastebin
1.20.6:
* Prepares for 2023.1 release
Expand Down
5 changes: 5 additions & 0 deletions HasteIt/src/main/java/de/lukweb/hasteit/HasteMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void onHaste(String hasteUrl) {
uploadSuccessNotification(hasteUrl, fileName, project);
}

@Override
public void onAuthorizationRequired() {
errorNotification("Configure an API key for hastebin in the settings.");
}

@Override
public void onFailure(Throwable ex) {
errorNotification(ex.getClass().getName() + " while uploading: " + ex.getMessage());
Expand Down
16 changes: 16 additions & 0 deletions HasteIt/src/main/java/de/lukweb/hasteit/HasteSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,23 @@ public String computeUploadURL() {
}

public String computeFileURL(String hasteCode, String extension) {
if (getBaseURL().equals(DEFAULT_URL)) {
return getBaseURL() + "/share/" + hasteCode + "." + extension;
}

return getBaseURL() + "/" + hasteCode + "." + extension;
}

public String getAPIKey() {
return getState().getAPIKey();
}

public String getAPIKeyOrEmpty() {
if (getAPIKey() == null) {
return "";
} else {
return getAPIKey();
}
}

}
31 changes: 29 additions & 2 deletions HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsPage.form
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="de.lukweb.hasteit.HasteSettingsPage">
<grid id="27dc6" binding="panelMain" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="panelMain" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
Expand All @@ -10,7 +10,7 @@
<children>
<vspacer id="55a50">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="41678" class="javax.swing.JLabel" binding="labelHasteUrl">
Expand Down Expand Up @@ -40,6 +40,33 @@
<toolTipText value="Reset the value of the text field to the URL hastebin.com"/>
</properties>
</component>
<component id="26c52" class="javax.swing.JLabel" binding="labelApiKey">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<labelFor value="fafbe"/>
<text value="API Key"/>
</properties>
</component>
<component id="8d672" class="com.intellij.ui.components.JBTextField" binding="textApiKey">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="c241e" class="javax.swing.JButton" binding="buttonGetApiKey">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Get"/>
<toolTipText value="Toptal hosts hastebin and requires an API key. You can open their website to get an API key by pressing this button"/>
</properties>
</component>
</children>
</grid>
</form>
44 changes: 41 additions & 3 deletions HasteIt/src/main/java/de/lukweb/hasteit/HasteSettingsPage.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.lukweb.hasteit;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
Expand All @@ -16,6 +15,10 @@

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class HasteSettingsPage implements SearchableConfigurable {

Expand All @@ -25,10 +28,13 @@ public class HasteSettingsPage implements SearchableConfigurable {
private JLabel labelHasteUrl;
private JBTextField textHasteUrl;
private JButton buttonReset;
private JLabel labelApiKey;
private JBTextField textApiKey;
private JButton buttonGetApiKey;

public HasteSettingsPage() {
disposable = Disposer.newDisposable();
Disposer.register(ApplicationManager.getApplication().getService(HasteSettings.class), disposable);
Disposer.register(HasteSettings.getInstance(), disposable);
}

@Override
Expand Down Expand Up @@ -59,6 +65,22 @@ protected void textChanged(@NotNull DocumentEvent e) {

buttonReset.addActionListener(l -> textHasteUrl.setText(HasteSettings.DEFAULT_URL));

labelApiKey.setLabelFor(textApiKey);
textApiKey.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(@NotNull DocumentEvent e) {
updateResetButton();
}
});
buttonGetApiKey.addActionListener(l -> {
try {
URL docUrl = new URI("https://www.toptal.com/developers/hastebin/documentation").toURL();
ShareWebTools.openURL(docUrl);
} catch (URISyntaxException | MalformedURLException e) {
throw new RuntimeException(e);
}
});

reset();
updateResetButton();

Expand All @@ -71,7 +93,12 @@ public JComponent getPreferredFocusedComponent() {
}

private void updateResetButton() {
boolean canReset = !textHasteUrl.getText().equals(textHasteUrl.getEmptyText().getText());
HasteSettings settings = HasteSettings.getInstance();

boolean canReset =
!textHasteUrl.getText().equals(textHasteUrl.getEmptyText().getText()) ||
!textApiKey.getText().equals(settings.getAPIKeyOrEmpty());

buttonReset.setEnabled(canReset);
}

Expand All @@ -83,13 +110,18 @@ public boolean isModified() {
return true;
}

if (Configurable.isFieldModified(textApiKey, settingsState.getAPIKeyOrEmpty())) {
return true;
}

return false;
}

@Override
public void reset() {
HasteSettings settings = HasteSettings.getInstance();
textHasteUrl.setText(settings.getBaseURL());
textApiKey.setText(settings.getAPIKeyOrEmpty());
}

@Override
Expand All @@ -108,6 +140,12 @@ public void apply() throws ConfigurationException {
settingsState.setBaseURL(HasteSettings.DEFAULT_URL);
reset();
}

if (textApiKey.getText().isEmpty()) {
settingsState.getState().setAPIKey(null);
} else {
settingsState.getState().setAPIKey(textApiKey.getText());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class HasteSettingsState implements ShareSettingsState {

private String customUrl = null;
private String apiKey = null;

public String getCustomUrl() {
return customUrl;
Expand All @@ -13,4 +14,12 @@ public String getCustomUrl() {
public void setCustomUrl(String customUrl) {
this.customUrl = customUrl;
}

public String getAPIKey() {
return apiKey;
}

public void setAPIKey(String apiKey) {
this.apiKey = apiKey;
}
}
10 changes: 10 additions & 0 deletions HasteIt/src/main/java/de/lukweb/hasteit/HasteUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public void upload(String content, String extension, HasteResult result) {
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
if (settings.getAPIKey() != null) {
con.setRequestProperty("Authorization", "Bearer " + settings.getAPIKey());
}

// Send the post request
con.setDoOutput(true);
Expand All @@ -38,6 +41,11 @@ public void upload(String content, String extension, HasteResult result) {
wr.flush();
wr.close();

if (con.getResponseCode() == 401) {
result.onAuthorizationRequired();
return;
}

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
Expand All @@ -61,6 +69,8 @@ public void upload(String content, String extension, HasteResult result) {

public interface HasteResult extends ShareResult {

void onAuthorizationRequired();

void onHaste(String hasteUrl);

}
Expand Down

0 comments on commit 7b7f2a0

Please sign in to comment.