Class: HaveAPI::ClientExamples::PhpClient
Instance Attribute Summary
#action, #action_name, #base_url, #host, #resource, #resource_path, #version
Instance Method Summary
collapse
auth, clients, code, example, init, #initialize, label, order, register, #version_url
Instance Method Details
#auth(method, desc) ⇒ Object
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
|
# File 'lib/haveapi/client_examples/php_client.rb', line 15
def auth(method, desc)
case method
when :basic
<<~END
#{init}
$api->authenticate("basic", ["user" => "user", "password" => "secret"]);
END
when :token
<<~END
#{init}
// Get token using username and password
$api->authenticate("token", [#{auth_token_credentials(desc).map { |k, v| "\"#{k}\" => \"#{v}\"" }.join(', ')}]);
echo "Token = ".$api->getAuthenticationProvider()->getToken();
// Next time, the client can authenticate using the token directly
$api->authenticate("token", ["token" => $savedToken]);
END
when :oauth2
<<~END
// OAuth2 requires session
session_start();
// Client instance
#{init}
// Check if we already have an access token
if (isset($_SESSION["access_token"])) {
// We're already authenticated, reuse the existing access token
$api->authenticate("oauth2", ["access_token" => $_SESSION["access_token"]]);
} else {
// Follow the OAuth2 authorization process to get an access token using
// authorization code
$api->authenticate("oauth2", [
// Client id and secret are given by the API server
"client_id" => "your client id",
"client_secret" => "your client secret",
// This example code should run on the URL below
"redirect_uri" => "https://your-client.tld/oauth2-callback",
// Scopes are specific to the API implementation
"scope" => "all",
]);
$provider = $api->getAuthenticationProvider();
// We don't have authorization code yet, request one
if (!isset($_GET['code'])) {
// Redirect the user to the authorization endpoint
$provider->requestAuthorizationCode();
exit;
} else {
// Request access token using the token endpoint
$provider->requestAccessToken();
// Store the access token in the session
$_SESSION['access_token'] = $provider->jsonSerialize();
}
}
END
end
end
|
#example(sample) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/haveapi/client_examples/php_client.rb', line 84
def example(sample)
args = []
args.concat(sample[:path_params]) if sample[:path_params]
if sample[:request] && !sample[:request].empty?
args << format_parameters(:input, sample[:request])
end
out = "#{init}\n"
out << "$reply = $api->#{resource_path.join('->')}->#{action_name}"
out << "(#{args.join(', ')});\n"
return (out << response(sample)) if sample[:status]
out << '// Throws exception \\HaveAPI\\Client\\Exception\\ActionFailed'
out
end
|
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/haveapi/client_examples/php_client.rb', line 149
def format_parameters(dir, params, prefix = '')
ret = params.map do |k, v|
if action[dir][:parameters][k][:type] == 'Custom'
"#{prefix} \"#{k}\" => custom type}"
else
"#{prefix} \"#{k}\" => #{value(v)}"
end
end
"#{prefix}[\n#{ret.join(",\n")}\n#{prefix}]"
end
|
#init ⇒ Object
9
10
11
12
13
|
# File 'lib/haveapi/client_examples/php_client.rb', line 9
def init
<<~END
$api = new \\HaveAPI\\Client("#{base_url}", "#{version}");
END
end
|
#response(sample) ⇒ Object
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
|
# File 'lib/haveapi/client_examples/php_client.rb', line 103
def response(sample)
out = "\n"
case action[:output][:layout]
when :hash
out << "// $reply is an instance of \\HaveAPI\\Client\\Response\n"
out << "// $reply->getResponse() returns an associative array of output parameters:\n"
out << format_parameters(:output, sample[:response] || {}, '// ')
when :hash_list
out << "// $reply is an instance of \\HaveAPI\\Client\\Response\n"
out << "// $reply->getResponse() returns an array of associative arrays:\n"
when :object
out << "// $reply is an instance of \\HaveAPI\\Client\\ResourceInstance\n"
(sample[:response] || {}).each do |pn, pv|
param = action[:output][:parameters][pn]
if param[:type] == 'Resource'
out << "// $reply->#{pn} = \\HaveAPI\\Client\\ResourceInstance("
out << "resource: #{param[:resource].join('.')}, "
out << if pv.is_a?(::Hash)
pv.map { |k, v| "#{k}: #{PP.pp(v, '').strip}" }.join(', ')
else
"id: #{pv}"
end
out << ")\n"
elsif param[:type] == 'Custom'
out << "// $reply->#{pn} is a custom type"
else
out << "// $reply->#{pn} = #{PP.pp(pv, '')}"
end
end
when :object_list
out << '// $reply is an instance of \\HaveAPI\\Client\\ResourceInstanceList'
end
out
end
|
#value(v) ⇒ Object
161
162
163
164
165
|
# File 'lib/haveapi/client_examples/php_client.rb', line 161
def value(v)
return v if v.is_a?(::Numeric) || v === true || v === false
"\"#{v}\""
end
|