1414use OCP \DB \Exception ;
1515use OCP \Http \Client \IClient ;
1616use OCP \Http \Client \IClientService ;
17+ use OCP \Http \Client \IPromise ;
1718use OCP \Http \Client \IResponse ;
1819use OCP \IConfig ;
1920use OCP \IRequest ;
@@ -96,29 +97,39 @@ private function requestToExAppInternal(
9697 array $ options ,
9798 ): array |IResponse {
9899 try {
99- switch ($ method ) {
100- case 'GET ' :
101- $ response = $ this ->client ->get ($ uri , $ options );
102- break ;
103- case 'POST ' :
104- $ response = $ this ->client ->post ($ uri , $ options );
105- break ;
106- case 'PUT ' :
107- $ response = $ this ->client ->put ($ uri , $ options );
108- break ;
109- case 'DELETE ' :
110- $ response = $ this ->client ->delete ($ uri , $ options );
111- break ;
112- default :
113- return ['error ' => 'Bad HTTP method ' ];
114- }
115- return $ response ;
100+ return match ($ method ) {
101+ 'GET ' => $ this ->client ->get ($ uri , $ options ),
102+ 'POST ' => $ this ->client ->post ($ uri , $ options ),
103+ 'PUT ' => $ this ->client ->put ($ uri , $ options ),
104+ 'DELETE ' => $ this ->client ->delete ($ uri , $ options ),
105+ default => ['error ' => 'Bad HTTP method ' ],
106+ };
116107 } catch (\Exception $ e ) {
117- $ this ->logger ->error (sprintf ('Error during request to ExApp %s: %s ' , $ exApp ->getAppid (), $ e ->getMessage ()), ['exception ' => $ e ]);
108+ $ this ->logger ->warning (sprintf ('Error during request to ExApp %s: %s ' , $ exApp ->getAppid (), $ e ->getMessage ()), ['exception ' => $ e ]);
118109 return ['error ' => $ e ->getMessage ()];
119110 }
120111 }
121112
113+ /**
114+ * @throws \Exception
115+ */
116+ public function aeRequestToExAppAsync (
117+ ExApp $ exApp ,
118+ string $ route ,
119+ ?string $ userId = null ,
120+ string $ method = 'POST ' ,
121+ array $ params = [],
122+ array $ options = [],
123+ ?IRequest $ request = null ,
124+ ): IPromise {
125+ $ this ->exAppUsersService ->setupExAppUser ($ exApp ->getAppid (), $ userId );
126+ $ requestData = $ this ->prepareRequestToExApp ($ exApp , $ route , $ userId , $ method , $ params , $ options , $ request );
127+ return $ this ->requestToExAppInternalAsync ($ exApp , $ method , $ requestData ['url ' ], $ requestData ['options ' ]);
128+ }
129+
130+ /**
131+ * @throws \Exception
132+ */
122133 public function requestToExAppAsync (
123134 ExApp $ exApp ,
124135 string $ route ,
@@ -127,35 +138,32 @@ public function requestToExAppAsync(
127138 array $ params = [],
128139 array $ options = [],
129140 ?IRequest $ request = null ,
130- ): void {
141+ ): IPromise {
131142 $ requestData = $ this ->prepareRequestToExApp ($ exApp , $ route , $ userId , $ method , $ params , $ options , $ request );
132- $ this ->requestToExAppInternalAsync ($ exApp , $ method , $ requestData ['url ' ], $ requestData ['options ' ]);
143+ return $ this ->requestToExAppInternalAsync ($ exApp , $ method , $ requestData ['url ' ], $ requestData ['options ' ]);
133144 }
134145
146+ /**
147+ * @throws \Exception if bad HTTP method
148+ */
135149 private function requestToExAppInternalAsync (
136150 ExApp $ exApp ,
137151 string $ method ,
138152 string $ uri ,
139153 #[\SensitiveParameter]
140154 array $ options ,
141- ): void {
142- switch ($ method ) {
143- case 'POST ' :
144- $ promise = $ this ->client ->postAsync ($ uri , $ options );
145- break ;
146- case 'PUT ' :
147- $ promise = $ this ->client ->putAsync ($ uri , $ options );
148- break ;
149- case 'DELETE ' :
150- $ promise = $ this ->client ->deleteAsync ($ uri , $ options );
151- break ;
152- default :
153- $ this ->logger ->error ('Bad HTTP method: requestToExAppAsync accepts only `POST`, `PUT` and `DELETE` ' );
154- return ;
155- }
156- $ promise ->then (function (IResponse $ response ) use ($ exApp ) {
157- }, function (\Exception $ exception ) use ($ exApp ) {
155+ ): IPromise {
156+ $ promise = match ($ method ) {
157+ 'GET ' => $ this ->client ->getAsync ($ uri , $ options ),
158+ 'POST ' => $ this ->client ->postAsync ($ uri , $ options ),
159+ 'PUT ' => $ this ->client ->putAsync ($ uri , $ options ),
160+ 'DELETE ' => $ this ->client ->deleteAsync ($ uri , $ options ),
161+ default => throw new \Exception ('Bad HTTP method ' ),
162+ };
163+ $ promise ->then (onRejected: function (\Exception $ exception ) use ($ exApp ) {
164+ $ this ->logger ->warning (sprintf ('Error during requestToExAppAsync %s: %s ' , $ exApp ->getAppid (), $ exception ->getMessage ()), ['exception ' => $ exception ]);
158165 });
166+ return $ promise ;
159167 }
160168
161169 private function prepareRequestToExApp (
0 commit comments