HttpUtils

public class HttpUtils

Functions for doing HTTP GET requests, using the default credential for server authorization.

  • Success callback, invoked with the parsed JSON body as an NSDictionary.

    Declaration

    Swift

    public typealias SuccessCallback = (_ json : NSDictionary) -> Void
  • Success callback, invoked with the parsed JSON body as an NSDictionary and headers from the server as an NSDictionary.

    Declaration

    Swift

    public typealias SuccessCallbackWithHeaders = (_ json : NSDictionary, _ headers : NSDictionary) -> Void
  • Failure callback, invoked with the error message.

    Declaration

    Swift

    public typealias FailureCallback = (_ error : String, _ statusCode : Int, _ errorCode: String) -> Void
  • Asynchronous HTTP GET, parsing the result as JSON and issuing callback on the main thread, without redirection. The caller is authenticated using the default registered credential.

    public func confirmEmail(nonce: String, onSuccess: @escaping () -> Void, onError: @escaping ((_ errorMessage: String) -> ())) {
        HttpUtils.httpGetNoRedir(urlPath: "\(self.backendUrl)email/\(nonce)", onSuccess: { _ in }, onErrorOrRedir: { location in
            NSLog("Redirecting to "+location)
        })
    }
    

    Declaration

    Swift

    public static func httpGetNoRedir(_ urlPath: String, onSuccess: @escaping SuccessCallback, onErrorOrRedir: @escaping FailureCallback)

    Parameters

    urlPath

    The absolute URL for the GET request.

    onSuccess

    SuccessCallback, invoked with parsed JSON as an NSDictionary.

    onErrorOrRedir

    FailureCallback, invoked with a String error message, or value of Location header.

  • Asynchronous HTTP GET, parsing the result as JSON and issuing callback on the main thread, with auto-redirection. The caller is authenticated using the default registered credential.

    public func login(onSuccess: @escaping () -> Void, onError: @escaping HttpUtils.FailureCallback) { HttpUtils.httpGet(urlPath: \(self.backendUrl)login, onSuccess: { _ in onSuccess() }, onError: onError) }

    Declaration

    Swift

    public static func httpGet(_ urlPath: String, additionalHeaders: [String : String]? = nil, onSuccess: @escaping SuccessCallback, onError: @escaping FailureCallback)

    Parameters

    urlPath

    The absolute URL for the GET request.

    onSuccess

    SuccessCallback, invoked with parsed JSON as an NSDictionary.

    onError

    FailureCallback, invoked with a String error message.

  • Asynchronous HTTP GET, parsing the result as JSON along with headers from the server and issuing callback on the main thread, with auto-redirection. The caller is authenticated using the default registered credential.

    public func login(@escaping onSuccess: () -> Void, onError: @escaping HttpUtils.FailureCallback) { HttpUtils.httpGet(urlPath: \(self.backendUrl)login, onSuccess: { _, headers in onSuccess() }, onError: onError) }

    Declaration

    Swift

    public static func httpGet(_ urlPath: String, additionalHeaders: [String : String]? = nil, onSuccess: @escaping SuccessCallbackWithHeaders, onError: @escaping FailureCallback)

    Parameters

    urlPath

    The absolute URL for the GET request.

    onSuccess

    SuccessCallbackWithHeaders, invoked with parsed JSON as an NSDictionary and headers from the server as an NSDictionary.

    onError

    FailureCallback, invoked with a String error message.

  • Asynchronous HTTP DELETE, parsing the result as JSON and issuing callback on the main thread, with auto-redirection. The caller is authenticated using the default registered credential.

    public func login(onSuccess: @escaping () -> Void, onError: @escaping HttpUtils.FailureCallback) { HttpUtils.httpDelete(urlPath: \(self.backendUrl)login, onSuccess: { _ in onSuccess() }, onError: onError) }

    Declaration

    Swift

    public static func httpDelete(_ urlPath: String, onSuccess: @escaping SuccessCallback, onError: @escaping FailureCallback)

    Parameters

    urlPath

    The absolute URL for the DELETE request.

    onSuccess

    SuccessCallback, invoked with parsed JSON as an NSDictionary.

    onError

    FailureCallback, invoked with a String error message.

  • Asynchronous HTTP POST, sending JSON, parsing the result as JSON, and issuing callback on the main thread, with auto-redirection. The caller is authenticated using the default registered credential.

        HttpUtils.httpPost(urlPath: "\(self.backendUrl)login", "json", onSuccess: { _ in
            //...
        }, onError: {})
    }
    

    Declaration

    Swift

    public static func httpPost(_ urlPath: String, json: Any, additionalHeaders: [String : String]? = nil, onSuccess: @escaping SuccessCallback, onError: @escaping FailureCallback)

    Parameters

    urlPath

    The absolute URL for the GET request.

    json

    The object to post as JSON.

    additionalHeaders

    The dictionary of keys and values of headers to pass onto the server.

    onSuccess

    SuccessCallback, invoked with parsed JSON as an NSDictionary.

    onError

    FailureCallback, invoked with a String error message.

  • Asynchronous HTTP POST, sending JSON, parsing the result as JSON, and issuing callback on the main thread, with auto-redirection. The caller is authenticated using the default registered credential.

    HttpUtils.httpPost(urlPath: \(self.backendUrl)login, json, onSuccess: { _ in //… }, onError: {}) }

    Declaration

    Swift

    public static func httpPost(_ urlPath: String, json: Any, additionalHeaders: [String : String]? = nil, onSuccessWithHeaders: @escaping SuccessCallbackWithHeaders, onError: @escaping FailureCallback)

    Parameters

    urlPath

    The absolute URL for the GET request.

    json

    The object to post as JSON.

    additionalHeaders

    The dictionary of keys and values of headers to pass onto the server.

    onSuccessWithHeaders

    SuccessCallbackWithHeaders, invoked with parsed JSON as an NSDictionary and headers from the server as an NSDictionary.

    onError

    FailureCallback, invoked with a String error message.

  • Undocumented

    Declaration

    Swift

    public static func httpPost(_ urlPath: String, postBodyData: Data, additionalHeaders: [String: String]? = nil,  onSuccess: @escaping SuccessCallback, onError: @escaping FailureCallback)
  • Create a URL from URLComponents, fixing the URL-encoding.

    func get(paramA: String, paramB: String) {
        let components = URLComponents(string: "\(self.backendUrl)some_endpoint")!
        components.queryItems = []
        components.queryItems!.append(URLQueryItem(name: "parama", value: paramA))
        components.queryItems!.append(URLQueryItem(name: "paramb", value: paramB))
        let url = HttpUtils.buildUrlFromComponents(components: components)
        HttpUtils.httpGet(urlPath: url.absoluteString, onSuccess: { _ in }, onError: { _ in })
    }
    

    Declaration

    Swift

    public static func buildUrlFromComponents(components: URLComponents) -> URL

    Parameters

    components

    An instance of URLComponents

    Return Value

    New instance of URL, with any query items correctly URL-encoded.

  • Find the value of a particular query string item.

    Declaration

    Swift

    public static func getQueryParameterValue(_ queryItems: [URLQueryItem]?, key: String) -> String?

    Parameters

    queryItems

    Array of URLQueryItem.

    key

    The name of the query item to retrieve.

    Return Value

    The value of the specified query item; or nil if not found.