rubyでCognito→DynamoDBの流れを確認したので、次はJavaFX+KotlinでGUIアプリをテスト作成。
Xamarinとかいろいろあるけど、やっぱりクロスプラットフォームなGUIといえばJavaですよね!
なんだけどやっぱりKotlinなんて出てるしそっちで書いてみる。
JavaFXとKotlinで書く単純なGUIならサンプル転がってるので、いろいろと参照しながら。
class CognitoPanel : Application() {
override fun start(primaryStage: Stage?) {
val pane = GridPane()
val message = Text().apply {
text = "hello world! input text below."
fill = Color.DARKGREEN
font = Font.font("serif", FontWeight.EXTRA_BOLD, 20.0)
}
val text_input = TextField().apply {
promptText = "input here."
}
val send_btn = Button().apply {
text = "send"
}
send_btn.onAction = EventHandler {
message.text = text_input.text + " だってさ。"
}
pane.add(message, 0, 0)
pane.add(text_input, 0, 1)
pane.add(send_btn, 0, 2)
val scene = Scene(pane, 320.0, 320.0)
primaryStage?.scene = scene
primaryStage?.show()
}
}
fun main(args: Array<String>) {
// val list = listOf("args: ", *args)
// println(list)
Application.launch(CognitoPanel::class.java, *args)
}
コードのテキストとかは置いといて、これでラベル、テキストインプット、ボタンが並んだパネルが出て来る。
(Kotlinの細かい文法とかは割愛)
最終的にボタンを押すと、テキストの内容をDynamoにputするように作っていきます。
Cognito+Dynamoの流れは前のRubyを参照にしながら…
(一回得意な言語で作れば流れ同じだし、関数名とか同じなので別の言語で書くのも割りと簡単だったりします)
fun initCognito() {
var client = AWSCognitoIdentityProviderClientBuilder.standard()
.withRegion(Regions.AP_NORTHEAST_1)
.build()
System.out.println(client)
// user login
var resp = client.initiateAuth(InitiateAuthRequest()
.withAuthFlow(AuthFlowType.USER_PASSWORD_AUTH)
.withAuthParameters(mapOf("USERNAME" to "メールアドレス", "PASSWORD" to "パスワード"))
.withClientId("アプリクライアントID")
)
// System.out.println(resp)
var at = resp.authenticationResult.idToken
System.out.println("access token:" + at)
var cognito_identity = AmazonCognitoIdentityClientBuilder
.standard().build()
var resp_id = cognito_identity.getId(GetIdRequest()
.withIdentityPoolId("IDプールID")
.withLogins(mapOf("cognito-idp.ap-northeast-1.amazonaws.com/プールID" to at))
)
cognito_id = resp_id.identityId
System.out.println("Identity ID: " + resp_id.identityId)
// get credentials
var resp_cred = cognito_identity.getCredentialsForIdentity(GetCredentialsForIdentityRequest()
.withIdentityId(resp_id.identityId)
.withLogins(mapOf("cognito-idp.ap-northeast-1.amazonaws.com/プールID" to at))
)
System.out.println("access key: " + resp_cred.credentials.accessKeyId)
System.out.println("secret key: " + resp_cred.credentials.secretKey)
System.out.println("session token: " + resp_cred.credentials.sessionToken)
var sessionCredentials = BasicSessionCredentials(
resp_cred.credentials.accessKeyId,
resp_cred.credentials.secretKey,
resp_cred.credentials.sessionToken
)
// get dynamo
dynamo_db = AmazonDynamoDBClientBuilder.standard()
.withCredentials(AWSStaticCredentialsProvider(sessionCredentials))
.build()
}
ということで流れは同じ(当たり前)なので、その通りにJavaのAWSドキュメント読みながら突き合わせて書いていきます。
Kotlinだと型推論してくれるから変数宣言が楽ですね。
もちろんIntelliJで書きました。
あとはボタンのイベントにdynamoにputするのを書くだけ。
class CognitoPanel : Application() {
private var dynamo_db: AmazonDynamoDB? = null
private var cognito_id: String = ""
override fun start(primaryStage: Stage?) {
initCognito()
System.out.println(dynamo_db)
val pane = GridPane()
val message = Text().apply {
text = "hello world! input text below."
fill = Color.DARKGREEN
font = Font.font("serif", FontWeight.EXTRA_BOLD, 20.0)
}
val text_input = TextField().apply {
promptText = "input here."
}
val send_btn = Button().apply {
text = "send"
}
send_btn.onAction = EventHandler {
message.text = text_input.text + " だってさ。"
System.out.println(dynamo_db?.putItem("テーブル", mapOf("email" to AttributeValue(cognito_id), "text" to AttributeValue(text_input.text))))
}
pane.add(message, 0, 0)
pane.add(text_input, 0, 1)
pane.add(send_btn, 0, 2)
val scene = Scene(pane, 320.0, 320.0)
primaryStage?.scene = scene
primaryStage?.show()
}
fun initCognito() {
....
}
}
で、ボタンを押したらちゃんとデータが変わってるかAWSコンソールから確認。
何もエラーでなくて変わってたらOK
一回わかりやすい言語で書くと後で楽ですね。
まず慣れが肝心かも。