Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #4420] Add Feishu Sink connector #4522

Merged
merged 11 commits into from
Nov 19, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,14 @@ public class Constants {

public static final String DEFAULT = "default";

public static final String FEISHU_SEND_MESSAGE_API = "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=";

public static final String FEISHU_RECEIVE_ID = "receive_id";

public static final String FEISHU_MSG_TYPE = "msg_type";

public static final String FEISHU_CONTENT = "content";

public static final String FEISHU_UUID = "uuid";

}
33 changes: 33 additions & 0 deletions eventmesh-connectors/eventmesh-connector-feishu/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

List feishu = [
"com.larksuite.oapi:oapi-sdk:$feishu_version",
"com.github.rholder:guava-retrying:$guava_retrying_version",
"org.apache.httpcomponents:httpclient",
project(":eventmesh-common")
]

dependencies {
api project(":eventmesh-openconnect:eventmesh-openconnect-java")
implementation feishu
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

testImplementation "org.mockito:mockito-core"
testImplementation "org.mockito:mockito-junit-jupiter"
}
18 changes: 18 additions & 0 deletions eventmesh-connectors/eventmesh-connector-feishu/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
feishu_version=2.0.28
guava_retrying_version=2.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.eventmesh.connector.feishu.server;

import org.apache.eventmesh.connector.feishu.sink.connector.FeishuSinkConnector;
import org.apache.eventmesh.openconnect.Application;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FeishuConnectServer {

Check warning on line 26 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/server/FeishuConnectServer.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/server/FeishuConnectServer.java#L25-L26

Added lines #L25 - L26 were not covered by tests

public static void main(String[] args) throws Exception {

Application feishuSinkApp = new Application();
feishuSinkApp.run(FeishuSinkConnector.class);
}

Check warning on line 32 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/server/FeishuConnectServer.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/server/FeishuConnectServer.java#L30-L32

Added lines #L30 - L32 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.eventmesh.connector.feishu.sink.config;

import org.apache.eventmesh.openconnect.api.config.SinkConfig;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class FeishuSinkConfig extends SinkConfig {

public SinkConnectorConfig connectorConfig;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.eventmesh.connector.feishu.sink.config;

import lombok.Data;

@Data
public class SinkConnectorConfig {

private String connectorName;

Check warning on line 25 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/config/SinkConnectorConfig.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/config/SinkConnectorConfig.java#L25

Added line #L25 was not covered by tests

private String receiveId;

private String appId;

Check warning on line 29 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/config/SinkConnectorConfig.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/config/SinkConnectorConfig.java#L29

Added line #L29 was not covered by tests

private String appSecret;

Check warning on line 31 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/config/SinkConnectorConfig.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/config/SinkConnectorConfig.java#L31

Added line #L31 was not covered by tests

private String receiveIdType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.eventmesh.connector.feishu.sink.connector;

import static org.apache.eventmesh.common.Constants.FEISHU_CONTENT;
import static org.apache.eventmesh.common.Constants.FEISHU_MSG_TYPE;
import static org.apache.eventmesh.common.Constants.FEISHU_RECEIVE_ID;
import static org.apache.eventmesh.common.Constants.FEISHU_SEND_MESSAGE_API;
import static org.apache.eventmesh.common.Constants.FEISHU_UUID;

import org.apache.eventmesh.connector.feishu.sink.config.FeishuSinkConfig;
import org.apache.eventmesh.connector.feishu.sink.config.SinkConnectorConfig;
import org.apache.eventmesh.openconnect.api.config.Config;
import org.apache.eventmesh.openconnect.api.connector.ConnectorContext;
import org.apache.eventmesh.openconnect.api.connector.SinkConnectorContext;
import org.apache.eventmesh.openconnect.api.sink.Sink;
import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import com.github.rholder.retry.Attempt;
import com.github.rholder.retry.RetryListener;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import com.lark.oapi.Client;
import com.lark.oapi.core.response.RawResponse;
import com.lark.oapi.core.token.AccessTokenType;
import com.lark.oapi.service.im.v1.enums.MsgTypeEnum;
import com.lark.oapi.service.im.v1.model.ext.MessageText;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FeishuSinkConnector implements Sink {

private FeishuSinkConfig sinkConfig;

private Client feishuClient;

private static final int MAX_RETRY_TIME = 3;

private static final int FIXED_WAIT_SECOND = 1;

private final Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder().retryIfException().retryIfResult(res -> !res)
.withWaitStrategy(WaitStrategies.fixedWait(FIXED_WAIT_SECOND, TimeUnit.SECONDS))
.withStopStrategy(StopStrategies.stopAfterAttempt(MAX_RETRY_TIME)).withRetryListener(new RetryListener() {

@Override
public <V> void onRetry(Attempt<V> attempt) {
long times = attempt.getAttemptNumber();
log.warn("retry invoke http,times={}", times);
}
}).build();

@Override
public Class<? extends Config> configClass() {
return FeishuSinkConfig.class;

Check warning on line 79 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java#L79

Added line #L79 was not covered by tests
}

@Override
public void init(Config config) throws Exception {
// init config for feishu sink connector
this.sinkConfig = (FeishuSinkConfig) config;
doInit();
}

Check warning on line 87 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java#L85-L87

Added lines #L85 - L87 were not covered by tests

@Override
public void init(ConnectorContext connectorContext) throws Exception {
// init config for feishu sink connector
SinkConnectorContext sinkConnectorContext = (SinkConnectorContext) connectorContext;
this.sinkConfig = (FeishuSinkConfig) sinkConnectorContext.getSinkConfig();
doInit();
}

Check warning on line 95 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java#L92-L95

Added lines #L92 - L95 were not covered by tests

private void doInit() {
this.feishuClient = Client.newBuilder(this.getConfig().getAppId(), this.getConfig().getAppSecret()).requestTimeout(3, TimeUnit.SECONDS)
.build();
}

Check warning on line 100 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java#L98-L100

Added lines #L98 - L100 were not covered by tests

@Override
public void start() {
}

@Override
public void commit(ConnectRecord record) {

}

Check warning on line 109 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java#L109

Added line #L109 was not covered by tests

@Override
public String name() {
return this.sinkConfig.getConnectorConfig().getConnectorName();

Check warning on line 113 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java#L113

Added line #L113 was not covered by tests
}

@Override
public void stop() {
}

Check warning on line 118 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java#L118

Added line #L118 was not covered by tests

@Override
public void put(List<ConnectRecord> sinkRecords) {
SinkConnectorConfig connectorConfig = getConfig();
try {
for (ConnectRecord connectRecord : sinkRecords) {
AtomicReference<RawResponse> response = new AtomicReference<>();
retryer.call(() -> {
Map<String, Object> body = new HashMap<>();
body.put(FEISHU_RECEIVE_ID, connectorConfig.getReceiveId());
body.put(FEISHU_CONTENT, MessageText.newBuilder().text(connectRecord.getData().toString()).build());
body.put(FEISHU_MSG_TYPE, MsgTypeEnum.MSG_TYPE_TEXT.getValue());
body.put(FEISHU_UUID, UUID.randomUUID().toString());
response.set(feishuClient.post(FEISHU_SEND_MESSAGE_API + connectorConfig.getReceiveIdType(), body, AccessTokenType.Tenant));
if (response.get().getStatusCode() != 200) {
log.error("request feishu open api err{}", new String(response.get().getBody()));
return false;

Check warning on line 135 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java#L134-L135

Added lines #L134 - L135 were not covered by tests
}
return true;
});
}
} catch (Exception e) {
log.error("failed to put message to feishu", e);

Check warning on line 141 in eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-feishu/src/main/java/org/apache/eventmesh/connector/feishu/sink/connector/FeishuSinkConnector.java#L140-L141

Added lines #L140 - L141 were not covered by tests
}
}

public SinkConnectorConfig getConfig() {
return this.sinkConfig.getConnectorConfig();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

pubSubConfig:
meshAddress: 127.0.0.1:10000
subject: TopicTest
idc: FT
env: PRD
group: feishuSink
appId: 5031
userName: feishuSinkUser
passWord: feishuPassWord
connectorConfig:
connectorName: feishuSink
reciveId: reciveIdValue
reciveType: open_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reciveId and recoverType fields in the sink-config.yml are different from the field names in SinkConnectorConfig, which will cause the configuration to fail to be imported correctly.

@SunnyBoy-WYH 配置文件中reciveId和reciveType字段与SinkConnectorConfig中字段名不同,这将导致配置无法正确导入。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you willing to fix this bug? @hhuang1231 .
Additionally, this bug makes me worried about the correctness and effectiveness of this Connector in the absence of this bug.

您是否愿意修改这个bug?@hhuang1231 。另外,该bug也让我有点担心,当没有该bug时,该Connector的正确性和有效性。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pandaapo ok, I'll try to perfect it and make it work.

Loading
Loading