ApiConnection(String)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
2
package com.github.theresasogunle;
3
4
import com.mashape.unirest.http.HttpResponse;
5
import com.mashape.unirest.http.JsonNode;
6
import com.mashape.unirest.http.Unirest;
7
import com.mashape.unirest.http.exceptions.UnirestException;
8
import org.json.JSONObject;
9
import java.security.KeyManagementException;
10
import java.security.NoSuchAlgorithmException;
11
import java.util.HashMap;
12
import java.util.logging.Level;
13
import java.util.logging.Logger;
14
import javax.net.ssl.SSLContext;
15
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
16
import static org.apache.http.conn.ssl.SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
17
import org.apache.http.conn.ssl.SSLContexts;
18
import org.apache.http.impl.client.CloseableHttpClient;
19
import org.apache.http.impl.client.HttpClients;
20
21
22
23
/**
24
 * @author Theresa
25
 */
26
public class ApiConnection {
27
28
    final private String url;
0 ignored issues
show
Coding Style introduced by
The modifiers are customarily declared int the following order: annotations, scope (public, protected, private), abstract, static, final, transient, volatile, synchronized, native, strictfp. Consider reordering your modifiers to comply with this custom.

The Java specification lists the customary order.

Loading history...
29
  
30
    /**
31
     * @param url - Flutterwave API url
32
     */
33
    public ApiConnection(String url) {
34
       this.url = url;
35
     this.enforceTlsV1point2();
36
    }
37
     private void enforceTlsV1point2() {
38
        try {
39
            SSLContext sslContext = SSLContexts.custom()
40
                    .useTLS()
41
                    .build();
42
            SSLConnectionSocketFactory f = new SSLConnectionSocketFactory(
43
                    sslContext,
44
                    new String[]{"TLSv1.2"},
45
                    null,
46
                    BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
47
            CloseableHttpClient httpClient = HttpClients.custom()
48
                    .setSSLSocketFactory(f)
49
                    .build();
50
            Unirest.setHttpClient(httpClient);
51
52
        } catch (NoSuchAlgorithmException | KeyManagementException ex) {
53
            Logger.getLogger(ApiConnection.class.getName()).log(Level.SEVERE, null, ex);
54
        }
55
    }
56
57
     
58
    /**
59
     * Connects to and queries Flutterwave API with POST
60
     *
61
     * @param query - APIQuery containing parameters to send
62
     * @return - JSONObject containing API response
63
     */
64
    public JSONObject connectAndQuery(ApiQuery query) {
65
        try {
66
            HttpResponse<JsonNode> queryForResponse = Unirest.post(url)
67
                    .header("Accept", "application/json")
68
                    .fields(query.getParams())
69
                    .asJson();
70
            
71
           
72
            
73
            try{
74
            return queryForResponse.getBody().getObject();
75
            }catch(Exception ex){}
0 ignored issues
show
Bug introduced by
Consider removing the empty block or filling it with code. You can also add a comment to explain the empty block.
Loading history...
Best Practice introduced by
Ignoring an Exception may lead to hard to find bugs. Consider logging or rethrowing the original exception. If you want to throw a different exception, you can set the original exception as its cause to preserve the stacktrace.

When instantiating a new Exception, you can set another Exception as its cause.

See the Oracle documentation on Throwables.

Usage example

throw new Exception("Exception Message", originalException);

Complete Example:

class ReThrowException {
  public static void throwsException() {
        try {
            throw new Exception("I am the original exception");
        } catch (final Exception e) {
            throw new RuntimeException("I am the new exception", e);
        }
    }
    public static void main(String[] args) {
        try {
            throwsException();
        }
        catch (final RuntimeException e) {
            System.out.println(e.getMessage());
            System.out.println("and my cause is: " + e.getCause().getMessage());
            e.printStackTrace();
        }
    }
}
Loading history...
76
        } catch (UnirestException e) {
0 ignored issues
show
Best Practice introduced by
Ignoring an Exception may lead to hard to find bugs. Consider logging or rethrowing the original exception. If you want to throw a different exception, you can set the original exception as its cause to preserve the stacktrace.

When instantiating a new Exception, you can set another Exception as its cause.

See the Oracle documentation on Throwables.

Usage example

throw new Exception("Exception Message", originalException);

Complete Example:

class ReThrowException {
  public static void throwsException() {
        try {
            throw new Exception("I am the original exception");
        } catch (final Exception e) {
            throw new RuntimeException("I am the new exception", e);
        }
    }
    public static void main(String[] args) {
        try {
            throwsException();
        }
        catch (final RuntimeException e) {
            System.out.println(e.getMessage());
            System.out.println("and my cause is: " + e.getCause().getMessage());
            e.printStackTrace();
        }
    }
}
Loading history...
Bug introduced by
Consider removing the empty block or filling it with code. You can also add a comment to explain the empty block.
Loading history...
77
    }
78
         return  null;
79
    }
80
    /**
81
     * Connects to and queries API with POST
82
     *
83
     * @param query - HashMap containing parameters to send
84
     * @return - JSONObject containing API response
85
     */
86
    public JSONObject connectAndQuery(HashMap<String, Object> query) {
0 ignored issues
show
Best Practice introduced by
It is considered good practice to use the Java Collections API interfaces over specific implementations to define public objects and methods. Consider changing the type of query from HashMap to Map.
Loading history...
87
        try {
88
            HttpResponse<JsonNode> queryForResponse = Unirest.post(url)
89
                    .header("Accept", "application/json")
90
                    
91
                    .fields(query)
92
                    .asJson();
93
            return queryForResponse.getBody().getObject();
94
        } catch (UnirestException e) {
0 ignored issues
show
Bug introduced by
Consider removing the empty block or filling it with code. You can also add a comment to explain the empty block.
Loading history...
Best Practice introduced by
Ignoring an Exception may lead to hard to find bugs. Consider logging or rethrowing the original exception. If you want to throw a different exception, you can set the original exception as its cause to preserve the stacktrace.

When instantiating a new Exception, you can set another Exception as its cause.

See the Oracle documentation on Throwables.

Usage example

throw new Exception("Exception Message", originalException);

Complete Example:

class ReThrowException {
  public static void throwsException() {
        try {
            throw new Exception("I am the original exception");
        } catch (final Exception e) {
            throw new RuntimeException("I am the new exception", e);
        }
    }
    public static void main(String[] args) {
        try {
            throwsException();
        }
        catch (final RuntimeException e) {
            System.out.println(e.getMessage());
            System.out.println("and my cause is: " + e.getCause().getMessage());
            e.printStackTrace();
        }
    }
}
Loading history...
95
        }
96
        return null;
97
    }
98
99
    /**
100
     * Used to send a GET request to the Flutterwave API
101
     *
102
     * @return - JSONObject containing the API response
103
     */
104
    public JsonNode connectAndQueryWithGet() {
105
        try {
106
            HttpResponse<JsonNode> queryForResponse = Unirest.get(url)
107
                    .header("content-type", "application/json")
108
                    
109
                    .asJson();
110
            
111
            return queryForResponse.getBody();
112
           
113
        } catch (UnirestException e) {
0 ignored issues
show
Best Practice introduced by
Ignoring an Exception may lead to hard to find bugs. Consider logging or rethrowing the original exception. If you want to throw a different exception, you can set the original exception as its cause to preserve the stacktrace.

When instantiating a new Exception, you can set another Exception as its cause.

See the Oracle documentation on Throwables.

Usage example

throw new Exception("Exception Message", originalException);

Complete Example:

class ReThrowException {
  public static void throwsException() {
        try {
            throw new Exception("I am the original exception");
        } catch (final Exception e) {
            throw new RuntimeException("I am the new exception", e);
        }
    }
    public static void main(String[] args) {
        try {
            throwsException();
        }
        catch (final RuntimeException e) {
            System.out.println(e.getMessage());
            System.out.println("and my cause is: " + e.getCause().getMessage());
            e.printStackTrace();
        }
    }
}
Loading history...
114
            System.out.println("Cant query at this time!");
115
        }
116
        return null;
117
    }
118
    
119
120
}
121