NowAPIService interface - Android

  • 릴리스 버전: Australia
  • 업데이트 날짜 2026년 03월 12일
  • 소요 시간: 3분
  • The NowAPIService interface provides the ability to perform requests on a specified ServiceNow REST API.

    주:
    Guest users only work for the NowAPIService interface if the REST API being accessed is configured for guest users.
    표 1. Properties
    Name Type Description
    configuration NowServiceConfiguration Service configuration to associate with the protocol.

    NowAPIService - data(endpoint: NowAPIService.Endpoint, queryParams: QueryParams, headers: Headers, body: String)

    Calls the specified REST API on the specified ServiceNow instance.

    표 2. Parameters
    Name Type Description
    endpoint NowAPIService.Endpoint REST endpoint to call.
    queryParams QueryParams Optional. Query parameter object that contains the query parameters to pass in the REST call.

    Default: null

    headers Headers Optional. Headers object that contains the headers to pass in the REST call.

    Default: null

    body String Optional. Object that contains the request body and content type to pass into the REST call.

    Default: null

    표 3. Returns
    Type Description
    Call<ByteArray> Return results from the called REST endpoint.

    suspend fun loadCases(): List<CaseItem> = withContext(Dispatchers.IO) {
      val apiService = nowServiceManager.getNowAPIService()
    
      val result = runCatching {
        val response = apiService.data(NowAPIService.Endpoint(
          relativePath = CASES_API,
          requestMethod = HttpMethod.GET,
          requireAuth = true)
        ).execute()
    
        val byteArray = response.body ?: throw Exception("Null Result")
        val buffer = Buffer()
        val reader = JsonReader.of(buffer.write(byteArray))
        val listType = Types.newParameterizedType(
          List::class.java,
          CaseItem::class.java
        )
    
        val resultType = Types.newParameterizedType(
          ResultResponse::class.java,
          listType
        )
        val adapter = moshi.adapter<ResultResponse<List<CaseItem>>>(resultType)
        adapter.fromJson(reader)
      }
    
      if (result.isSuccess) {
          result.getOrNull()?.result ?: throw Exception("Cases not in response")
      } else {
          throw result.exceptionOrNull() ?: Exception("Error Cases")
      }
    }