添付ファイルと添付ファイルメタデータを取得
NowAttachmentService API を使用すると、添付ファイルに対して CRUD 操作を実行し、ServiceNow インスタンスから添付ファイルのメタデータを取得できます。
注:
NowAttachment API は、ベース ServiceNow システムのゲストユーザーに対して機能します。
この API を使用すると、次のことができます。
- 添付ファイルを ServiceNow インスタンスにアップロードし、特定のレコードに関連付けます。
- 1 つ以上の添付ファイルをダウンロードします。
- 添付ファイルを削除します。
- 添付ファイルの計算されたハッシュを予想されるハッシュと比較して、添付ファイルを検証します。
- 添付ファイルのメタデータをダウンロードします。このメタデータは、添付ファイルがアップロードされたときに ServiceNow インスタンスによって生成されます。
添付ファイルの操作の詳細については、「 添付ファイル API」を参照してください。
NowAttachmentService をインスタンス化する方法を次に示します。
/**
* Helper class used to handle different Now service instances.
* It has an application scope or is a Singleton.
*/
@Singleton
class SdkManager @Inject constructor() {
private var nowAttachmentService: NowAttachmentService? = null
/**
* Create the NowAttachmentService once in the lifetime of the application
* inside the Application class or another manager class
* that will be injected into other classes via dagger/hilt.
* NowAttachmentService should be created after initializing the NowSDK
*/
suspend fun getNowAttachmentService(): NowAttachmentService? {
if (nowAttachmentService != null) return nowAttachmentService
return NowDataSDK.makeAttachmentService(URL("https://instance-name.service-now.com"))
.getOrThrow()
.also { this.nowAttachmentService = it }
}
}次に、特定の添付ファイルのメタデータを取得する Call オブジェクトを取得する方法を示します。指定された要求を実行し、 Response<NowAttachmentMetadata>を返すことができる実行可能オブジェクトを作成します。
suspend fun fetchMetadata(sysId: String) {
val response = runCatching {
sdkManager.getNowAttachmentService()?.attachmentMetadata(sysId)?.execute()
}
if (response.isSuccess) {
val nowAttachmentMetadata = response.getOrNull()?.body
} else {
Log.e("NowSDK", "result not successful")
}
}複数の添付ファイルのメタデータを取得する Callオブジェクトを取得する 方法は次のとおりです。指定された要求を実行し、 Response< List<NowAttachmentMetadata>>を返すことができる実行可能オブジェクトを作成します。
suspend fun fetchMultipleMetadata() {
val filterQuery = "content_type=text/plain"
val limit = 5
val response = runCatching {
sdkManager.getNowAttachmentService()?.attachmentMetadata(Filter(filterQuery), limit)?.execute()
}
if (response.isSuccess) {
val attachmentMetadataList = response.getOrNull()?.body
} else {
Log.e("NowSDK", "result not successful")
}
}添付ファイルメタデータのページネーション
NowAttachmentService メソッドを使用して、1 つ以上の添付ファイルの添付ファイルメタデータをダウンロードできます。複数の添付ファイルからメタデータをダウンロードする場合は、 NowAttachmentService - attachmentMetadataPaginator(filter: Filter? = null, limit: Int? = null) メソッドを使用して、返される可能性のある大量のデータを簡単に反復処理できる Paginator オブジェクトを返すことができます。
その後、Paginatorメソッドを使用して返されたメタデータを反復処理し、 observe() コールバックで応答を受け取ることができます。
suspend fun createAttachmentMetadataPaginator() {
val filterQuery: String = "content_type=text/plain"
val filter = filterQuery.let(::Filter)
val limit = 10
val paginator = sdkmanager.getNowAttachmentService()?.attachmentMetadataPaginator(filter, limit)
?.observe(object : PaginatorCallBack<NowAttachmentMetadata> {
override fun onSuccess(response: Response<List<NowAttachmentMetadata>>) {
// Handle response
}
override fun onFailure(e: NowDataError) {
// Handle error
}
})
?: throw Exception("Response is null")
// Use paginator operators to navigate. Example
while (paginator.hasNext() && !paginator.isBusy()) {
paginator.next()
}
}返された Paginator オブジェクトは、返されたレコードをページングできるようにする次のメソッドを提供します。Paginatorのコールバックを設定するには、まず observe() 関数を呼び出す必要があります。
- first()
- hasNext()
- hasPrevious
- はビジー
- last()
- next()
- オブザーブ()
- previous()
- reset()
注:
一部の Paginator メソッドは、フェッチするページがなくなった場合など、例外をスローする場合があります。