GTRest

open class GTRest : NSObject, GTAssistiveTools

A lightweight class to perform web requests.

See documentation.

  • It manages request HTTP headers.

    Declaration

    Swift

    public var requestHttpHeaders: GTRest.RestEntity
  • It manages URL query parameters.

    Declaration

    Swift

    public var urlQueryParameters: GTRest.RestEntity
  • It manages HTTP body parameters. Use it when content type is either application/json or application/x-www-form-urlencoded. There is no need to provide a value for the httpBody property.

    Declaration

    Swift

    public var httpBodyParameters: GTRest.RestEntity
  • The HTTP body data that should be sent along with a request. Use this property if content type is other than application/json and application/x-www-form-urlencoded.

    Declaration

    Swift

    public var httpBody: Data?
  • GTRest default initializer.

    Declaration

    Swift

    public override init()
  • It initiates a web request to the given URL using the specified HTTP method.

    Any data that should be sent along with the request should be provided prior to calling this method. Use the following properties:

    • requestHttpHeaders: To provide request HTTP headers.
    • urlQueryParameters: To provide URL query parameters.
    • httpBodyParameters: To provide HTTP body parameters if the content type is either application/json or application/x-www-form-urlencoded.
    • httpBody: To provide the HTTP body data if the content type is other than application/json or application/x-www-form-urlencoded.

    The web request takes place asynchronously. On completion, the method returns a Results object which contains the data and response coming from server, or any potential error.

    Usage example:

    let url = URL(...) // A URL object.
    let rest = GTRest()
    rest.makeRequest(toURL: url, httpMethod: .get) { [unowned self] (results) in // or [weak self] (results) in
        // Access data:
        if let data = results.data {
           // Perform app-specific actions
        }
    
        // Access the response:
        if let response = data.response {
           // Do something with the response object if necessary.
           // Remember that it's a GTRest.Response object.
           // Checking the HTTP status code :
           if (200...299).contains(response.httpStatusCode) {
               // Successful request.
           } else { ... }
        }
    
        // Access the error:
        if let error = results.error {
           // Do something with the error.
        }
    
        // Update your UI on main thread always:
        DispatchQueue.main.async {
           // Update UI.
        }
    }
    

    Declaration

    Swift

    public func makeRequest(toURL url: URL,
                            withHttpMethod httpMethod: HttpMethod,
                            completion: @escaping (_ result: Results) -> Void)

    Parameters

    url

    The URL to make the request to.

    httpMethod

    The request HTTP method (get, post, etc).

    completion

    The completion handler called when server has responded with data. It is also called if the request object (URLRequest) cannot be created for some reason.

    result

    The GTRest.Results object containing the results of the web request.

  • It uploads the given files to the given URL, including additional data as URL query parameters or HTTP body parameters.

    A file is described by a GTRest.FileInfo object, and the collection of files to upload is a collection of GTRest.FileInfo objects.

    On completion, a GTRest.Results object is returned which includes the results of the request. In addition, an optional array of String values is also included in the completion which, if not nil, contains the names of files that could not be uploaded for some reason.

    Important

    multipart/form-data; boundary=xxx is the content type used for web requests made through this method. Do not specify a content type in request HTTP headers, it is automatically set.

    Usage example:

    var rest = GTRest()
    
    // Prepare the files that will be uploaded.
    var resume = GTRest.FileInfo()
    resume.fileContents = ... // File contents data
    resume.mimetype = "application/pdf" // or GTRest.MimeType.applicationPDF.rawValue
    resume.filename = "resume.pdf"
    
    var avatar = GTRest.FileInfo()
    avatar.fileContents = ... // File contents data
    avatar.mimetype = GTRest.MimeType.imagePNG.rawValue
    avatar.filename = "avatar.png"
    
    // Optionally, set any request HTTP methods, but not the "content-type".
    // Also, set any required URL query or HTTP body parameters.
    
    // Prepare the URL.
    let url = ... // A URL object.
    
    rest.upload(files: [resume, avatar], toURL: url, withHttpMethod: .post) { [unowned self] (results, failedFiles) in // or [unowned self] (results, failedFiles) in
       if let failedFiles = failedFiles {
           // Do something with the files that failed to be uploaded.
       }
    
       // Do something with the results.
       if let data = results.data {
           // ...
       }
    
       // Update your UI on main thread always:
       DispatchQueue.main.async {
           // Update UI.
       }
    }
    

    Declaration

    Swift

    public func upload(files: [FileInfo], toURL url: URL, withHttpMethod httpMethod: HttpMethod, completion: @escaping(_ result: Results, _ failedFiles: [String]?) -> Void)

    Parameters

    files

    A collection of GTRest.FileInfo objects with data regarding the files to upload.

    url

    The URL to make the request to.

    httpMethod

    The request HTTP method (get, post, etc).

    completion

    The completion handler called when server has responded with data. It is also called if the request object (URLRequest) cannot be created for some reason, or the boundary cannot be generated.

    result

    The GTRest.Result object containing the results of the web request

    failedFiles

    An optional array of String values containing the names of files failed to be uploaded.

  • It fetches data from the given URL.

    This method is useful for fetching single data from a URL, usually the contents of a file. For example, the image data for a user profile picture, or the contents of a PDF file.

    Note

    Data fetching takes place asynchronously.

    Usage example:

    let rest = GTRest()
    rest.getData(fromURL: url)  { [unowned self] (data) in // or [weak self] (data) in
       if let data = data {
           // Do something with the fetched data.
       }
    }
    

    Declaration

    Swift

    public func getData(fromURL url: URL, completion: @escaping (_ data: Data?) -> Void)

    Parameters

    url

    The URL to fetch data from.

    completion

    The completion handler that gets called after having fetched the data. It returns a Data object.

    data

    The Data object with the fetched data from the given URL. It can be nil.

  • The available HTTP methods.

    See more

    Declaration

    Swift

    public enum HttpMethod : String
  • It represents a REST entity.

    A REST entity can be any of the following:

    • Request HTTP headers
    • Response HTTP headers
    • URL query parameters
    • HTTP body parameters
    See more

    Declaration

    Swift

    public struct RestEntity : GTKeyValueCompliant, CustomStringConvertible
  • It keeps data related to the server response.

    See more

    Declaration

    Swift

    public struct Response
  • It contains results related to a web request.

    See more

    Declaration

    Swift

    public struct Results
  • A custom type to keep data about a file that should be uploaded.

    See more

    Declaration

    Swift

    public struct FileInfo
  • It represents custom errors in GTRest class.

    See more

    Declaration

    Swift

    public enum CustomError : Error