55import fr .rabian .ovhApi .core .http .Header ;
66import fr .rabian .ovhApi .core .http .HttpRequests ;
77import fr .rabian .ovhApi .core .utils .HashFunctions ;
8+ import fr .rabian .ovhApi .core .utils .NOKResponseException ;
89import fr .rabian .ovhApi .core .utils .Timestamps ;
910
11+ import java .security .NoSuchAlgorithmException ;
1012import java .util .ArrayList ;
1113import java .util .List ;
1214
@@ -50,8 +52,9 @@ protected AppManager(Application app) {
5052 * @param scope Authorization scope
5153 * @param redirectURL URL to redirect to after authentication
5254 * @return Feteched Consumer
55+ * @throws NOKResponseException If the response differs from 200 - OK
5356 */
54- public Consumer getConsumer (List <ScopeElement > scope , String redirectURL ) {
57+ public Consumer getConsumer (List <ScopeElement > scope , String redirectURL ) throws NOKResponseException {
5558 GsonBuilder builder = new GsonBuilder ();
5659 Gson gson = builder .create ();
5760 List <Header > headers = new ArrayList <>();
@@ -61,16 +64,13 @@ public Consumer getConsumer(List<ScopeElement> scope, String redirectURL) {
6164
6265 RequestCredentials rc = new RequestCredentials (scope , redirectURL );
6366 String req = gson .toJson (rc );
64- int result = 0 ;
65- try {
66- result = HttpRequests .sendPost (ep .getURL () + "/auth/credential" , out , req , headers );
67- } catch (Exception e ) {
68- e .printStackTrace ();
69- }
70- Consumer c = null ;
67+ int result = HttpRequests .sendPost (ep .getURL () + "/auth/credential" , out , req , headers );
68+ Consumer c ;
7169 if (result == 200 ) {
7270 c = gson .fromJson (out .toString (), Consumer .class );
7371 c .setScope (scope );
72+ } else {
73+ throw new NOKResponseException (result );
7474 }
7575 return c ;
7676 }
@@ -81,9 +81,9 @@ public Consumer getConsumer(List<ScopeElement> scope, String redirectURL) {
8181 * @param path Path to the function
8282 * @param c Consumer concerned
8383 * @return Response body
84- * @throws Exception Occuring during the request
84+ * @throws NOKResponseException If the response differs from 200 - OK
8585 */
86- public String sendGetReq (String path , Consumer c ) throws Exception {
86+ public String sendGetReq (String path , Consumer c ) throws NOKResponseException {
8787 return sendReq (path , "GET" , c , "" );
8888 }
8989
@@ -93,9 +93,9 @@ public String sendGetReq(String path, Consumer c) throws Exception {
9393 * @param path Path to the function
9494 * @param c Consumer concerned
9595 * @return Response body
96- * @throws Exception Occuring during the request
96+ * @throws NOKResponseException If the response differs from 200 - OK
9797 */
98- public String sendDeleteReq (String path , Consumer c ) throws Exception {
98+ public String sendDeleteReq (String path , Consumer c ) throws NOKResponseException {
9999 return sendReq (path , "DELETE" , c , "" );
100100 }
101101
@@ -106,9 +106,9 @@ public String sendDeleteReq(String path, Consumer c) throws Exception {
106106 * @param c Consumer concerned
107107 * @param body Request body
108108 * @return Response body
109- * @throws Exception Occuring during the request
109+ * @throws NOKResponseException If the response differs from 200 - OK
110110 */
111- public String sendPostReq (String path , Consumer c , String body ) throws Exception {
111+ public String sendPostReq (String path , Consumer c , String body ) throws NOKResponseException {
112112 return sendReq (path , "POST" , c , body );
113113 }
114114
@@ -119,9 +119,9 @@ public String sendPostReq(String path, Consumer c, String body) throws Exception
119119 * @param c Consumer concerned
120120 * @param body Request body
121121 * @return Response body
122- * @throws Exception Occuring during the request
122+ * @throws NOKResponseException If the response differs from 200 - OK
123123 */
124- public String sendPutReq (String path , Consumer c , String body ) throws Exception {
124+ public String sendPutReq (String path , Consumer c , String body ) throws NOKResponseException {
125125 return sendReq (path , "PUT" , c , body );
126126 }
127127
@@ -133,9 +133,9 @@ public String sendPutReq(String path, Consumer c, String body) throws Exception
133133 * @param c Consumer concerned
134134 * @param body Request body
135135 * @return Response body
136- * @throws Exception Occuring during the request
136+ * @throws NOKResponseException If the response differs from 200 - OK
137137 */
138- private String sendReq (String path , String method , Consumer c , String body ) throws Exception {
138+ private String sendReq (String path , String method , Consumer c , String body ) throws NOKResponseException {
139139 List <Header > headers = new ArrayList <>();
140140 path = ep .getURL () + path ;
141141 long time = ts .getTime ();
@@ -151,15 +151,21 @@ private String sendReq(String path, String method, Consumer c, String body) thro
151151 forSig .append (body );
152152 forSig .append ("+" );
153153 forSig .append (Long .toString (time ));
154- String sig = "$1$" + HashFunctions .hashMD ("SHA-1" , forSig .toString ());
154+
155+ String sig = "$1$" ;
156+ try {
157+ sig = sig + HashFunctions .hashMD ("SHA-1" , forSig .toString ());
158+ } catch (NoSuchAlgorithmException e ) {
159+ //Silenced because always available.
160+ }
155161
156162 headers .add (new Header ("X-Ovh-Application" , app .getPubKey ()));
157163 headers .add (new Header ("X-Ovh-Consumer" , c .getConsumerKey ()));
158164 headers .add (new Header ("Accept" , "application/json" ));
159165 headers .add (new Header ("X-Ovh-Timestamp" , Long .toString (time )));
160166 headers .add (new Header ("X-Ovh-Signature" , sig ));
161167
162- int result = 0 ;
168+ int result ;
163169 StringBuffer out = new StringBuffer ();
164170
165171 switch (method ) {
@@ -179,6 +185,10 @@ private String sendReq(String path, String method, Consumer c, String body) thro
179185 throw new IllegalArgumentException ("Incorrect HTTP method." );
180186 }
181187
182- return out .toString ();
188+ if (result == 200 ) {
189+ return out .toString ();
190+ } else {
191+ throw new NOKResponseException (result );
192+ }
183193 }
184194}
0 commit comments