-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
551 additions
and
175 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/main/java/org/embulk/output/kintone/KintoneClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.embulk.output.kintone; | ||
|
||
import com.kintone.client.KintoneClientBuilder; | ||
import com.kintone.client.RecordClient; | ||
import com.kintone.client.model.app.field.FieldProperty; | ||
import com.kintone.client.model.record.FieldType; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
import org.embulk.config.ConfigException; | ||
import org.embulk.output.kintone.util.Lazy; | ||
import org.embulk.spi.Column; | ||
import org.embulk.spi.Schema; | ||
|
||
public class KintoneClient implements AutoCloseable { | ||
private final PluginTask task; | ||
private final Schema schema; | ||
private final com.kintone.client.KintoneClient client; | ||
private final Map<String, FieldProperty> fields; | ||
|
||
public static Lazy<KintoneClient> lazy(Supplier<PluginTask> task, Schema schema) { | ||
return new Lazy<KintoneClient>() { | ||
@Override | ||
protected KintoneClient initialValue() { | ||
return new KintoneClient(task.get(), schema); | ||
} | ||
}; | ||
} | ||
|
||
private KintoneClient(PluginTask task, Schema schema) { | ||
this.task = task; | ||
this.schema = schema; | ||
KintoneClientBuilder builder = KintoneClientBuilder.create("https://" + task.getDomain()); | ||
if (task.getGuestSpaceId().isPresent()) { | ||
builder.setGuestSpaceId(task.getGuestSpaceId().get()); | ||
} | ||
if (task.getBasicAuthUsername().isPresent() && task.getBasicAuthPassword().isPresent()) { | ||
builder.withBasicAuth(task.getBasicAuthUsername().get(), task.getBasicAuthPassword().get()); | ||
} | ||
if (task.getUsername().isPresent() && task.getPassword().isPresent()) { | ||
builder.authByPassword(task.getUsername().get(), task.getPassword().get()); | ||
} else if (task.getToken().isPresent()) { | ||
builder.authByApiToken(task.getToken().get()); | ||
} else { | ||
throw new ConfigException("Username and password or token must be configured."); | ||
} | ||
client = builder.build(); | ||
fields = client.app().getFormFields(task.getAppId()); | ||
KintoneMode.of(task).validate(task, this); | ||
} | ||
|
||
public void validateUpdateKey(String columnName) { | ||
Column column = getColumn(columnName); | ||
if (column == null) { | ||
throw new ConfigException("The column '" + columnName + "' for update does not exist."); | ||
} | ||
String fieldCode = getFieldCode(column); | ||
FieldType fieldType = getFieldType(fieldCode); | ||
if (fieldType == null) { | ||
throw new ConfigException("The field '" + fieldCode + "' for update does not exist."); | ||
} | ||
if (fieldType != FieldType.SINGLE_LINE_TEXT && fieldType != FieldType.NUMBER) { | ||
throw new ConfigException("The update_key must be 'SINGLE_LINE_TEXT' or 'NUMBER'."); | ||
} | ||
} | ||
|
||
public Column getColumn(String columnName) { | ||
return schema.getColumns().stream() | ||
.filter(column -> column.getName().equals(columnName)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
public FieldType getFieldType(String fieldCode) { | ||
FieldProperty field = fields.get(fieldCode); | ||
return field == null ? null : field.getType(); | ||
} | ||
|
||
public RecordClient record() { | ||
return client.record(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
try { | ||
client.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException("kintone throw exception", e); | ||
} | ||
} | ||
|
||
private String getFieldCode(Column column) { | ||
KintoneColumnOption option = task.getColumnOptions().get(column.getName()); | ||
return option != null ? option.getFieldCode() : column.getName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.