윤준석

ADD: get item by keyword service

...@@ -3,28 +3,6 @@ package main ...@@ -3,28 +3,6 @@ package main
3 import "github.com/labstack/echo/v4" 3 import "github.com/labstack/echo/v4"
4 4
5 func main() { 5 func main() {
6 - //keyword := "m1 pro 맥북 프로 16인치"
7 - //encText := url2.QueryEscape("중고나라" + keyword)
8 - //url := "https://openapi.naver.com/v1/search/cafearticle.json?query=" + encText + "&sort=sim"
9 - //
10 - //req, err := http.NewRequest("GET", url, nil)
11 - //if err != nil {
12 - // log.Fatal(err)
13 - //}
14 - //req.Header.Add("X-Naver-Client-Id", config.Cfg.Secret.CLIENTID)
15 - //req.Header.Add("X-Naver-Client-Secret", config.Cfg.Secret.CLIENTSECRET)
16 - //
17 - //client := &http.Client{}
18 - //resp, err := client.Do(req)
19 - //if err != nil {
20 - // log.Fatal(err)
21 - //}
22 - //defer resp.Body.Close()
23 - //
24 - //bytes, _ := ioutil.ReadAll(resp.Body)
25 - //str := string(bytes)
26 - //fmt.Println(str)
27 -
28 e := echo.New() 6 e := echo.New()
29 7
30 e.Logger.Fatal(e.Start(":8080")) 8 e.Logger.Fatal(e.Start(":8080"))
......
1 +package service
2 +
3 +import (
4 + "fmt"
5 + "io/ioutil"
6 + "joongna/config"
7 + "joongna/model"
8 + "log"
9 + "net/http"
10 + "net/url"
11 +)
12 +
13 +func GetItemByKeyword(keyword string) ([]model.Item, error) {
14 + var items []model.Item
15 + return items, nil
16 +}
17 +
18 +func GetItemInfoByKeyword(keyword string) {
19 + encText := url.QueryEscape("중고나라" + keyword)
20 + url := "https://openapi.naver.com/v1/search/cafearticle.json?query=" + encText + "&sort=sim"
21 +
22 + req, err := http.NewRequest("GET", url, nil)
23 + if err != nil {
24 + log.Fatal(err)
25 + }
26 + req.Header.Add("X-Naver-Client-Id", config.Cfg.Secret.CLIENTID)
27 + req.Header.Add("X-Naver-Client-Secret", config.Cfg.Secret.CLIENTSECRET)
28 +
29 + client := &http.Client{}
30 + resp, err := client.Do(req)
31 + if err != nil {
32 + log.Fatal(err)
33 + }
34 + defer resp.Body.Close()
35 +
36 + bytes, _ := ioutil.ReadAll(resp.Body)
37 + str := string(bytes)
38 + fmt.Println(str)
39 +}