Objective C Using MS OData SDK - How to Guide

21
5/26/2018 ObjectiveCUsingMSODataSDK-HowtoGuide-slidepdf.com http://slidepdf.com/reader/full/objective-c-using-ms-odata-sdk-how-to-guide 1/21  Consuming SAP NetWeaver Gateway Services from Objective C Applications

description

objective c sap gateway

Transcript of Objective C Using MS OData SDK - How to Guide

  • Consuming SAP NetWeaver Gateway Services from Objective C Applications

  • Copyright/Trademark

    Copyright

    Copyright 2011 SAP AG. All rights reserved. SAP Library document classification: PUBLIC No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation. Linux is the registered trademark of Linus Torvalds in the U.S. and other countries. Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer, StreamWork, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects Software Ltd. Business Objects is an SAP company. Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, and other Sybase products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Sybase, Inc. Sybase is an SAP company. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

  • Copyright/Trademark

    Table of Contents

    Consuming SAP NetWeaver Gateway Services from Objective C Applications ....................... 4

    Prerequisites .................................................................................................................................................... 5 Building an iOS Application ........................................................................................................................... 6

    Function Import Example .......................................................................................................................... 6 Read Example ............................................................................................................................................ 9 Create Example........................................................................................................................................ 10 Creation of Request Body for Create Method ..................................................................................... 11

    Authentication and Cookies ........................................................................................................................ 12 Known issues for Microsoft OData SDK V1.3 .......................................................................................... 12

    Appendix 1 - RMTSAMPLEFLIGHT Service Metadata Description ............................................ 13

    Appendix 2 Code Snippets .......................................................................................................... 19

    Get Available Flights .................................................................................................................................... 19 Get Carrier by ID ........................................................................................................................................... 20 Create a Flight Booking ............................................................................................................................... 20

  • Copyright/Trademark

    CONSUMING SAP NETWEAVER GATEWAY SERVICES

    FROM OBJECTIVE C APPLICATIONS

    SAP NetWeaver Gateway technology provides a simple way to interact with SAP applications through variety of devices, environments and platforms based on market standards. The framework enables development of innovative, people-centric solutions that bring the power of SAP business software into new experiences, such as: social networking and collaboration environments; mobile and tablet devices; and rich internet applications. The framework supports rapid innovation while ensuring security, integrity, management and optimized maintenance of the core SAP systems. Completely flexible, the software offers connectivity to SAP applications using any programming language or model without the need for SAP knowledge by taking advantage of REST services and OData/ATOM protocols.

    Note: There is added value in using the SAP OData Mobile SDK (which comes with the usage of SUP). This added value is not included when using the code snippets.

    This guide provides information on how to consume SAP NetWeaver Gateway services from iOS applications based on the following scenario:

    The user wants to fly from New York to San Francisco.

    He goes online and searches for the different flights available. He enters his search parameters: city of origin, destination city, departure date, and arrival date (Function Import).

    From the SAP NetWeaver Gateway, he receives the information for the option(s) that meet(s) his search parameters (Get Function).

    He can now select the one that suits him the most and book his flight (Create Function).

    The links to access the service document and metadata document are provided the in table below.

    Service Document

    http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/

    Metadata Document

    http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/ $metadata?$format=xml

    Each service URL should contain the following information:

    Gateway host

    Port

    SAP Client no need to specify client if connecting to the default client.

    Example:

    http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT?sap-client=100

    To obtain information on the selected service (its properties, function imports, associations, etc.), refer to Appendix 1, were we show the Metadata description of the RMTSAMPLEFLIGHT service.

    To view the code snippets in a format that can be copied into new code, refer to Appendix 2 Code Snippets.

  • Copyright/Trademark

    The Flight service contains the following collections:

    Collection URL

    FlightCollection (list of flights)

    http://:/sap/opu/sdata/ iwfnd/RMTSAMPLEFLIGHT/FlightCollection

    CarrierCollection (list of carriers http://:/ sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection

    BookingsCollection (list of Bookings) http://:/sap/opu/sdata/ iwfnd/RMTSAMPLEFLIGHT/BookingCollection

    The operation GetAvailableFlights on FlightCollection is used to get a list of flights. The following is the URL

    for the same:

    http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?

    Each collection supports some or all of the operations listed below. The table below provides the list of

    operations supported and the methods that each of this operations map to respectively:

    Operation Method

    Create HTTP POST

    Read GET

    Update PUT

    Delete DELETE

    Prerequisites

    The following tools are required for developing an iOS application:

    Software Version

    SAP NetWeaver Gateway 2.0

    Microsoft OData SDK for Objective C 1.3

    To download go to: http://odataobjc.codeplex.com/

    Contact your system administrator for landscape details to connect to the SAP NetWeaver Gateway system.

  • Copyright/Trademark

    Make sure you have performed the following procedures:

    Add proxies for the RMTSAMPLEFLIGHT service to your iOS project as described in the OData SDK documentation.

    Add the following imports:

    #import "WindowsCredential.h"

    #import "RMTSAMPLEFLIGHT.h"

    #import "Constants.h"

    Building an iOS Application

    Function Import Example

    To get a list of the available flights based on filter criteria, trigger the GetAvailableFlights function import

    with cityFrom, cityTo, fromDate and toDate parameters. The response will contain a list of flights that meet

    the criteria.

    Note: For more information on Function Import (also called Service Operation), refer to OData

    documentation at http://www.odata.org/developers/protocols/uri-conventions under the Addressing

    Service Operations section.

    Refer to the code snippet below for details on how to do it from an iOS application:

    1. The RMTSAMPLEFLIGHT service requires basic authentication. For this, you need to initiate a WindowsCredential object containing your user name and password.

    WindowsCredential *credential = nil;

    NSString *userName = @"UserName";

    NSString *password = @"Password";

    credential = [[[WindowsCredential alloc] initWithUserName:userName password:password] autorelease];

    2. Initialize the service proxy object, for example RMTSAMPLEFLIGHT, using the service URL and credentials.

    RMTSAMPLEFLIGHT *flightServiceEntities = [[RMTSAMPLEFLIGHT alloc] initWithUri:@"http://:/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT" credential:credential];

    3. Add the XMLHttpRequest http header needed for Post requests. To work around issue# to add HTTP headers to the request implement the ODataDelegate protocol:

    -(void) onBeforeSend: (HttpRequest*)request{

    [request.m_httpHeaders.m_httpHeaders addEntriesFromDictionary:[NSDictionary dictionaryWithObject:@"XMLHttpRequest" forKey:@"X-Requested-With"]];

    }

    -(void) onAfterReceive: (HttpResponse*)response{

    }

    And then set the delegate in the proxy object:

    flightServiceEntities.m_oDataDelegate = self;

  • Copyright/Trademark

    4. Call the GetAvailableFlights method which uses the following parameters: flight departure date, arrival

    date, and cities.

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyyMMdd"];

    NSDate *dateFrom = [dateFormatter dateFromString:@"20110105"];

    NSDate *dateTo = [dateFormatter dateFromString:@"20110728"];

    [dateFormatter release];

    NSArray *flights = [flightServiceEntities GetAvailableFlightsWithfromdate:dateFrom todate:dateTo cityfrom:@"NEW YORK" cityto:@"SAN FRANCISCO"];

    5. The method returns an array of flight objects. Go over all the flights in the array and print their carrier

    ID.

    for (RMTSAMPLEFLIGHT_Flight *flight in flights) {

    NSLog(@"Carrier ID: %@", flight.m_carrid);

    }

    Response

    - http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/FlightCollection FlightCollection 2011-09-27T06:32:42Z

    - -

    - AA 0017 2011-01-05T00:00:00 889.00 USD 747-400 385 367 185742.73 31 22 21 21 - US new york JFK US SAN FRANCISCO SFO 361 PT11H00M00S PT14H01M00S 2574.0000 SMI 0

  • Copyright/Trademark

    http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/FlightCollection(carrid='AA',connid='0017',fldate='20110105')

    Flight 2011-09-27T06:32:42Z

    - - -

    AA 0017 2011-03-16T00:00:00 422.94 USD 747-400 385 371 192129.22 31 30 21 20 - US new york JFK US SAN FRANCISCO SFO 361 PT11H00M00S PT14H01M00S 2574.0000 SMI 0

    http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/FlightCollection(carrid='AA',connid='0017',

    fldate='20110316')

    Flight 2011-09-27T06:32:42Z

  • Copyright/Trademark

    Read Example

    To read the carrier details, you must call the CarrierCollection using the Carrier ID parameter.

    Refer to the code snippet below for details on how to do it from an iOS application:

    Create a Get Specific carrier query string and execute it using the service proxy object. The result

    returned in the response is an array containing the specific Carrier object.

    NSString *carrierById = [NSString stringWithFormat:@"CarrierCollection(carrid=\'%@\')",flight.m_carrid];

    QueryOperationResponse *response = [flightServiceEntities execute:carrierById];

    RMTSAMPLEFLIGHT_Carrier *carrier = [[response getResult] objectAtIndex:0];

    NSLog(@"carrier name: %@",carrier.m_CARRNAME);

    Response

    - - AA American Airlines USD http://www.aa.com :/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection(carrid='AA') Carrier 2011-09-27T10:00:38Z

  • Copyright/Trademark

    Create Example

    To book a flight, trigger an HTTP POST method on the Booking Collection (with the Booking details in the request body). Check for the HTTP response code. If the response code is 201, then the HTTP-POST is successful. The response contains the newly created Booking ID in the bookid field, as well as the entire information that was part of the request body.

    Refer to the code snippet below for details on how to invoke a create request from an iOS application:

    1. Initialize a new Booking object.

    RMTSAMPLEFLIGHT_Booking *booking = [[RMTSAMPLEFLIGHT_Booking alloc] init];

    booking.m_carrid = @"AA";

    booking.m_connid = @"0017";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyyMMdd"];

    NSDate *date = [dateFormatter dateFromString:@"20111221"];

    booking.m_fldate = date;

    booking.m_CUSTOMID = @"00004617";

    booking.m_PASSNAME = @"Joe Smith";

    booking.m_PASSFORM = @"1234567";

    booking.m_CUSTTYPE = @"P";

    booking.m_WUNIT = @"KG";

    booking.m_LUGGWEIGHT = [NSDecimalNumber decimalNumberWithString:@"14.4000"];

    booking.m_CLASS = @"Y";

    booking.m_FORCURAM = [NSDecimalNumber decimalNumberWithString:@"879.82"];

    booking.m_FORCURKEY = @"USD";

    booking.m_LOCCURAM = [NSDecimalNumber decimalNumberWithString:@"803.58"];

    booking.m_LOCCURKEY = @"USD";

    NSDate *date2 = [dateFormatter dateFromString:@"20110522"];

    booking.m_ORDER_DATE = date2;

    booking.m_AGENCYNUM = @"00000325";

    booking.m_PASSBIRTH = [dateFormatter dateFromString:@"19901010"];

    booking.m_COUNTER = @"00000000";

    [dateFormatter release];

    2. Add an XMLHttpRequest http header. Implement the ODataDelegate protocol as described in the Function Import example, then set the delegate in the proxy object:

    flightServiceEntities.m_oDataDelegate = self;

    3. Add the new Booking object to the BookingCollection and call the saveChanges method to trigger the

    request.

    [flightServiceEntities addToBookingCollection:booking];

    [flightServiceEntities setSaveChangesOptions:None];

    [flightServiceEntities saveChanges];

    4. Use the updated Booking object after the server response.

    NSLog(@"Booking ID: %@", booking.m_bookid);

    [booking release];

  • Copyright/Trademark

    Creation of Request Body for Create Method

    The Request body can be easily created by looking at the tags in the collections metadata. For example,

    the metadata for Booking Collection provides information about the various fields (tags) that are needed

    for booking:

  • Copyright/Trademark

    Authentication and Cookies

    The authentication of the OData client using the NetWeaver service is performed using the credentials

    entered in steps 1 and 2 of the Building an iOS Application procedure above.

    When the credentials are successfully authenticated, a cookie is returned from the NetWeaver Gateway

    service and saved in the service proxy object. When this service proxy object is used, it sends the cookie

    and not the user name and password. If the cookie is not valid anymore, the credentials are sent again.

    Known issues for Microsoft OData SDK V1.3

    1. When the odatagen is used to generate classes for services that have a complex type inside the

    FunctionImport section, the generated class inherited from the ObjectContext does not compile.

    Each time the complex type name appears (in the h and m files), there is a namespace prefix missing.

    For example:

    - (FlightAvailability *) CheckFlightAvailabilityWithairlineid:(NSString *)airlineid connectionid:(NSString *)connectionid flightdate:(NSDate *)flightdate;

    Should be:

    - (RMTAMPLEFLIGHT_FlightAvailability *) CheckFlightAvailabilityWithairlineid:(NSString *)airlineid connectionid:(NSString *)connectionid flightdate:(NSDate *)flightdate;

    For more details, refer to http://odataobjc.codeplex.com/workitem/11307

    2. When reading a property of type Edm.Time, the returned value is always nil regardless of the real value

    returned from the service.

    For more details, refer to http://odataobjc.codeplex.com/workitem/11311

    3. After adding a new object to a collection and performing saveChanges, the object is not updated with values from the server. For example, when creating a new booking object and performing saveChanges, the booking_id value returned from the service is not included in the booking object. For more details refer to http://odataobjc.codeplex.com/workitem/11310

    4. When reading a property of type Edm.Byte, the returned value is always 0 regardless of the real value

    returned from the service. Setting the property to 1 and performing saveChanges will cause the application to crash. For more details refer to http://odataobjc.codeplex.com/workitem/11309

    5. The addHeader:headerValue: method does not work in the objectContext class.

    For more details refer to http://odataobjc.codeplex.com/workitem/11308

  • Copyright/Trademark

    Appendix 1 - RMTSAMPLEFLIGHT Service Metadata

    Description

  • Copyright/Trademark

  • Copyright/Trademark

  • Copyright/Trademark

  • Copyright/Trademark

    Airline Flight Number Date

  • Copyright/Trademark

    Date Date Depart.city Arrival city

  • Copyright/Trademark

    APPENDIX 2 CODE SNIPPETS

    Get Available Flights

    #import "WindowsCredential.h"

    #import "RMTSAMPLEFLIGHT.h"

    #import "Constants.h"

    WindowsCredential *credential = nil;

    NSString *userName = @"UserName";

    NSString *password = @"Password";

    credential = [[[WindowsCredential alloc] initWithUserName:userName password:password] autorelease];

    RMTSAMPLEFLIGHT *flightServiceEntities = [[RMTSAMPLEFLIGHT alloc] initWithUri:@"http://host:port/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT" credential:credential];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyyMMdd"];

    NSDate *dateFrom = [dateFormatter dateFromString:@"20110105"];

    NSDate *dateTo = [dateFormatter dateFromString:@"20110728"];

    [dateFormatter release];

    flightServiceEntities.m_oDataDelegate = self;

    NSArray *flights = [flightServiceEntities GetAvailableFlightsWithfromdate:dateFrom todate:dateTo cityfrom:@"NEW YORK" cityto:@"SAN FRANCISCO"];

    for (RMTSAMPLEFLIGHT_Flight *flight in flights) {

    NSLog(@"Carrier ID: %@", flight.m_carrid);

    }

    ODataDelegate implementation

    - (void) onBeforeSend: (HttpRequest*)request{

    [request.m_httpHeaders.m_httpHeaders addEntriesFromDictionary:[NSDictionary dictionaryWithObject:@"XMLHttpRequest" forKey:@"X-Requested-With"]];

    }

    - (void) onAfterReceive: (HttpResponse*)response{

    }

  • Copyright/Trademark

    Get Carrier by ID

    #import "WindowsCredential.h"

    #import "RMTSAMPLEFLIGHT.h"

    #import "Constants.h"

    WindowsCredential *credential = nil;

    NSString *userName = @"UserName";

    NSString *password = @"Password";

    credential = [[[WindowsCredential alloc] initWithUserName:userName password:password] autorelease];

    RMTSAMPLEFLIGHT *flightServiceEntities = [[RMTSAMPLEFLIGHT alloc] initWithUri:@"http://host:port/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT" credential:credential];

    NSString *carrierById = [NSString stringWithFormat:@"CarrierCollection(carrid=\'%@\')",flight.m_carrid];

    QueryOperationResponse *response = [flightServiceEntities execute:carrierById];

    RMTSAMPLEFLIGHT_Carrier *carrier = [[response getResult] objectAtIndex:0];

    NSLog(@"carrier name: %@",carrier.m_CARRNAME);

    Create a Flight Booking

    #import "WindowsCredential.h"

    #import "RMTSAMPLEFLIGHT.h"

    #import "Constants.h"

    WindowsCredential *credential = nil;

    NSString *userName = @"UserName";

    NSString *password = @"Password";

    credential = [[[WindowsCredential alloc] initWithUserName:userName password:password] autorelease];

    RMTSAMPLEFLIGHT *flightServiceEntities = [[RMTSAMPLEFLIGHT alloc] initWithUri:@"http://host:port/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT" credential:credential];

    RMTSAMPLEFLIGHT_Booking *booking = [[RMTSAMPLEFLIGHT_Booking alloc] init];

    booking.m_carrid = @"AA";

    booking.m_connid = @"0017";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyyMMdd"];

    NSDate *date = [dateFormatter dateFromString:@"20111221"];

  • Copyright/Trademark

    booking.m_fldate = date;

    booking.m_CUSTOMID = @"00004617";

    booking.m_PASSNAME = @"Joe Smith";

    booking.m_PASSFORM = @"1234567";

    booking.m_CUSTTYPE = @"P";

    booking.m_WUNIT = @"KG";

    booking.m_LUGGWEIGHT = [NSDecimalNumber decimalNumberWithString:@"14.4000"];

    booking.m_CLASS = @"Y";

    booking.m_FORCURAM = [NSDecimalNumber decimalNumberWithString:@"879.82"];

    booking.m_FORCURKEY = @"USD";

    booking.m_LOCCURAM = [NSDecimalNumber decimalNumberWithString:@"803.58"];

    booking.m_LOCCURKEY = @"USD";

    NSDate *date2 = [dateFormatter dateFromString:@"20110522"];

    booking.m_ORDER_DATE = date2;

    booking.m_AGENCYNUM = @"00000325";

    booking.m_PASSBIRTH = [dateFormatter dateFromString:@"19901010"];

    booking.m_COUNTER = @"00000000";

    flightServiceEntities.m_oDataDelegate = self;

    [flightServiceEntities addToBookingCollection:booking];

    [flightServiceEntities setSaveChangesOptions:None];

    [flightServiceEntities saveChanges];

    [booking release];

    [dateFormatter release];

    ODataDelegate implementation

    - (void) onBeforeSend: (HttpRequest*)request{

    [request.m_httpHeaders.m_httpHeaders addEntriesFromDictionary:[NSDictionary dictionaryWithObject:@"XMLHttpRequest" forKey:@"X-Requested-With"]];

    }

    - (void) onAfterReceive: (HttpResponse*)response{

    }