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
|
# File 'lib/haveapi/client_examples/php_client.rb', line 15
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/haveapi/client_examples/php_client.rb', line 39
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
|
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/haveapi/client_examples/php_client.rb', line 104
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
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
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
|
# File 'lib/haveapi/client_examples/php_client.rb', line 58
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
119
120
121
122
|
# File 'lib/haveapi/client_examples/php_client.rb', line 119
def value(v)
return v if v.is_a?(::Numeric) || v === true || v === false
"\"#{v}\""
end
|