Skip to content

Commit

Permalink
Migrate extract api to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkRunWu committed Feb 2, 2024
1 parent 78eacae commit c19a6f4
Show file tree
Hide file tree
Showing 24 changed files with 762 additions and 349 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ android {
}

dependencies {
implementation("com.github.oursky:formx-sdk:0.2.4")
implementation("com.github.oursky:formx-sdk:0.3.0")
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.mockito:mockito-core:5.0.0'
implementation 'androidx.camera:camera-core:1.3.0-alpha07'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ class FormxSdkFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
"init" -> {
val formId = call.argument<String>("formId")
val extractorId = call.argument<String>("extractorId")
val accessToken = call.argument<String>("accessToken")
val endpoint = call.argument<String>("endpoint")
if (formId != null && accessToken != null) {
val apiHost = call.argument<String>("apiHost")
if (extractorId != null && accessToken != null) {
try {
FormXSDKInitializer.init(formId, accessToken, endpoint)
FormXSDKInitializer.init(extractorId, accessToken, apiHost)
} catch (e: Exception) {
result.error(runtimeError(e), e)
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package ai.formx.mobile.sdk.formx_sdk_flutter

import ai.formx.mobile.sdk.ExtractDocument
import ai.formx.mobile.sdk.ExtractDocumentError
import ai.formx.mobile.sdk.ExtractDocumentMetaData
import ai.formx.mobile.sdk.ExtractDocumentResult
import ai.formx.mobile.sdk.ExtractMetaData
import ai.formx.mobile.sdk.FormXAPIDetectDocumentsResponse
import ai.formx.mobile.sdk.FormXAPIExtractResponse
import ai.formx.mobile.sdk.FormXAutoExtractionIntItem
import ai.formx.mobile.sdk.FormXAutoExtractionItem
import ai.formx.mobile.sdk.FormXAutoExtractionItemType
import ai.formx.mobile.sdk.FormXAutoExtractionPurchaseInfoItem
import ai.formx.mobile.sdk.FormXAutoExtractionStringItem
import ai.formx.mobile.sdk.FormXDocumentRegion
import ai.formx.mobile.sdk.FormXPointF
import ai.formx.mobile.sdk.FormXDataBoolListValue
import ai.formx.mobile.sdk.FormXDataBoolValue
import ai.formx.mobile.sdk.FormXDataMapListValue
import ai.formx.mobile.sdk.FormXDataMapValue
import ai.formx.mobile.sdk.FormXDataNumListValue
import ai.formx.mobile.sdk.FormXDataNumValue
import ai.formx.mobile.sdk.FormXDataProductListValue
import ai.formx.mobile.sdk.FormXDataStringListValue
import ai.formx.mobile.sdk.FormXDataStringValue
import ai.formx.mobile.sdk.FormXDataUnknownValue
import ai.formx.mobile.sdk.FormXDataValue
import ai.formx.mobile.sdk.FormXProductValue

fun FormXAPIDetectDocumentsResponse.toMap(): HashMap<String, Any> =
HashMap<String, Any>().apply {
Expand All @@ -30,53 +40,121 @@ fun FormXAPIDetectDocumentsResponse.toMap(): HashMap<String, Any> =

fun FormXAPIExtractResponse.toMap(): HashMap<String, Any> =
HashMap<String, Any>().apply {
put("formId", formId)
put("status", status)
put("autoExtractionItems",
autoExtractionItems.map { extractionItem ->
extractionItem.toMap()
})
put("metaData", metaData.toMap())
put("documents", documents.map {
it.toMap()
})
}

fun FormXAutoExtractionItem.toMap(): HashMap<String, Any> =
fun ExtractMetaData.toMap(): HashMap<String, Any> =
HashMap<String, Any>().apply {
put("type", type.name)
when (type) {
FormXAutoExtractionItemType.IntValueType -> (value as FormXAutoExtractionIntItem).let {
put("name", it.name)
put("value", it.value)
}
put("extractorId", extractorId)
put("requestId", requestId)
put("usage", usage)
jobId?.let {
put("jobId", it)
}
}

fun ExtractDocument.toMap(): HashMap<String, Any> =
HashMap<String, Any>().apply {
put("type", "ExtractDocument")
put("extractorId", extractorId)
put("metaData", metaData.toMap())
type?.let {

FormXAutoExtractionItemType.StringValueType -> (value as FormXAutoExtractionStringItem).let {
put("name", it.name)
put(
"value",
it.value
)
put("documentType", it)
}
typeConfidence?.let {
put("typeConfidence", it)
}
put("data", data.mapValues {
it.value?.toMap()
})
put("detailedData", detailedData.mapValues {
it.value.map { v ->
v.toMap()
}
})
boundingBox?.let {
put("boundingBox", it)
}
}

FormXAutoExtractionItemType.PurhcaseInfoValueType ->
(value as FormXAutoExtractionPurchaseInfoItem).let { purchaseItem ->
put("name", purchaseItem.name)
put("value", purchaseItem.value.map { infoItem ->
HashMap<String, String>().apply {
put("name", infoItem.name ?: "")
put("amount", Integer.parseInt(infoItem.amount ?: "0"))
put("discount", (infoItem.discount ?: "0").toDouble())
put("sku", infoItem.sku ?: "")
put("quantity", Integer.parseInt(infoItem.quantity ?: "0"))
put("unitPrice", (infoItem.unitPrice ?: "0").toDouble())
}
})
fun ExtractDocumentMetaData.toMap(): HashMap<String, Any> =
HashMap<String, Any>().apply {
extractorType?.let {
put("extractorType", it)
}
put("pageNo", pageNo)
put("sliceNo", sliceNo)
}

fun FormXProductValue.toMap(): HashMap<String, Any?> =
HashMap<String, Any?>().apply {
put("name", name)
put("amount", amount ?: 0.0)
put("discount", discount)
put("sku", sku)
put("quantity", quantity ?: 0)
put("unitPrice", unitPrice ?: 0.0)
}

fun FormXDataValue.toMap(): HashMap<String, Any?> =
HashMap<String, Any?>().also { it ->
it["extractedBy"] = extractedBy
it["confidence"] = confidence
it["valueType"] = valueType
it["value"] = HashMap<String, Any?>().also { valueMap ->
valueMap["type"] = this.javaClass.simpleName
when (this) {
is FormXDataBoolListValue -> valueMap["value"] = value
is FormXDataBoolValue -> valueMap["value"] = value
is FormXDataMapListValue -> valueMap["value"] = value
is FormXDataMapValue -> valueMap["value"] = value
is FormXDataNumListValue -> valueMap["value"] = value.map { it.toDouble() }
is FormXDataNumValue -> {
valueMap["value"] = value.toDouble()
}
is FormXDataProductListValue -> valueMap["value"] = value.map { productValue ->
productValue.toMap()
}

FormXAutoExtractionItemType.UnsupportedValueType -> put("value", value.toString())
is FormXDataStringListValue -> valueMap["value"] = value
is FormXDataStringValue -> valueMap["value"] = value
is FormXDataUnknownValue -> valueMap["value"] = value
}
}
}


fun Error.toMap(): HashMap<String, Any> =
HashMap<String, Any>().apply {
put("code", code.name)
put("message", message ?: "")
}
}

fun ai.formx.mobile.sdk.Error.toMap(): HashMap<String, Any> =
HashMap<String, Any>().apply {
put("code", code)
put("message", message)
info?.let {
put("info", it)
}
}

fun ExtractDocumentError.toMap(): HashMap<String, Any> =
HashMap<String, Any>().apply {
put("type", "ExtractDocumentError")
put("metaData", metaData.toMap())
extractorId?.let {
put("extractorId", it)
}
put("error", error.toMap())
}

fun ExtractDocumentResult.toMap(): HashMap<String, Any> =
when (this) {
is ExtractDocument -> this.toMap()
is ExtractDocumentError -> this.toMap()
}
2 changes: 1 addition & 1 deletion example/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FORMX_FORM_ID=
FORMX_EXTRACTOR_ID=
FORMX_ACCESS_TOKEN=
FORMX_API_HOST=

6 changes: 3 additions & 3 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ First, you will need an Access Token and the Form ID of a Pre-Built Receipt Extr
- Click 'Create new extractor' button of 'Extractors' page on FormX Portal.
- Select 'Receipts' in the list of built-in extractors.
- Enter a name for the extractor and create it.
- Open 'Extract' tab in your extractor detail page to obtain the `Form ID`.
- Open 'Extract' tab in your extractor detail page to obtain the `Extractor ID`.

Created `.env` in example project then configure the FormX SDK using the `form id` and `access token`:
Created `.env` in example project then configure the FormX SDK using the `extractor id` and `access token`:

```bash
# example/.env

FORMX_FORM_ID=<FORM ID>
FORMX_EXTRACTOR_ID=<EXTRACTOR ID>
FORMX_ACCESS_TOKEN=<ACCESS TOKEN>
```
## Installation
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ target 'Runner' do
use_frameworks!
use_modular_headers!

pod 'FormX', :git => 'https://github.com/oursky/formx-sdk.git', tag: '0.2.4'
pod 'FormX', :git => 'https://github.com/oursky/formx-sdk.git', tag: '0.3.0'

flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
target 'RunnerTests' do
Expand Down
12 changes: 6 additions & 6 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- Flutter (1.0.0)
- FormX (0.2.4)
- FormX (0.3.0)
- formx_sdk_flutter (1.0.0):
- Flutter
- FormX
Expand All @@ -13,7 +13,7 @@ PODS:

DEPENDENCIES:
- Flutter (from `Flutter`)
- FormX (from `https://github.com/oursky/formx-sdk.git`, tag `0.2.4`)
- FormX (from `https://github.com/oursky/formx-sdk.git`, tag `0.3.0`)
- formx_sdk_flutter (from `.symlinks/plugins/formx_sdk_flutter/ios`)
- image_picker_ios (from `.symlinks/plugins/image_picker_ios/ios`)
- integration_test (from `.symlinks/plugins/integration_test/ios`)
Expand All @@ -24,7 +24,7 @@ EXTERNAL SOURCES:
:path: Flutter
FormX:
:git: https://github.com/oursky/formx-sdk.git
:tag: 0.2.4
:tag: 0.3.0
formx_sdk_flutter:
:path: ".symlinks/plugins/formx_sdk_flutter/ios"
image_picker_ios:
Expand All @@ -37,16 +37,16 @@ EXTERNAL SOURCES:
CHECKOUT OPTIONS:
FormX:
:git: https://github.com/oursky/formx-sdk.git
:tag: 0.2.4
:tag: 0.3.0

SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
FormX: f1ab685d62b6b31b50ba9dc84dd39b5d3fba516c
FormX: 246b88710995d37218707e12cdd9a57dd45a3c21
formx_sdk_flutter: af2d460c3fb218b0e544371432aab283f8f9e776
image_picker_ios: 4a8aadfbb6dc30ad5141a2ce3832af9214a705b5
integration_test: 13825b8a9334a850581300559b8839134b124670
permission_handler_apple: e76247795d700c14ea09e3a2d8855d41ee80a2e6

PODFILE CHECKSUM: e79061acc18288f01a77c523181bacd0ce09c33a
PODFILE CHECKSUM: 1c4fd636587ac2f3da3bccffe0013d9496db8848

COCOAPODS: 1.13.0
Loading

0 comments on commit c19a6f4

Please sign in to comment.