-
Notifications
You must be signed in to change notification settings - Fork 6
/
BlazeMeterUtils.java
134 lines (113 loc) · 4.46 KB
/
BlazeMeterUtils.java
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
/**
* Copyright 2018 BlazeMeter Inc.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blazemeter.api.utils;
import com.blazemeter.api.exception.UnexpectedResponseException;
import com.blazemeter.api.http.HttpUtils;
import com.blazemeter.api.logging.Logger;
import com.blazemeter.api.logging.UserNotifier;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import okhttp3.Credentials;
import okhttp3.Request;
import org.apache.commons.lang.StringUtils;
public class BlazeMeterUtils extends HttpUtils {
private String apiKeyId;
private String apiKeySecret;
protected String address;
protected String dataAddress;
protected UserNotifier notifier;
/**
* @param apiKeyId - BlazeMeter Api Key Id
* @param apiKeySecret - BlazeMeter Api Key Secret
* @param address - BlazeMeter app address: https://a.blazemeter.com/
* @param dataAddress - BlazeMeter data address: https://data.blazemeter.com/
* @param notifier - user notifier, to show user information
* @param logger - logger, for log events of http requests / response etc.
*/
public BlazeMeterUtils(String apiKeyId, String apiKeySecret,
String address, String dataAddress,
UserNotifier notifier, Logger logger) {
super(logger);
this.address = address;
this.dataAddress = dataAddress;
this.apiKeyId = apiKeyId;
this.apiKeySecret = apiKeySecret;
this.notifier = notifier;
}
public BlazeMeterUtils(String address, String dataAddress, UserNotifier notifier, Logger logger) {
this("", "", address, dataAddress, notifier, logger);
}
protected boolean isValidCredantials(String apiKeyId, String apiKeySecret) {
return !StringUtils.isBlank(apiKeyId) && !StringUtils.isBlank(apiKeySecret);
}
@Override
protected Request.Builder addRequiredHeader(Request.Builder requestBuilder) {
return isValidCredantials(apiKeyId, apiKeySecret) ?
requestBuilder.addHeader(AUTHORIZATION, Credentials.basic(apiKeyId, apiKeySecret)) :
requestBuilder;
}
@Override
protected JSONObject processResponse(String response) {
String error = extractErrorMessage(response);
if (error != null) {
logger.error("Received response with the following error: " + error);
throw new UnexpectedResponseException("Received response with the following error: " + error);
}
return JSONObject.fromObject(response);
}
@Override
protected String extractErrorMessage(String response) {
if (response != null && !response.isEmpty()) {
try {
JSONObject jsonResponse = JSONObject.fromObject(response);
JSONObject errorObj = jsonResponse.getJSONObject("error");
if (errorObj.containsKey("message")) {
return errorObj.getString("message");
}
} catch (JSONException ex) {
logger.debug("Cannot parse response: " + response, ex);
return "Cannot parse response: " + response;
}
}
return null;
}
public UserNotifier getNotifier() {
return notifier;
}
public String getAddress() {
return address;
}
public String getDataAddress() {
return dataAddress;
}
public void setApiKeyId(String apiKeyId) {
this.apiKeyId = apiKeyId;
}
public void setApiKeySecret(String apiKeySecret) {
this.apiKeySecret = apiKeySecret;
}
public void setAddress(String address) {
this.address = address;
}
public void setDataAddress(String dataAddress) {
this.dataAddress = dataAddress;
}
public static long getCheckTimeout() {
try {
return Long.parseLong(System.getProperty("bzm.checkTimeout", "10000"));
} catch (NumberFormatException ex) {
return 10000;
}
}
}