60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
|
package pkg_manager
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type GitHubRepo struct {
|
||
|
FullName string `json:"full_name"`
|
||
|
Description string `json:"description"`
|
||
|
HTMLURL string `json:"html_url"`
|
||
|
Stars int `json:"stargazers_count"`
|
||
|
Language string `json:"language"`
|
||
|
}
|
||
|
|
||
|
type GitHubSearchResponse struct {
|
||
|
TotalCount int `json:"total_count"`
|
||
|
Items []GitHubRepo `json:"items"`
|
||
|
}
|
||
|
|
||
|
func SearchGitHubSource(query string) ([]GitHubRepo, error) {
|
||
|
baseURL := "https://api.github.com/search/repositories"
|
||
|
|
||
|
// Create search query
|
||
|
params := url.Values{}
|
||
|
params.Add("q", query)
|
||
|
params.Add("sort", "stars")
|
||
|
params.Add("order", "desc")
|
||
|
params.Add("per_page", "5") // Limit to top 5 results
|
||
|
|
||
|
client := &http.Client{Timeout: 10 * time.Second}
|
||
|
|
||
|
req, err := http.NewRequest("GET", baseURL+"?"+params.Encode(), nil)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("error creating request: %w", err)
|
||
|
}
|
||
|
|
||
|
req.Header.Set("Accept", "application/vnd.github.v3+json")
|
||
|
req.Header.Set("User-Agent", "UPM-Package-Manager")
|
||
|
|
||
|
resp, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("error making request: %w", err)
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
if resp.StatusCode != http.StatusOK {
|
||
|
return nil, fmt.Errorf("GitHub API returned status: %d", resp.StatusCode)
|
||
|
}
|
||
|
|
||
|
var searchResp GitHubSearchResponse
|
||
|
if err := json.NewDecoder(resp.Body).Decode(&searchResp); err != nil {
|
||
|
return nil, fmt.Errorf("error decoding response: %w", err)
|
||
|
}
|
||
|
|
||
|
return searchResp.Items, nil
|
||
|
}
|