From 618b472d1329cabbd22c2b4450bff7ea17445315 Mon Sep 17 00:00:00 2001 From: Leo Dev Date: Tue, 21 May 2024 15:26:01 +0200 Subject: [PATCH] pms support --- src/fetcher.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/fetcher.go diff --git a/src/fetcher.go b/src/fetcher.go new file mode 100644 index 0000000..827f86a --- /dev/null +++ b/src/fetcher.go @@ -0,0 +1,64 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" +) + +type Request struct { + Body []byte + StatusCode int +} + +type Package struct { + Author string `json:"author"` + Description string `json:"description"` + URL string `json:"url"` +} + +type PackageIndex struct { + Name string `json:"name"` + Repo string `json:"repo"` + Message string `json:"message"` +} + +func sendRequest(method, url string, jsonBody []byte) Request { + bodyReader := bytes.NewReader(jsonBody) + req, err := http.NewRequest(method, url, bodyReader) + if err != nil { + fmt.Printf("Error fetching package info: %v\n", err) + return Request{Body: []byte(""), StatusCode: 402} + } + req.Header.Set("Content-Type", "application/json") + res, err := http.DefaultClient.Do(req) + if err != nil { + fmt.Printf("client: error making http request: %s\n", err) + os.Exit(1) + } + defer res.Body.Close() + resBody, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Printf("client: could not read response body: %s\n", err) + os.Exit(1) + } + return Request{Body: resBody, StatusCode: res.StatusCode} +} + +func getRepoURL(packageName string) (string, error) { + var pkgidx PackageIndex + jsonBody := []byte(fmt.Sprintf(`{"name": "%s"}`, packageName)) + req := sendRequest(http.MethodGet, "http://localhost:3000", jsonBody) + err := json.Unmarshal(req.Body, &pkgidx) + if err != nil { + fmt.Println("Json Error: ", err) + return "", err + } + if req.StatusCode != 200 { + return "", fmt.Errorf(pkgidx.Message) + } + return pkgidx.Repo, nil +}