Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hw framework #6

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions explain
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$touch test-area
$nano test-area
$ git add test-area
$ git commit -m "create test area"
$ git checkout -b branch-for-conflict #2nd branch
$ nano test-area #1st change
$ git commit -a -m "add change A"
$ git checkout main
$ nano test-area #2nd branch
$ git commit -a -m "add change B"
$ git merge branch-for-conflict #try to merge

merge conflict occured:

Auto-merging test-area
CONFLICT (content): Merge conflict in test-area
Automatic merge failed; fix conflicts and then commit the result.

$ nano test-area #solve merge conflict
$ git commit -a -m "merge into branch 'main' branch 'branch-for-conflict'"

173 changes: 173 additions & 0 deletions src/main/java/apiclient/ApiClientUsers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package apiclient;

import config.Config;
import data.Endpoints;
import org.json.JSONObject;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class ApiClientUsers {
private static String usersUrl () {return Config.BASE_URL + Endpoints.USERS;}
private static String userUrl (int id) {return Config.BASE_URL + Endpoints.USERS + "/" + id;}
private static String registerUrl () {return Config.BASE_URL + Endpoints.REGISTER;}
private static String loginUrl () {return Config.BASE_URL + Endpoints.LOGIN;}

private static HttpURLConnection usersResource () throws IOException {
URL url = new URL (usersUrl());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

return connection;
}



private static HttpURLConnection userResource (int id) throws IOException {

URL url = new URL(userUrl(id));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

return connection;
}

private static HttpURLConnection registerResource () throws IOException {
URL url = new URL(registerUrl());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

return connection;
}
private static HttpURLConnection loginResource () throws IOException {
URL url = new URL(loginUrl());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

return connection;
}

private static String rawResponse (HttpURLConnection connection) throws IOException {
InputStream stream = connection.getErrorStream();
if (stream == null) stream = connection.getInputStream();
String line;
StringBuilder response = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();



return response.toString();
}

private static JSONObject jsonResponse (HttpURLConnection connection) throws IOException {
JSONObject response = new JSONObject(rawResponse(connection));

return response;
}

private static void post (HttpURLConnection connection, String body) throws IOException {
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
connection.setRequestMethod("POST");
try(OutputStream os = connection.getOutputStream()) {
byte[] input = body.getBytes("utf-8");
for (int i = 0; i < input.length; i++) {
os.write(input[i]);
}
}
}

private static void put (HttpURLConnection connection, String body) throws IOException {
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
try(OutputStream os = connection.getOutputStream()) {
byte[] input = body.getBytes("utf-8");
for (int i = 0; i < input.length; i++) {
os.write(input[i]);
}
}
}




public Response getUser (int id) throws IOException {
HttpURLConnection connection = userResource(id);
connection.setRequestMethod("GET");
connection.disconnect();

Response response = new Response(connection.getResponseCode(), jsonResponse(connection));

return response;
}

public Response getUsers () throws IOException {
HttpURLConnection connection = usersResource();
connection.setRequestMethod("GET");
connection.disconnect();

Response response = new Response(connection.getResponseCode(), jsonResponse(connection));

return response;
}

public Response createUser (String name, String job ) throws IOException {
HttpURLConnection connection = usersResource();
post(connection, "{\"name\": \"" + name + "\", \"job\": \""+job+"\"}");
//System.out.println("Status code: "+connection.getResponseCode());
connection.disconnect();

Response response = new Response(connection.getResponseCode(), jsonResponse(connection));

return response;
}

public Response updateUserPut (int id, String name, String job ) throws IOException {
HttpURLConnection connection = userResource(id);
put(connection, "{\"name\": \"" + name + "\", \"job\": \""+job+"\"}");
//System.out.println("Status code: "+connection.getResponseCode());
connection.disconnect();

Response response = new Response(connection.getResponseCode(), jsonResponse(connection));

return response;
}

public Response register (String email, String password) throws IOException {
HttpURLConnection connection = registerResource();
post(connection, "{\"email\": \"" + email + "\", \"password\": \""+password+"\"}");
connection.disconnect();

Response response = new Response(connection.getResponseCode(), jsonResponse(connection));

return response;
}

public Response login (String email, String password) throws IOException {
HttpURLConnection connection = loginResource();
post(connection, "{\"email\": \"" + email + "\", \"password\": \""+password+"\"}");
connection.disconnect();

Response response = new Response(connection.getResponseCode(), jsonResponse(connection));

return response;
}

public Response deleteUser (int id) throws IOException {
HttpURLConnection connection = userResource(id);
connection.setRequestMethod("DELETE");
connection.disconnect();

Response response = new Response(connection.getResponseCode());

return response;
}



}
17 changes: 17 additions & 0 deletions src/main/java/apiclient/Response.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package apiclient;

import org.json.JSONObject;

public class Response {
public int statusCode;
public JSONObject body;

public Response (int statusCode) {
this.statusCode = statusCode;
}

public Response (int statusCode, JSONObject body) {
this.statusCode = statusCode;
this.body = body;
}
}
5 changes: 5 additions & 0 deletions src/main/java/config/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package config;

public class Config {
public static final String BASE_URL = "https://reqres.in";
}
7 changes: 7 additions & 0 deletions src/main/java/data/Endpoints.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package data;

public class Endpoints {
public static final String USERS = "/api/users";
public static final String REGISTER = "/api/register";
public static final String LOGIN = "/api/login";
}
Loading