Skip to Content

Getting Started with the SAP Cloud Platform SDK for iOS

Part 4 — Online OData

Previous (Logging)   Next (Offline OData)

OData stands for Open Data Protocol and is a common way of accessing data from an SAP or other backend using RESTful APIs. The SAP Cloud Platform SDK for iOS can be used to generate proxy classes from a backend that provides an OData interface.

The following are some additional sources of documentation on OData and generating and using proxy classes to access OData.
OData in the Getting Started with Kapsel
SAPOData Reference
Developing Online Applications
Online OData with Native iOS Mobile Interactive Tutorial

The following instructions demonstrate how to create proxy classes that represent the entity types of the sample SAP Cloud Platform Mobile Services sample OData service such as a Product or SalesOrder. These classes will then be used to return data to the application.

  1. Download the metadata document for the service to the project folder and name it metadata.xml. The URL is the OData service URL + /$metadata.
    https://hcpms-XXXXXXtrial.hanatrial.ondemand.com/SampleServices/ESPM.svc/$metadata

    Note that the XML content is not shown in Safari unless the preference Show Develop menu in menu bar is enabled under Advanced.

  2. Create a folder in the project where the proxy OData classes will be generated such as GettingStarted/GettingStarted/ProxyClasses.
    mkdir ~/Documents/iOSSDKProjects/GettingStarted/GettingStarted/ProxyClasses/
  3. Generate the proxy OData classes by executing the following command in a terminal. Note the tools can be installed via the Install Tools menu of the Assistant.
    sapcpsdk-proxygenerator -m ~/Documents/iOSSDKProjects/GettingStarted/metadata.xml -d ~/Documents/iOSSDKProjects/GettingStarted/GettingStarted/ProxyClasses
  4. Add the proxy classes to the Xcode project by dragging and dropping the generated ProxyClasses folder into the project.

  5. Add the following import to the top of ViewController.
    import SAPOData
    
  6. Add the following method which will make a request to the OData service to return products.
    // MARK: - OData queries
    private func getProducts(_ serviceRoot: URL, _ urlSession: SAPURLSession) {
    	let oDataProvider = OnlineODataProvider(serviceName: "ESPMContainer", serviceRoot: serviceRoot, sapURLSession: urlSession)
    	let espmContainer = ESPMContainer(provider: oDataProvider)
    	espmContainer.fetchProducts() { products, error in
    		guard let products = products else {
    			print("Error fetching products:  \(error!.localizedDescription)")
    			return
    		}
    		print("Got \(products.count) products and the first product name is \(products[0].name!)")
    	}
    }
    
  7. At the end of the buttonPressed method call the method.
    getProducts(serviceURL, myContext.sapURLSession)

    The result is displayed in the All Output area of Xcode.

  8. Below is a slightly different example that makes use of a query to sort the results and only bring back the product names that are in the Notebooks category. Also note that it has enabled debug output and tracing for the OData component.
    private func getProductNamesOnly(_ serviceRoot: URL, _ urlSession: SAPURLSession) {
    	let oDataProvider = OnlineODataProvider(serviceName: "ESPMContainer", serviceRoot: serviceRoot, sapURLSession: urlSession)
    	Logger.shared(named: "SAP.OData").logLevel = LogLevel.debug
    	oDataProvider.traceRequests = true
    	oDataProvider.traceWithData = true
    	oDataProvider.prettyTracing = true
    	let espmContainer = ESPMContainer(provider: oDataProvider)
    	let query = DataQuery()
    					.select(Product.name)
    					.where(Product.category.equal("Notebooks"))
    					.orderBy(Product.name)
    	espmContainer.fetchProducts(matching: query) { products, error in
    		guard let products = products else {
    			self.logger.error("Error fetching products:  \(error!.localizedDescription)")
    			return
    		}
    		print("Got \(products.count) products names only and the first product name is \(products[0].name!)")
    	}
    }
    
  9. The following is a subset of the debug/trace output from the above query.
  10. The previously generated project using the Assistant contains a more complete example that demonstrates create and update operations.

Previous (Logging)   Next (Offline OData)

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply