@@ -17,13 +17,15 @@ class HttpClient
1717 */
1818 private $ projectSecret ;
1919
20- private const URL = 'http://larvabug.local/api/v1/exception ' ;
20+ //Development
21+ private const POST_EXCEPTION = 'http://dev.larvabug.com/api/v1/exception ' ;
22+ private const VALIDATE_CREDENTIALS = 'http://dev.larvabug.com/api/validate/credentials ' ;
2123
2224 /**
2325 * @param string $projectId
2426 * @param string $projectSecret
2527 */
26- public function __construct (string $ projectId , string $ projectSecret )
28+ public function __construct (string $ projectId = null , string $ projectSecret = null )
2729 {
2830 $ this ->projectId = $ projectId ;
2931 $ this ->projectSecret = $ projectSecret ;
@@ -39,24 +41,14 @@ public function report($exceptionData)
3941 try {
4042 $ data_string = json_encode ($ exceptionData );
4143
42- $ header = [
43- 'Content-Type:application/json ' ,
44- 'Authorization-APP: ' . $ this ->projectId ,
45- 'Authorization-KEY: ' . $ this ->projectSecret
46- ];
44+ $ result = $ this ->postRequest ($ data_string ,self ::POST_EXCEPTION );
4745
48- $ ch = curl_init (self ::URL );
49-
50- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data_string );
51- curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ header );
52- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
53-
54- $ result = curl_exec ($ ch );
55-
56- curl_close ($ ch );
57-
58- if ($ result && $ result != 404 ){
59- Session::put ('lb.lastExceptionId ' , $ result );
46+ if ($ result &&
47+ isset ($ result ['status ' ]) &&
48+ isset ($ result ['exceptionId ' ]) &&
49+ $ result ['status ' ] == 200
50+ ){
51+ Session::put ('lb.lastExceptionId ' , $ result ['exceptionId ' ]);
6052 }
6153
6254 return true ;
@@ -65,4 +57,37 @@ public function report($exceptionData)
6557 }
6658 }
6759
60+ public function validateCredentials (array $ credentials )
61+ {
62+ $ result = $ this ->postRequest ($ credentials ,self ::VALIDATE_CREDENTIALS );
63+
64+ if ($ result && isset ($ result ['status ' ]) && $ result ['status ' ] == 200 ){
65+ return true ;
66+ }
67+
68+ return false ;
69+
70+ }
71+
72+ private function postRequest ($ requestData , $ url )
73+ {
74+ $ header = [
75+ 'Content-Type:application/json ' ,
76+ 'Authorization-APP: ' . $ this ->projectId ,
77+ 'Authorization-KEY: ' . $ this ->projectSecret
78+ ];
79+
80+ $ ch = curl_init ($ url );
81+
82+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ requestData );
83+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ header );
84+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
85+
86+ $ result = curl_exec ($ ch );
87+
88+ curl_close ($ ch );
89+
90+ return $ result ;
91+ }
92+
6893}
0 commit comments