NowAPIService class - iOS
The NowAPIService class provides functions that enable you to perform requests to a specified ServiceNow REST API.
| Name | Type | Description |
|---|---|---|
| configuration | NowServiceConfiguration | Service configuration to associate with the protocol. |
NowAPIService - data(for endpoint: NowAPIEndpoint, queryItems: [String: String], httpHeaders: [String: String], body: Data, completion: @escaping (Result<DataResponse, NowDataError>)
Requests data from the specified ServiceNow REST API and then executes the completion handler.
| Name | Type | Description |
|---|---|---|
| for endpoint | NowAPIEndpoint | Object containing the ServiceNow REST API endpoint to access, including the HTTP method, relative path to the endpoint, path parameters, and whether authentication is required. |
| queryItems | Array | Optional. Query parameters for the endpoint as key/value pairs. Default: nil |
| httpHeaders | Array | Optional. Request HTTP headers required by the endpoint as key/value pairs. Default: nil |
| body | String | Optional. Request body parameters. Default: nil |
| completion | @escaping (Result<DataResponse, NowDataError>) | Completion handler to execute after the REST API call completes. Return
values for the completion handler:
|
| Type | Description |
|---|---|
| None |
The following code example shows how to call this function.
struct Endpoint: NowAPIEndpoint {
let httpMethod: HTTPMethod
let relativePath: String
let requiresAuthentication: Bool
}
/// HTTP request method
public enum HTTPMethod: CaseIterable {
case options, get, head, post, put, patch, delete, trace, connect
}
let relativePath = “/api/now/account”
let endPoint = Endpoint(httpMethod: HTTPMethod.get, relativePath: relativePath, requiresAuthentication: true)
apiService.data(for: endPoint, queryItems: [“name”: “abel”], httpHeaders: httpHeaders, body: body.data(using: .utf8)) { [weak self] (result) in
switch result {
case .success(let response):
// Data request successful
case .failure(let error):
// Data request failed, return NowDataError
// .failure(error)
}
}
NowAPIService - data(for endpoint: NowAPIEndpoint, queryItems: [String: String], httpHeaders: [String: String], body: Data) async throws
Requests data from the specified ServiceNow REST API.
| Name | Type | Description |
|---|---|---|
| for endpoint | NowAPIEndpoint | Object containing the ServiceNow REST API endpoint to access, including the HTTP method, relative path to the endpoint, path parameters, and whether authentication is required. |
| queryItems | Array | Optional. Query parameters for the endpoint as key/value pairs. Default: nil |
| httpHeaders | Array | Optional. Request HTTP headers required by the endpoint as key/value pairs. Default: nil |
| body | String | Optional. Request body parameters. Default: nil |
| Type | Description |
|---|---|
| DataResponse | Returned when the method is successful. Data from the REST API call. |
| NowDataError | Thrown when the method fails.
|
The following code examples shows how to call this method.
do {
let dataResponse = try await apiService.data(for: endpoint, queryItems: queryItems, httpHeaders: httpHeaders, body: body)
// Data request successful
} catch {
// Data request failed, NowDataError thrown
}
NowAPIService - data(for endpoint: NowAPIEndpoint, queryItems: [String: String]? = nil, httpHeaders: [String: String]? = nil, body: Data? = nil)
Requests data from the specified ServiceNow REST API.
| Name | Type | Description |
|---|---|---|
| for endpoint | NowAPIEndpoint | Object containing the ServiceNow REST API endpoint to access, including the HTTP method, relative path to the endpoint, path parameters, and whether authentication is required. |
| queryItems | Array | Optional. Query parameters for the endpoint as key/value pairs. Default: nil |
| httpHeaders | Array | Optional. Request HTTP headers required by the endpoint as key/value pairs. Default: nil |
| body | Data | Optional. Request body parameters. Default: nil |
| Type | Description |
|---|---|
| AnyPublisher<DataResponse, NowDataError> | Success: Data returned by the REST API. Failure: NowDataError
|
The following code example shows how to call this function.
struct Endpoint: NowAPIEndpoint {
let httpMethod: HTTPMethod
let relativePath: String
let requiresAuthentication: Bool
}
/// HTTP request method
public enum HTTPMethod: CaseIterable {
case options, get, head, post, put, patch, delete, trace, connect
}
let relativePath = "/api/now/account"
let endPoint = Endpoint(httpMethod: HTTPMethod.get, relativePath: relativePath, requiresAuthentication: true)
// Using Combine
apiService.data(for: Endpoint(httpMethod: httpMethod, relativePath: endpointPath, requiresAuthentication: requiresAuthentication), queryItems: queryItems, httpHeaders: httpHeaders, body: body.data(using: .utf8))
.receive(on: DispatchQueue.main)
.sink { [weak self] (comp) in
if case let .failure(error) = comp {
// Data request failed, return NowDataError
}
} receiveValue: { [weak self] (response) in
// Data request successful
self?.publish(data: response.data)
}
.store(in: &self.subscriptions)
}