Skip to content

Instantly share code, notes, and snippets.

@ganbat96
Last active March 13, 2021 16:01
Show Gist options
  • Save ganbat96/3ced5a52f7c495cd74216d19bdb3fc10 to your computer and use it in GitHub Desktop.
Save ganbat96/3ced5a52f7c495cd74216d19bdb3fc10 to your computer and use it in GitHub Desktop.
API дуудах жишээ client - Java - (2 files : API.java, Main.java)
package mn.artlab.api.client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class API {
public static final String BASE_URL = "https://api.artlab.mn/";
public API() {
super();
}
public static Result get(String path, Map<String, String> headers) {
return call("GET", path, null, headers);
}
public static Result post(String path, String body, Map<String, String> headers) {
return call("POST", path, body, headers);
}
private static Result call(String method, String path, String body, Map<String, String> headers) {
HttpURLConnection conn = null;
try {
URL url = new URL(BASE_URL + path);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(method);
conn.setRequestProperty("Content-Type", "application/json");
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
if (body != null) {
OutputStream os = conn.getOutputStream();
os.write( body.getBytes() );
os.flush();
}
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
return new Result(conn.getResponseCode());
}
BufferedReader br = new BufferedReader(
new InputStreamReader((conn.getInputStream())));
StringBuilder value = new StringBuilder();
String output = br.readLine();
while (output != null) {
value.append(output).append("\n");
output = br.readLine();
}
return new Result(conn.getResponseCode(), value.toString(), conn.getHeaderField("Authorization"));
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
public static Builder builder(String path) {
return new Builder().path(path);
}
public static class Result {
public final int code;
public final String result;
public final String authorization;
public Result(int code) {
this(code, null, null);
}
public Result(int code, String result, String authorization) {
this.code = code;
this.result = result;
this.authorization = authorization;
}
}
public static class Builder {
private String path;
private String method;
private String inputParam;
private Map<String, String> headers = new HashMap<>();
public Builder path(String path) {
this.path = path;
return this;
}
public Builder authorization(String authorization) {
headers.put("Authorization", authorization);
return this;
}
public Builder method(String method) {
this.method = method;
return this;
}
public Builder input(String inputParam) {
this.inputParam = inputParam;
return this;
}
public Result call() {
return API.call(method, path, inputParam, headers);
}
public Result get() {
return method("GET").call();
}
public Result post() {
return method("POST").call();
}
}
}
package mn.artlab.api.client;
public class Main {
public static void main(String[] args) {
final String TEMPLATE = ("{" +
"`username`:`@user`," +
"`password`:`@pass`," +
"`type`:`@type`" +
"}").replace('`', '"');
// Хэрэглэгчийн мэдээлэл. ArtLab-с авна.
final String username = "username-from-artlab";
final String password = "password-from-artlab";
final String type = "type-from-artlab";
// login хийх
API.Result r0 = API.builder("auth/login").input(TEMPLATE
.replace("@user", username)
.replace("@pass", password)
.replace("@type", type)).post();
// login хийж чадсан эсэхээ шалгах
// бусад resource-ууд руу хандахдаа иймэрхүү байдлаар шалгана
API.Result r1 = API.builder("auth/check").authorization(r0.authorization).get();
System.out.println(r1.result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment