99namespace Joomla \Github ;
1010
1111use Joomla \Http \Exception \UnexpectedResponseException ;
12- use Joomla \Http \Http as BaseHttp ;
12+ use Joomla \Http \Http ;
1313use Joomla \Http \Response ;
1414use Joomla \Registry \Registry ;
1515use Joomla \Uri \Uri ;
@@ -82,15 +82,27 @@ abstract class AbstractGithubObject
8282 * Constructor.
8383 *
8484 * @param Registry $options GitHub options object.
85- * @param BaseHttp $client The HTTP client object.
85+ * @param Http $client The HTTP client object.
8686 *
8787 * @since 1.0
8888 */
89- public function __construct (Registry $ options = null , BaseHttp $ client = null )
89+ public function __construct (Registry $ options = null , Http $ client = null )
9090 {
9191 $ this ->options = $ options ?: new Registry ;
9292 $ this ->client = $ client ?: new Http ($ this ->options );
9393
94+ // Make sure the user agent string is defined.
95+ if (!isset ($ this ->options ['userAgent ' ]))
96+ {
97+ $ this ->options ['userAgent ' ] = 'JGitHub/2.0 ' ;
98+ }
99+
100+ // Set the default timeout to 120 seconds.
101+ if (!isset ($ this ->options ['timeout ' ]))
102+ {
103+ $ this ->options ['timeout ' ] = 120 ;
104+ }
105+
94106 $ this ->package = \get_class ($ this );
95107 $ this ->package = substr ($ this ->package , strrpos ($ this ->package , '\\' ) + 1 );
96108 }
@@ -114,7 +126,18 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
114126 // Get a new Uri object focusing the api url and given path.
115127 $ uri = new Uri ($ this ->options ->get ('api.url ' ) . $ path );
116128
117- if (!$ this ->options ->get ('gh.token ' , false ))
129+ if ($ this ->options ->get ('gh.token ' , false ))
130+ {
131+ // Use oAuth authentication
132+ $ headers = $ this ->client ->getOption ('headers ' , array ());
133+
134+ if (!isset ($ headers ['Authorization ' ]))
135+ {
136+ $ headers ['Authorization ' ] = 'token ' . $ this ->options ->get ('gh.token ' );
137+ $ this ->client ->setOption ('headers ' , $ headers );
138+ }
139+ }
140+ else
118141 {
119142 // Use basic authentication
120143 if ($ this ->options ->get ('api.username ' , false ))
@@ -143,27 +166,6 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
143166 return (string ) $ uri ;
144167 }
145168
146- /**
147- * Returns the Authorization header, if required.
148- *
149- * If the options passed to the constructor contain a value for `gh.token`,
150- * an array with a suitable Authorization header is returned, an empty array
151- * otherwise.
152- *
153- * @return array Authorization header if set in options.
154- *
155- * @since 1.8.0
156- */
157- protected function authHeader ()
158- {
159- if ($ this ->options ->get ('gh.token ' , false ))
160- {
161- return array ('Authorization: ' . $ this ->options ->get ('gh.token ' ));
162- }
163-
164- return array ();
165- }
166-
167169 /**
168170 * Process the response and decode it.
169171 *
@@ -172,8 +174,8 @@ protected function authHeader()
172174 *
173175 * @return mixed
174176 *
175- * @throws UnexpectedResponseException
176177 * @since 1.0
178+ * @throws UnexpectedResponseException
177179 */
178180 protected function processResponse (Response $ response , $ expectedCode = 200 )
179181 {
0 commit comments