-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeginExecution.php
171 lines (141 loc) · 4.04 KB
/
BeginExecution.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
include_once 'Api_Version1.php';
include_once 'Api_Version2.php';
include_once 'Response.php';
/**
* Author: Ackim Williams
*
* Call this script using php BeginExecution.php inputfile version#
*
* Requirements: register_argc_argv is enabled
*
*/
class HiringApiClient
{
//webservice root
const webserviceRoot = "http://website.connect.net";
//supported commands
const COMMAND_GET = "get";
const COMMAND_SET = "set";
const COMMAND_DELETE = "delete";
const COMMAND_LIST = "list";
const COMMAND_AUTH = "auth";
private $_inputFile = "";
private $_apiVersion = 2; //set the default api version
/**
* Class constructor
*
* @param string $inputFile
*/
public function __construct( $inputFile, $apiVersion )
{
$this->_inputFile = $inputFile;
$this->_apiVersion = $apiVersion;
}
/**
* Process file
*/
public function processFile()
{
$file = @fopen( $this->_inputFile, "r" );
$hndApi = null; //initialize http client
if ( $this->_apiVersion == 1 )
$hndApi = new Api_Version1();
else if ( $this->_apiVersion == 2 )
$hndApi = new Api_Version2();
//read entire file to completion
while ( !feof( $file ) )
{
//get a line from file
$currentLine = trim( strtolower( fgets( $file ) ) );
//split across consecutive whitespace characters
$lineParts = preg_split( "/\s+/", $currentLine );
$response = null; //initialize webservice response
try
{
switch( $lineParts[0] )
{
case self::COMMAND_GET:
$response = $hndApi->get( $lineParts[1] );
$this->displayOutput( self::COMMAND_GET, $response, $lineParts[1] );
break;
case self::COMMAND_SET:
$response = $hndApi->put( $lineParts[1], $lineParts[2] );
$this->displayOutput( self::COMMAND_SET, $response );
break;
case self::COMMAND_DELETE:
$response = $hndApi->delete( $lineParts[1] );
$this->displayOutput( self::COMMAND_DELETE, $response );
break;
case self::COMMAND_LIST:
$response = $hndApi->getList();
$this->displayOutput( self::COMMAND_LIST, $response );
break;
case self::COMMAND_AUTH:
$response = $hndApi->authenticate( $lineParts[1], $lineParts[2] );
$this->displayOutput( self::COMMAND_AUTH, $response );
break;
default:
//something bad occurred, flag the record, send to record correction
}
}
catch( Exception $ex )
{
//something bad occurred, flag the record, send to record correction
}
}
fclose( $file );
echo "\n\n\n----- complete -----\n\n";
}
/**
* Display output to stdout
*
* @param string $command
* @param Response $response
* @param string $key
*/
private function displayOutput( $command, Response $response, $key = "" )
{
if ( is_null( $response ) ) //should throw an error here
return;
//catch empty content
//@todo: consider handling this as an error
if ( is_null( $response->getContent() ) )
return;
if ( $response->getHttpStatusCode() == 200 )
{
switch( $command )
{
case self::COMMAND_LIST:
foreach ( $response->getContent()->keys as $singleKey )
echo "\n" . $singleKey;
break;
case self::COMMAND_GET:
//GET call can return an empty key with API V1
if ( isset( $response->getContent()->$key ) )
echo "\n" . $response->getContent()->$key;
else
echo "\n";
break;
case self::COMMAND_DELETE:
case self::COMMAND_SET:
case self::COMMAND_AUTH:
default:
echo "\n" . $response->getStatus();
}
}
else
{
if ( isset( $response->getContent()->msg ) )
{
echo "\nerror " . $response->getHttpStatusCode() . " " . $response->getContent()->msg;
}
else
{
//@todo: consider throwing an error
}
}
}
}
$hndBeginExecution = new BeginExecution( $argv[1], $argv[2] );
$hndBeginExecution->processFile(); //begin program