2
2
3
3
namespace Nexmo \Account ;
4
4
5
+ use Nexmo \ApiErrorHandler ;
5
6
use Nexmo \Client \ClientAwareInterface ;
6
7
use Nexmo \Client \ClientAwareTrait ;
7
8
use Nexmo \Network ;
@@ -95,6 +96,78 @@ public function topUp($trx)
95
96
}
96
97
}
97
98
99
+ public function listSecrets ($ apiKey )
100
+ {
101
+ $ body = $ this ->get ( \Nexmo \Client::BASE_API . '/accounts/ ' .$ apiKey .'/secrets ' );
102
+ return SecretCollection::fromApi ($ body );
103
+ }
104
+
105
+ public function getSecret ($ apiKey , $ secretId )
106
+ {
107
+ $ body = $ this ->get ( \Nexmo \Client::BASE_API . '/accounts/ ' .$ apiKey .'/secrets/ ' . $ secretId );
108
+ return Secret::fromApi ($ body );
109
+ }
110
+
111
+ public function createSecret ($ apiKey , $ newSecret )
112
+ {
113
+ $ body = [
114
+ 'secret ' => $ newSecret
115
+ ];
116
+
117
+ $ request = new Request (
118
+ \Nexmo \Client::BASE_API . '/accounts/ ' .$ apiKey .'/secrets '
119
+ ,'POST '
120
+ , 'php://temp '
121
+ , ['content-type ' => 'application/json ' ]
122
+ );
123
+
124
+ $ request ->getBody ()->write (json_encode ($ body ));
125
+ $ response = $ this ->client ->send ($ request );
126
+
127
+ $ rawBody = $ response ->getBody ()->getContents ();
128
+ $ responseBody = json_decode ($ rawBody , true );
129
+ ApiErrorHandler::check ($ responseBody , $ response ->getStatusCode ());
130
+
131
+ return Secret::fromApi ($ responseBody );
132
+ }
133
+
134
+ public function deleteSecret ($ apiKey , $ secretId )
135
+ {
136
+ $ request = new Request (
137
+ \Nexmo \Client::BASE_API . '/accounts/ ' .$ apiKey .'/secrets/ ' . $ secretId
138
+ ,'DELETE '
139
+ , 'php://temp '
140
+ , ['content-type ' => 'application/json ' ]
141
+ );
142
+
143
+ $ response = $ this ->client ->send ($ request );
144
+ $ rawBody = $ response ->getBody ()->getContents ();
145
+ $ body = json_decode ($ rawBody , true );
146
+
147
+ // This will throw an exception on any error
148
+ ApiErrorHandler::check ($ body , $ response ->getStatusCode ());
149
+
150
+ // This returns a 204, so no response body
151
+ }
152
+
153
+ protected function get ($ url ) {
154
+ $ request = new Request (
155
+ $ url
156
+ ,'GET '
157
+ , 'php://temp '
158
+ , ['content-type ' => 'application/json ' ]
159
+ );
160
+
161
+ $ response = $ this ->client ->send ($ request );
162
+ $ rawBody = $ response ->getBody ()->getContents ();
163
+ $ body = json_decode ($ rawBody , true );
164
+
165
+ // This will throw an exception on any error
166
+ ApiErrorHandler::check ($ body , $ response ->getStatusCode ());
167
+
168
+ return $ body ;
169
+ }
170
+
98
171
protected function getException (ResponseInterface $ response , $ application = null )
99
172
{
100
173
$ body = json_decode ($ response ->getBody ()->getContents (), true );
@@ -111,4 +184,4 @@ protected function getException(ResponseInterface $response, $application = null
111
184
return $ e ;
112
185
}
113
186
114
- }
187
+ }
0 commit comments