apigility-powered apis on ibm i
TRANSCRIPT
Chuk Shirley
• Developing web applications since 2007
• PHP since 2011
• Winner of the 2015 COMMON Innovation Award for Sabel Steel Service’s mobile web application
@chukShirley
Today’s topics• API concepts
• Apigility installation
• Server configuration
• Creating RESTful web services
• Sanitizing input
• Using the toolkit
• Error handling
• Testing
API Terminology
• Examples:
• IBM i OS commands (CRTLIB, DSPJOBLOG)
• jQuery methods (jQuery.ajax(), jQuery.on())
• RPG built-in functions (%DEC, %XLATE)
“An API is a way for two computer applications to talk to each other over a network using a common language that they both understand.”
Jacobson, Daniel and Greg Brail. Sebastopol, APIs: A Strategy Guide Sebastopol, CA: O’Reilly Media, Inc., 2012
• API = “Application Programming Interface”
API Terminology• API vs Web API
• A Web API is an API accessed over HTTP
• Web service vs Web API
• A web service refers to one or many related API functions
• A web API is a collection of web services
Application Layers
PRESENTATION DOMAIN DATA SOURCE
Web (HTML, CSS, JS) PHP Db2
Mobile (Objective C, Cordova) CL, RPG, COBOL, Java Sessions
Toaster app SQL Cache
Web API
Layer Communication
Presentation layer
HTTP Request
HTTP Response
Domain layer
Data source layer
DB Interactions
Web API
Requirements
• Code Editor (Zend Studio, RDi, Notepad, etc.)
• Zend Server 6 or above running PHP 5.4.8 or above
• PHP Toolkit and XMLSERVICE Library
• REST Client (optional)
• Postman, cURL, etc.
Development Methods• Local
• Apigility admin UI runs locally
• Edit your code locally
• Push changes to server
• Remote
• Apigility admin UI runs on server
• Edit your code locally (pull files from server first)
• Push changes to server
PHP 5.6 and OpenSSL• PHP 5.6 now verifies peer certificates by default
• Download “good known” CA bundle
• Change php.ini setting
Verify OpenSSL Version• Only recent versions of OpenSSL can properly use this
CA bundle.
• Make sure you’re running at least 0.9.8
Too old!
Run Apigility Installer
• Rename the directory to AcmeAPI
• Return to PASE and change directories to /www
• Run the Apigility installer script
Disable OPCache• Enable opcache blacklist in php.ini
• Create file opcache_blacklist.txt and add two entries
Disable APC Cache• Change PHP setting in datacache.ini
• Set zend_datacache.apc_compatibility to “0”
Add Apache Virtual Host• In /www/zendsvr6/conf/httpd.conf
• Template is on next slide
• You’ll need to supply three values:
• Server IP address (10.x.x.x, 192.x.x.x)
• Port number **Choose one that isn’t in use**
• Path to the project
• Restart Zend Server
Listen *:[port] NameVirtualHost *:[port] <VirtualHost *:[port]> ServerName [server ip]:[port] DocumentRoot "/www/AcmeAPI/public" ! SetEnv APPLICATION_ENV "development" AllowEncodedSlashes On ! RewriteEngine on <Location /> RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] ! RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$ RewriteRule ^(.*) - [E=BASE:%1] RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L] </Location> </VirtualHost>
Initializing the DB2 TableCREATE TABLE MY_LIB.CUSTOMERS ( ID BIGINT GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1, NO ORDER, NO CYCLE, NO MINVALUE, NO MAXVALUE, CACHE 20), NAME VARCHAR (50), ADDRESS VARCHAR (75));
Designing the URIs
HTTP REQUEST METHOD EXAMPLE URI TASK
GET /customer lists all rowsPOST /customer Creates new row in dbGET /customer/{customer id} fetch a particular rowPUT /customer/{customer id} Update row in db
DELETE /customer/{customer id} Remove row from db
ZF2 Services
• The toolkit should be used as a service
• Services are configured in the Service Manager
• PHP array
• Assembled at runtime from many locations
• Global application services located at /config/autoload/application.config.php
Using the Toolkit• Edit CustomerResource::__construct() to require a
toolkit object and set the value of a class property
Error handling• Use HTTP response status codes for errors
• Apigility supports API Problem format
• Uses application/problem+json mediatype
Sending the API Problem• You can produce an API Problem with one of two
methods:
• return new \ZF\ApiProblem\ApiProblemResponse()
• throw new Exception()
Additional Resources
• This presentation’s GitHub repo: github.com/chukShirley/rpg-php-rest-services-apigility
• Apigility documentation: www.apigility.org/documentation
• HTTP status codes cheat sheet: http://httpstatus.es/