Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new http request #159

Open
wants to merge 2 commits into
base: mclib
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ logs/info.log
logs/error.log
build/swan-lib
build/swan-lib_unix
.idea
34 changes: 34 additions & 0 deletions client/ipfs/ipfs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ipfs

import (
"context"
"fmt"
shell "github.com/ipfs/go-ipfs-api"
"net/http"

"github.com/filswan/go-swan-lib/client/web"
Expand Down Expand Up @@ -46,3 +48,35 @@ func Export2CarFile(apiUrl, fileHash string, carFileFullPath string) error {
logs.GetLogger().Info(bytesWritten, " bytes have been written to:", carFileFullPath)
return nil
}

func UploadDirToIpfs(sh *shell.Shell, dirName string) (string, error) {

dataCid, err := sh.AddDir(dirName)
if err != nil {
logs.GetLogger().Error(err)
return "", err
}

return dataCid, nil
}

func DataCidIsDirectory(sh *shell.Shell, dataCid string) (*bool, error) {

path := utils.UrlJoin("/ipfs/", dataCid)
stat, err := sh.FilesStat(context.Background(), path)
if err != nil {
logs.GetLogger().Error(err)
return nil, err
}

isFile := false
if stat.Type == "directory" {
isFile = true
}

return &isFile, nil
}

func DownloadFromIpfs(sh *shell.Shell, dataCid, outDir string) error {
return sh.Get(dataCid, outDir)
}
66 changes: 66 additions & 0 deletions client/web/restful.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,72 @@ func HttpRequest(httpMethod, uri, tokenString string, params interface{}) ([]byt
return responseBody, nil
}

func HttpRequestWithKey(httpMethod, uri, key, token string, params interface{}) ([]byte, error) {
var request *http.Request
var err error

switch params := params.(type) {
case io.Reader:
request, err = http.NewRequest(httpMethod, uri, params)
if err != nil {
logs.GetLogger().Error(err)
return nil, err
}
request.Header.Set("Content-Type", HTTP_CONTENT_TYPE_FORM)
default:
jsonReq, errJson := json.Marshal(params)
if errJson != nil {
logs.GetLogger().Error(errJson)
return nil, errJson
}

request, err = http.NewRequest(httpMethod, uri, bytes.NewBuffer(jsonReq))
if err != nil {
logs.GetLogger().Error(err)
return nil, err
}
request.Header.Set("Content-Type", HTTP_CONTENT_TYPE_JSON)
}

if len(strings.Trim(key, " ")) > 0 {
request.Header.Set("api_key", key)
}

if len(strings.Trim(token, " ")) > 0 {
request.Header.Set("api_token", token)
}

client := &http.Client{}
response, err := client.Do(request)

if err != nil {
logs.GetLogger().Error(err)
return nil, err
}

defer response.Body.Close()

if response.StatusCode != http.StatusOK {
err := fmt.Errorf("http status: %s, code:%d, url:%s", response.Status, response.StatusCode, uri)
logs.GetLogger().Error(err)
switch response.StatusCode {
case http.StatusNotFound:
logs.GetLogger().Error("please check your url:", uri)
case http.StatusUnauthorized:
logs.GetLogger().Error("Please check your key:", key, " and token:", token)
}
return nil, err
}

responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
logs.GetLogger().Error(err)
return nil, err
}

return responseBody, nil
}

func HttpPutFile(url string, tokenString string, paramTexts map[string]string, paramFilename, paramFilepath string) (string, error) {
response, err := HttpRequestFile(http.MethodPut, url, tokenString, paramTexts, paramFilename, paramFilepath)
return response, err
Expand Down