Recuperar anexos e metadados de anexo
A API NowAttachmentService permite que você execute operações CRUD em anexos e recupere metadados de anexo da sua instância ServiceNow.
- Carregar anexos para sua instância ServiceNow e associá-los a um registro específico.
- Baixe um ou mais anexos.
- Excluir anexos.
- Valide um anexo comparando o hash calculado do anexo com o hash esperado.
- Baixar metadados do anexo. Esses metadados são gerados pela sua instância ServiceNow quando um anexo é carregado.
Para obter informações adicionais sobre como trabalhar com anexos, consulte API de anexos.
/**
* 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 }
}
}Veja a seguir como obter um objeto Call que busca metadados para anexos específicos. Ele cria um objeto executável que é capaz de executar a solicitação especificada e retornar a resposta<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")
}
}Veja a seguir como obter um objeto Call que busca metadados para vários anexos. Ele cria um objeto executável que é capaz de executar a solicitação especificada e retornar a Lista Response<<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")
}
}Paginação de metadados de anexo
Você pode usar os métodos NowAttachmentService para baixar metadados de anexo para um ou mais anexos. Ao baixar metadados de vários anexos, convém usar o método NowAttachmentService - attachmentMetadataPaginator (filtro: filtro? = nulo, limite: Int? = nulo), que retorna um objeto Paginator que permite iterar facilmente a quantidade potencialmente grande de dados retornados.
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()
}
}- first()
- hasNext()
- temAnterior
- estáOcupado
- último ()
- avançar()
- observar ()
- anterior ()
- redefinir ()