public static bool TryParse(string value, out LinkHeaderValue result) { if (string.IsNullOrEmpty(value)) { result = null; return false; } var match = Regex.Match(value, "^\\s*<(?<url>[^>]*)>\\s*;\\s*rel\\s*=\\s*\"(?<rel>[^\"]*)\"\\s*$"); if (match.Success) { var url = match.Groups["url"].Value; var rel = match.Groups["rel"].Value; result = new LinkHeaderValue(url, rel); return true; } result = null; return false; }
public async Task <GitLabPageResponse <T> > ToGitLabPageAsync <T>(CancellationToken cancellationToken) where T : GitLabObject { string?firstLink = null; string?lastLink = null; string?prevLink = null; string?nextLink = null; var headers = ResponseHeaders; if (headers.TryGetValues("link", out var values)) { foreach (var value in values) { foreach (var linkValue in value.Split(',')) { if (LinkHeaderValue.TryParse(linkValue, out var headerValue)) { if (string.Equals(headerValue.Rel, "first", StringComparison.OrdinalIgnoreCase)) { firstLink = headerValue.Url; } else if (string.Equals(headerValue.Rel, "last", StringComparison.OrdinalIgnoreCase)) { lastLink = headerValue.Url; } else if (string.Equals(headerValue.Rel, "next", StringComparison.OrdinalIgnoreCase)) { nextLink = headerValue.Url; } else if (string.Equals(headerValue.Rel, "prev", StringComparison.OrdinalIgnoreCase)) { prevLink = headerValue.Url; } } } } } else { #if DEBUG throw new GitLabException(RequestMethod, RequestUri, StatusCode, "The response does not contain the pagination headers."); #endif } var pageIndex = headers.GetHeaderValue("X-Page", -1); var pageSize = headers.GetHeaderValue("X-Per-Page", -1); var total = headers.GetHeaderValue("X-Total", -1); var totalPages = headers.GetHeaderValue("X-Total-Pages", -1); var data = await ToCollectionAsync <T>(cancellationToken).ConfigureAwait(false); if (data is null) { throw new GitLabException(RequestMethod, RequestUri, StatusCode, $"The response cannot be converted to '{typeof(T)}' collection because the body is null or empty"); } return(new GitLabPageResponse <T>( client: _client, data: data, pageIndex: pageIndex, pageSize: pageSize, totalItems: total, totalPages: totalPages, firstUrl: firstLink, previousUrl: prevLink, nextUrl: nextLink, lastUrl: lastLink)); }