Skip to content

Commit bf584cc

Browse files
committed
init marketing-api-oceanengine
1 parent f0bf6c0 commit bf584cc

File tree

283 files changed

+18737
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

283 files changed

+18737
-0
lines changed

marketing-api-oceanengine/pom.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>io.github.hyq0719</groupId>
9+
<artifactId>marketing-api-sdk</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>marketing-api-oceanengine</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
<name>Marketing API - Ocean Engine Java SDK</name>
16+
<description>巨量引擎开放平台 Java SDK</description>
17+
18+
<properties>
19+
<maven.compiler.source>${java.version}</maven.compiler.source>
20+
<maven.compiler.target>${java.version}</maven.compiler.target>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>io.github.hyq0719</groupId>
26+
<artifactId>marketing-api-common</artifactId>
27+
<version>${common.version}</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.google.code.gson</groupId>
31+
<artifactId>gson</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>io.gsonfire</groupId>
35+
<artifactId>gson-fire</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
<scope>compile</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.hyq0719.mktapi.oceanengine;
2+
3+
import com.hyq0719.mktapi.common.ApiClient;
4+
import com.hyq0719.mktapi.common.executor.http.HttpHandler;
5+
import com.hyq0719.mktapi.common.token.ExternalTokenService;
6+
import com.hyq0719.mktapi.common.token.cache.ITokenLocalCache;
7+
8+
public class OceanApiClient extends ApiClient {
9+
10+
public OceanApiClient(ITokenLocalCache iTokenLocalCache, HttpHandler httpsClient,
11+
ExternalTokenService externalTokenService) {
12+
super(iTokenLocalCache, httpsClient, externalTokenService);
13+
}
14+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package com.hyq0719.mktapi.oceanengine;
2+
3+
4+
import com.hyq0719.mktapi.common.ApiClient;
5+
import com.hyq0719.mktapi.common.ApiRequest;
6+
import com.hyq0719.mktapi.common.ApiResponse;
7+
import com.hyq0719.mktapi.common.RetryStrategy;
8+
import com.hyq0719.mktapi.common.advice.ApiRequestAdvice;
9+
import com.hyq0719.mktapi.common.annotation.ApiRequestMapping;
10+
import com.hyq0719.mktapi.common.constant.AuthConstants;
11+
import com.hyq0719.mktapi.common.exception.ApiException;
12+
import com.hyq0719.mktapi.common.executor.parameter.BaseUrl;
13+
import com.hyq0719.mktapi.common.executor.parameter.RequestParam;
14+
import com.hyq0719.mktapi.oceanengine.bean.CodeKey;
15+
import com.hyq0719.mktapi.oceanengine.bean.TokenKey;
16+
import lombok.extern.slf4j.Slf4j;
17+
import org.apache.commons.lang3.StringUtils;
18+
19+
import java.util.Map;
20+
import java.util.Objects;
21+
22+
@Slf4j
23+
public class OceanApiRequest<T extends TokenKey, R extends CodeKey> extends ApiRequest<T, R> {
24+
25+
private static final BaseUrl BASE_URL = BaseUrl.builder()
26+
.scheme("https")
27+
.host("ad.oceanengine.com/open_api")
28+
.version("2")
29+
.build();
30+
31+
private final String path;
32+
33+
private final String method;
34+
35+
private final Boolean usePostBody;
36+
37+
private final String[] contentTypes;
38+
39+
private final String version;
40+
41+
private final String host;
42+
43+
private ApiClient apiClient;
44+
45+
private RetryStrategy retryStrategy;
46+
47+
public OceanApiRequest() {
48+
ApiRequestMapping annotation = getClass().getAnnotation(ApiRequestMapping.class);
49+
if (Objects.isNull(annotation)) {
50+
throw new RuntimeException("must be @ApiRequestMapping");
51+
}
52+
method = annotation.method();
53+
path = annotation.value();
54+
version = StringUtils.isEmpty(annotation.version()) ? BASE_URL.getVersion() : annotation.version();
55+
host = StringUtils.isEmpty(annotation.host()) ? BASE_URL.getHost() : annotation.host();
56+
usePostBody = annotation.usePostBody();
57+
contentTypes = annotation.contentTypes();
58+
59+
log.info("[load ApiRequestMapping] method:{},path:{},version:{},host:{},usePostBody:{},contentTypes:{}", method,
60+
path, version, host, usePostBody, contentTypes);
61+
62+
if (StringUtils.isEmpty(method)) {
63+
throw new RuntimeException("@ApiRequestMapping -> method is not null");
64+
}
65+
if (StringUtils.isEmpty(path)) {
66+
throw new RuntimeException("@ApiRequestMapping -> path is not null");
67+
}
68+
}
69+
70+
public void setConfig(ApiClient apiClient, RetryStrategy retryStrategy) {
71+
this.apiClient = apiClient;
72+
this.retryStrategy = retryStrategy;
73+
}
74+
75+
@Override
76+
public R retry(ApiResponse<R> resp, T t, ApiRequestAdvice apiRequestAdvice, String token)
77+
throws ApiException {
78+
R data = resp.getData();
79+
if (data.getCodeKey() == 0) {
80+
return data;
81+
}
82+
log.info("Ocean Engine result code:{}, message:{}", data.getCodeKey(), data.getMsg());
83+
if (retryStrategy.isTokenExpired(data.getCodeKey())) {
84+
refreshToken(t.getTokenKey());
85+
data = super.execute(t, apiRequestAdvice, token);
86+
log.info("Ocean Engine after refresh token result code:{}, message:{}", data.getCodeKey(), data.getMsg());
87+
}
88+
if (!retryStrategy.enable() || !retryStrategy.retryCondition(data.getCodeKey())) {
89+
return data;
90+
}
91+
92+
int count = 0;
93+
Integer retryCount = retryStrategy.retryCount();
94+
while (count < retryCount) {
95+
log.info("Ocean Engine AD SDK current retry time is " + (count + 1));
96+
data = super.execute(t, apiRequestAdvice);
97+
log.info("Ocean Engine retry result retryCount:{},code:{}, message:{}", (count + 1), data.getCodeKey(),
98+
data.getMsg());
99+
if (!retryStrategy.retryCondition(data.getCodeKey())) {
100+
return data;
101+
}
102+
count++;
103+
}
104+
return data;
105+
}
106+
107+
@Override
108+
public Boolean isUsePostBody() {
109+
return usePostBody;
110+
}
111+
112+
@Override
113+
public ApiClient getApiClient() {
114+
return apiClient;
115+
}
116+
117+
@Override
118+
protected void updateParamsForAuth(RequestParam param) {
119+
Map<String, String> headerParams = param.getHeaderParams();
120+
for (String authName : param.getAuthNames()) {
121+
if ("Access-Token".equals(authName) && StringUtils.isNotEmpty(param.getAccessToken())) {
122+
headerParams.put("Access-Token", param.getAccessToken());
123+
}
124+
}
125+
}
126+
127+
@Override
128+
public BaseUrl getBaseUrl() {
129+
return BASE_URL;
130+
}
131+
132+
@Override
133+
public String[] getRequestContentTypes() {
134+
return contentTypes;
135+
}
136+
137+
@Override
138+
public String[] getLocalVarAuthNames() {
139+
return AuthConstants.OCEAN_ENGINE_AUTH;
140+
}
141+
142+
@Override
143+
public String getMethod() {
144+
return method;
145+
}
146+
147+
@Override
148+
public String getVersion() {
149+
return version;
150+
}
151+
152+
@Override
153+
public String getHost() {
154+
return host;
155+
}
156+
157+
@Override
158+
public String getStringToken(String accountId) {
159+
return getApiClient().getToken(accountId);
160+
}
161+
162+
@Override
163+
public String getAccountId(T t) {
164+
return t.getTokenKey();
165+
}
166+
167+
@Override
168+
public String getLocalVarPath() {
169+
return path;
170+
}
171+
172+
private void refreshToken(String tokenKey) {
173+
log.info("Ocean Engine refresh TOKEN start·······");
174+
apiClient.refreshSingleToken(tokenKey);
175+
log.info("Ocean Engine refresh TOKEN end·······");
176+
}
177+
}
178+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.hyq0719.mktapi.oceanengine;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class OceanDefaultFields {
7+
/**
8+
* 广告组全部字段
9+
*/
10+
public final static List<String> BASE_CAMPAIGN_GET_FIEDS = Arrays.asList("id", "name", "budget", "budget_mode",
11+
"landing_type", "status", "modify_time", "status", "modify_time", "campaign_modify_time",
12+
"campaign_create_time");
13+
14+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.hyq0719.mktapi.oceanengine;
2+
3+
import com.hyq0719.mktapi.common.RetryStrategy;
4+
5+
6+
public class OceanRetryStrategy implements RetryStrategy {
7+
8+
private Integer retryCount = 10;
9+
10+
private Boolean enable = true;
11+
12+
public void setRetryCount(Integer retryCount) {
13+
if (retryCount == null) {
14+
return;
15+
}
16+
this.retryCount = retryCount;
17+
}
18+
19+
public void setEnable(Boolean enable) {
20+
if (enable == null) {
21+
return;
22+
}
23+
this.enable = enable;
24+
}
25+
26+
@Override
27+
public Integer retryCount() {
28+
return retryCount;
29+
}
30+
31+
@Override
32+
public Boolean retryCondition(Long code) {
33+
return code == 40100 || code == 50000;
34+
}
35+
36+
@Override
37+
public Boolean enable() {
38+
return enable;
39+
}
40+
41+
@Override
42+
public Boolean isTokenExpired(Long code) {
43+
return code == 40105 || code == 40102;
44+
}
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2021 Yuewen Inc. All rights reserved.
2+
3+
package com.hyq0719.mktapi.oceanengine.api;
4+
5+
import com.hyq0719.mktapi.common.ApiClient;
6+
import com.hyq0719.mktapi.common.RetryStrategy;
7+
import com.hyq0719.mktapi.oceanengine.OceanApiRequest;
8+
9+
import java.lang.reflect.Method;
10+
11+
public abstract class AbstractOceanApi {
12+
public static final String ADVERTISER_ID = "advertiser_id";
13+
public static final String FILTERING = "filtering";
14+
public static final String PAGE = "page";
15+
public static final String PAGE_SIZE = "page_size";
16+
public static final String START_DATE = "start_date";
17+
public static final String END_DATE = "end_date";
18+
public static final String GROUP_BY = "group_by";
19+
public static final String TIME_GRANULARITY = "time_granularity";
20+
21+
private final ApiClient apiClient;
22+
private final RetryStrategy retryStrategy;
23+
24+
protected AbstractOceanApi(ApiClient apiClient, RetryStrategy retryStrategy) {
25+
this.apiClient = apiClient;
26+
this.retryStrategy = retryStrategy;
27+
}
28+
29+
public OceanApiRequest init(Class classObj) {
30+
OceanApiRequest request = null;
31+
try {
32+
request = (OceanApiRequest) classObj.getDeclaredConstructors()[0].newInstance(this);
33+
Method methodSetConfig = classObj.getMethod("setConfig", ApiClient.class, RetryStrategy.class);
34+
methodSetConfig.invoke(request, apiClient, retryStrategy);
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
}
38+
return request;
39+
}
40+
}

0 commit comments

Comments
 (0)