public bool MoveNext() { if (buffer.Count == 0) { if (nextUrlToLoad == null) { return(false); } var request = SetupConnection(nextUrlToLoad, MethodType.Get, apiToken, _apiVersion); if (_apiVersion.UsesOauth()) { request.Headers["Authorization"] = "Bearer " + apiToken; } else { request.Headers["PRIVATE-TOKEN"] = apiToken; } using (var response = request.GetResponse()) { // <http://localhost:1080/api/v3/projects?page=2&per_page=0>; rel="next", <http://localhost:1080/api/v3/projects?page=1&per_page=0>; rel="first", <http://localhost:1080/api/v3/projects?page=2&per_page=0>; rel="last" var link = response.Headers["Link"]; string[] nextLink = null; if (string.IsNullOrEmpty(link) == false) { nextLink = link.Split(',') .Select(l => l.Split(';')) .FirstOrDefault(pair => pair[1].Contains("next")); } if (nextLink != null) { nextUrlToLoad = new Uri(nextLink[0].Trim('<', '>', ' ')); } else { nextUrlToLoad = null; } var stream = response.GetResponseStream(); var data = new StreamReader(stream).ReadToEnd(); buffer.AddRange(JsonConvert.DeserializeObject <T[]>(data)); } return(buffer.Count > 0); } if (buffer.Count > 0) { buffer.RemoveAt(0); return(buffer.Count > 0 ? true : MoveNext()); } return(false); }
public static GitLabClient Connect(string hostUrl, string username, string password, ApiVersion apiVersion) { var api = new Api(hostUrl, ""); api._ApiVersion = apiVersion; string PrivateToken = null; if (apiVersion.UsesOauth()) { //https://docs.gitlab.com/ee/api/oauth2.html#resource-owner-password-credentials var token = api.Post().With(new oauth() { UserName = username, Password = password, GrantType = "password" }).To <token>(oauth.Url); PrivateToken = token.AccessToken; } else { var session = api.Post().To <Session>($"/session?{(apiVersion== ApiVersion.V3_1?"email":"login")}={HttpUtility.UrlEncode(username)}&password={HttpUtility.UrlEncode(password)}"); PrivateToken = session.PrivateToken; } return(Connect(hostUrl, PrivateToken, apiVersion)); }
static WebRequest SetupConnection(Uri url, MethodType methodType, string privateToken, ApiVersion apiVersion) { var request = (HttpWebRequest)WebRequest.Create(url); request.Method = methodType.ToString().ToUpperInvariant(); request.Headers.Add("Accept-Encoding", "gzip"); request.AutomaticDecompression = DecompressionMethods.GZip; if (apiVersion.UsesOauth()) { request.Headers["Authorization"] = "Bearer " + privateToken; } else { request.Headers["PRIVATE-TOKEN"] = privateToken; } #if DEBUG request.Proxy.Credentials = CredentialCache.DefaultCredentials; ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; #endif return(request); }