Datasets:

ArXiv:
License:
File size: 4,448 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package httpcli;

import httpcli.adapter.RespBodyAdapter;
import java.io.IOException;
import java.nio.charset.Charset;

public class HttpRequest {
  /** Codificación predeterminada. */
  public static final Charset DEFAULT_ENCODING = Charset.forName("utf-8");

  /** Tiempo limite de espera por default. */
  public static final int DEFAULT_TIMEOUT = 2500 * 2 * 2;
  
  /** Metodo por default. */
  public static final String DEFAULT_METHOD = "GET";
  
  /** Metodo de la request: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE. */
  String method = DEFAULT_METHOD;
  
  /** Url de nuestra request. */
  HttpUrl url;
  
  /** Lista de encabezados adicionales de HTTP para esta peticion. */
  Headers headers;
 
  /** Parametros de nuestra request. */
  RequestBody body;
  
  /** Etiqueta para identificar la request. */ 
  Object tag = HttpRequest.class;
  
  /** Tiempo limite de espera. */
  int timeoutMs = DEFAULT_TIMEOUT;
  
  /** Codificacion. */
  Charset charset = DEFAULT_ENCODING;

  boolean isDebug;
  
  HttpCli cli = HttpCli.get();
  
  public HttpRequest() {
  }
  
  public HttpRequest(String method, String url, RequestBody body) {
    setMethod(method).setUrl(url).setBody(body);
  }
  
  public HttpRequest(String method, String url) {
    setMethod(method).setUrl(url);
  }
  
  public HttpRequest(String method, HttpUrl url, RequestBody body) {
    setMethod(method).setUrl(url).setBody(body);
  }
  
  public HttpRequest(String method, HttpUrl url) {
    setMethod(method).setUrl(url);
  }
    
  public String getMethod() {
    return method;
  }
  public HttpRequest setMethod(String method) {
    this.method = method.toUpperCase();
    return this;
  }
  
  public HttpUrl getUrl() {
    return url;
  }
  public HttpRequest setUrl(HttpUrl url) {
    this.url = url;
    return this;
  }
  public HttpRequest setUrl(String url) {
    return setUrl(new HttpUrl(url));
  }
  
  public Headers getHeaders() {
    return headers;
  }
  public HttpRequest setHeaders(Headers headers) {
    this.headers = headers;
    return this;
  }
  public HttpRequest addHeader(String name, String value) {
    if (headers == null) headers = new Headers();
    headers.add(name, value);
    return this;
  }

  public RequestBody getBody() {
    return body;
  }
  public HttpRequest setBody(RequestBody body) {
    this.body = body;
    return this;
  }
  
  public int getTimeoutMs() {
    return timeoutMs;
  }
  public HttpRequest setTimeoutMs(int timeoutMs) {
    this.timeoutMs = timeoutMs;
    return this;
  }

  public Charset getCharset() {
    return charset;
  }
  public HttpRequest setCharset(Charset charset) {
    this.charset = charset;
    return this;
  }

  
  public Object getTag() {
    return tag;
  }
  public HttpRequest setTag(Object tag) {
    this.tag = tag;
    return this;
  }

  public boolean isDebug() {
    return isDebug;
  }
  public HttpRequest setDebug(boolean isDebug) {
    this.isDebug = isDebug;
    return this;
  }
  
  
  public boolean requiresRequestBody() {
    return method.equals("POST") || method.equals("PUT") || method.equals("PATCH");
  }

  public String urlParams() {
    if (!requiresRequestBody())
      if (body != null)
        if (body instanceof FormBody) 
          return HttpUrl.toUrl(url.url, (FormBody)body, charset);
    
    return url.toString(charset);
  }
  
  @Override public String toString() {
    return "Request@" + hashCode() + " " + method + " " + url + "\nheaders:\n" 
            + headers + "\nbody: " + body + "\n\ntag=" + tag + ", timeoutMs=" + timeoutMs 
            + ", charset=" + charset + "\n---";
  }

  public void d(String format, Object... args) {
    if (isDebug) System.out.printf("Request@" + hashCode() + ": " + format + "\n", args);
  }
  public void d(RequestBody rb) throws IOException {
    if (isDebug) {
      rb.writeTo(System.out, charset);
      System.out.println("\n");
    }
  }
  
  public ResponseBody execute() throws Exception {
    return cli.execute(this);
  }
  
  public <V> V execute(RespBodyAdapter<V> adapter) throws Exception {
    return cli.execute(this, adapter);
  }
  public <V> V execute(Class<V> classOf) throws Exception {
    return cli.execute(this, classOf);
  }
  
  public <V> HttpCall<V> newCall(RespBodyAdapter<V> adapter) throws Exception {
    return cli.newCall(this, adapter);
  }
  public <V> HttpCall<V> newCall(Class<V> classOf) throws Exception {
    return cli.newCall(this, classOf);
  }
}