HttpUtils

public class HttpUtils

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

  • Prevent outgoing network traffic

    Declaration

    Swift

    public static func lockOffline()
  • Success callback, invoked with the parsed JSON body as an NSDictionary.

    Declaration

    Swift

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

    Declaration

    Swift

    public typealias FailureCallback = (_ error : String, _ statusCode : Int) -> 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, 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 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, onSuccess: @escaping SuccessCallback, onError: @escaping FailureCallback)

    Parameters

    urlPath

    The absolute URL for the GET request.

    json

    The object to post as JSON.

    onSuccess

    SuccessCallback, invoked with parsed JSON as an NSDictionary.

    onError

    FailureCallback, invoked with a String error message.

  • 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.