11package ru .redguy .redguyapi ;
22
3- import org . jetbrains . annotations . Contract ;
4- import org . jetbrains . annotations . NotNull ;
3+ import com . google . gson . Gson ;
4+ import okhttp3 . OkHttpClient ;
55import ru .redguy .redguyapi .hashes .Hashes ;
66import ru .redguy .redguyapi .links .Links ;
77import ru .redguy .redguyapi .minecraft .Minecraft ;
8- import ru .redguy .redguyapi .news .News ;
98import ru .redguy .redguyapi .text .Text ;
109import ru .redguy .redguyapi .token .Token ;
1110import ru .redguy .redguyapi .users .Users ;
1211
13- import java .util .HashMap ;
1412import java .util .Map ;
13+ import java .util .Objects ;
1514
1615public class RedGuyApi {
17-
18- private final Map <String , String > options ;
19-
20- private RedGuyApi () {
21- this .options = new HashMap <>();
22- }
16+ private final RedGuyApiClient client ;
2317
2418 /**
2519 * Constructs main api object.
2620 *
2721 * @param token Your api token.
2822 */
2923 public RedGuyApi (String token ) {
30- this . options = verifyOptions ( new HashMap <>(), token );
24+ this ( RedGuyApiConfig . configBuilder ( token ). build () );
3125 }
3226
3327 /**
3428 * Constructs main api object with specified params.
3529 *
3630 * @param token Your api token.
3731 * @param options Additional client params
32+ * @deprecated use {@link #builder(String)}.
3833 */
34+ @ Deprecated
3935 public RedGuyApi (String token , Map <String , String > options ) {
40- this .options = verifyOptions (options , token );
36+ RedGuyApiConfig .Builder builder = RedGuyApiConfig .configBuilder (token );
37+ if (options .containsKey ("baseUrl" )) {
38+ builder .baseUrl (options .get ("baseUrl" ));
39+ }
40+ this .client = new RedGuyApiClient (builder .build ());
41+ }
42+
43+ public RedGuyApi (RedGuyApiConfig config ) {
44+ this .client = new RedGuyApiClient (Objects .requireNonNull (config , "config" ));
4145 }
4246
43- @ NotNull
44- @ Contract ("_, _ -> param1" )
45- private Map <String , String > verifyOptions (@ NotNull Map <String , String > options , String token ) {
46- options .put ("token" , token );
47- return options ;
47+ public static Builder builder (String token ) {
48+ return new Builder (token );
4849 }
4950
5051 /**
@@ -53,7 +54,7 @@ private Map<String, String> verifyOptions(@NotNull Map<String, String> options,
5354 * @return Minecraft API section
5455 */
5556 public Minecraft minecraft () {
56- return new Minecraft (options );
57+ return new Minecraft (client );
5758 }
5859
5960 /**
@@ -62,7 +63,7 @@ public Minecraft minecraft() {
6263 * @return Token API section
6364 */
6465 public Token token () {
65- return new Token (options );
66+ return new Token (client );
6667 }
6768
6869 /**
@@ -71,16 +72,7 @@ public Token token() {
7172 * @return Users API section
7273 */
7374 public Users users () {
74- return new Users (options );
75- }
76-
77- /**
78- * Returns new instance of News API section
79- *
80- * @return News API section
81- */
82- public News news () {
83- return new News (options );
75+ return new Users (client );
8476 }
8577
8678 /**
@@ -89,7 +81,7 @@ public News news() {
8981 * @return Hashes API section
9082 */
9183 public Hashes hashes () {
92- return new Hashes (options );
84+ return new Hashes (client );
9385 }
9486
9587 /**
@@ -98,10 +90,41 @@ public Hashes hashes() {
9890 * @return Links API section
9991 */
10092 public Links links () {
101- return new Links (options );
93+ return new Links (client );
10294 }
10395
10496 public Text text () {
105- return new Text (options );
97+ return new Text (client );
98+ }
99+
100+ public static final class Builder {
101+ private final RedGuyApiConfig .Builder configBuilder ;
102+
103+ private Builder (String token ) {
104+ this .configBuilder = RedGuyApiConfig .configBuilder (token );
105+ }
106+
107+ public Builder baseUrl (String baseUrl ) {
108+ configBuilder .baseUrl (baseUrl );
109+ return this ;
110+ }
111+
112+ public Builder httpClient (OkHttpClient httpClient ) {
113+ configBuilder .httpClient (httpClient );
114+ return this ;
115+ }
116+
117+ public Builder gson (Gson gson ) {
118+ configBuilder .gson (gson );
119+ return this ;
120+ }
121+
122+ public RedGuyApiConfig config () {
123+ return configBuilder .build ();
124+ }
125+
126+ public RedGuyApi build () {
127+ return new RedGuyApi (config ());
128+ }
106129 }
107130}
0 commit comments