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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/haveapi/client_examples/php_client.rb', line 13
def auth(method, desc)
case method
when :basic
<<END
#{init}
$api->authenticate("basic", ["username" => "user", "password" => "secret"]);
END
when :token
<<END
#{init}
// Get token using username and password
$api->authenticate("token", ["username" => "user", "password" => "secret"]);
echo "Token = ".$api->getAuthenticationProvider()->getToken();
// Next time, the client can authenticate using the token directly
$api->authenticate("token", ["token" => $savedToken]);
END
end
end
|
#example(sample) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/haveapi/client_examples/php_client.rb', line 37
def example(sample)
args = []
args.concat(sample[:url_params]) if sample[:url_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
|
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/haveapi/client_examples/php_client.rb', line 102
def format_parameters(dir, params, prefix = '')
ret = []
params.each do |k, v|
if action[dir][:parameters][k][:type] == 'Custom'
ret << "#{prefix} \"#{k}\" => custom type}"
else
ret << "#{prefix} \"#{k}\" => #{value(v)}"
end
end
"#{prefix}[\n#{ret.join(",\n")}\n#{prefix}]"
end
|
#init ⇒ Object
7
8
9
10
11
|
# File 'lib/haveapi/client_examples/php_client.rb', line 7
def init
<<END
$api = new \\HaveAPI\\Client("#{base_url}", "#{version}");
END
end
|
#response(sample) ⇒ Object
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
|
# File 'lib/haveapi/client_examples/php_client.rb', line 56
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 |k, v|
param = action[:output][:parameters][k]
if param[:type] == 'Resource'
out << "// $reply->#{k} = \\HaveAPI\\Client\\ResourceInstance("
out << "resource: #{param[:resource].join('.')}, "
if v.is_a?(::Hash)
out << v.map { |k,v| "#{k}: #{PP.pp(v, '').strip}" }.join(', ')
else
out << "id: #{v}"
end
out << ")\n"
elsif param[:type] == 'Custom'
out << "// $reply->#{k} is a custom type"
else
out << "// $reply->#{k} = #{PP.pp(v, '')}"
end
end
when :object_list
out << "// $reply is an instance of \\HaveAPI\\Client\\ResourceInstanceList"
end
out
end
|
#value(v) ⇒ Object
117
118
119
120
|
# File 'lib/haveapi/client_examples/php_client.rb', line 117
def value(v)
return v if v.is_a?(::Numeric) || v === true || v === false
"\"#{v}\""
end
|