added user register

This commit is contained in:
Ekaterina Tuzova
2015-09-05 12:20:34 +03:00
parent f644f258e9
commit d8460bc7b0
3 changed files with 61 additions and 6 deletions
@@ -27,10 +27,7 @@ import org.apache.commons.codec.binary.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.*;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
@@ -76,6 +73,28 @@ public class EduStepicConnector {
return postCredentials(user, password);
}
public static boolean createUser(@NotNull final String user, @NotNull final String password) {
final HttpPost userRequest = new HttpPost(stepicApiUrl + "users");
initializeClient();
setHeaders(userRequest, "application/json");
String requestBody = new Gson().toJson(new UserWrapper(user, password));
userRequest.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try {
final CloseableHttpResponse response = ourClient.execute(userRequest);
final String responseString = EntityUtils.toString(response.getEntity());
final StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() != 201) {
LOG.error("Failed to create user " + responseString);
return false;
}
}
catch (IOException e) {
LOG.error(e.getMessage());
}
return true;
}
private static void initializeClient() {
final HttpGet request = new HttpGet(stepicUrl);
request.addHeader(new BasicHeader("referer", "https://stepic.org"));
@@ -818,4 +837,26 @@ public class EduStepicConnector {
}
static class User {
String first_name;
String last_name;
String email;
String password;
public User(String user, String password) {
email = user;
this.password = password;
this.first_name = user;
this.last_name = user;
}
}
static class UserWrapper {
User user;
public UserWrapper(String user, String password) {
this.user = new User(user, password);
}
}
}
@@ -4,6 +4,7 @@ import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class LoginDialog extends DialogWrapper {
private final LoginPanel myLoginPanel;
@@ -18,7 +19,7 @@ public class LoginDialog extends DialogWrapper {
@NotNull
protected Action[] createActions() {
return new Action[]{getOKAction(), getCancelAction()};
return new Action[]{getOKAction(), new RegisterAction(), getCancelAction()};
}
@Override
@@ -63,4 +64,17 @@ public class LoginDialog extends DialogWrapper {
this.password = password;
}
}
protected class RegisterAction extends DialogWrapperAction {
private RegisterAction() {
super("Register");
}
@Override
protected void doAction(ActionEvent e) {
EduStepicConnector.createUser(myLoginPanel.getAuthData().email, myLoginPanel.getAuthData().password);
doOKAction();
}
}
}
@@ -48,7 +48,7 @@ public class LoginPanel {
}
@NotNull
public String getLogin() {
private String getLogin() {
return myLoginTextField.getText().trim();
}