github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/api/download.go (about) 1 // Package api provides native Go-based API/SDK over HTTP(S). 2 /* 3 * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package api 6 7 import ( 8 "net/http" 9 "sort" 10 "time" 11 12 "github.com/NVIDIA/aistore/api/apc" 13 "github.com/NVIDIA/aistore/cmn" 14 "github.com/NVIDIA/aistore/cmn/cos" 15 "github.com/NVIDIA/aistore/ext/dload" 16 ) 17 18 func DownloadSingle(bp BaseParams, description string, 19 bck cmn.Bck, objName, link string, intervals ...time.Duration) (string, error) { 20 dlBody := dload.SingleBody{ 21 SingleObj: dload.SingleObj{ 22 ObjName: objName, 23 Link: link, 24 }, 25 } 26 if len(intervals) > 0 { 27 dlBody.ProgressInterval = intervals[0].String() 28 } 29 dlBody.Bck = bck 30 dlBody.Description = description 31 return DownloadWithParam(bp, dload.TypeSingle, &dlBody) 32 } 33 34 func DownloadRange(bp BaseParams, description string, bck cmn.Bck, template string, intervals ...time.Duration) (string, error) { 35 dlBody := dload.RangeBody{Template: template} 36 if len(intervals) > 0 { 37 dlBody.ProgressInterval = intervals[0].String() 38 } 39 dlBody.Bck = bck 40 dlBody.Description = description 41 return DownloadWithParam(bp, dload.TypeRange, dlBody) 42 } 43 44 func DownloadWithParam(bp BaseParams, dlt dload.Type, body any) (id string, err error) { 45 bp.Method = http.MethodPost 46 msg := cos.MustMarshal(body) 47 reqParams := AllocRp() 48 { 49 reqParams.BaseParams = bp 50 reqParams.Path = apc.URLPathDownload.S 51 reqParams.Body = cos.MustMarshal(dload.Body{Type: dlt, RawMessage: msg}) 52 reqParams.Header = http.Header{cos.HdrContentType: []string{cos.ContentJSON}} 53 } 54 id, err = reqParams.doDlDownloadRequest() 55 FreeRp(reqParams) 56 return 57 } 58 59 func DownloadMulti(bp BaseParams, description string, bck cmn.Bck, msg any, intervals ...time.Duration) (string, error) { 60 dlBody := dload.MultiBody{} 61 if len(intervals) > 0 { 62 dlBody.ProgressInterval = intervals[0].String() 63 } 64 dlBody.Bck = bck 65 dlBody.Description = description 66 dlBody.ObjectsPayload = msg 67 return DownloadWithParam(bp, dload.TypeMulti, dlBody) 68 } 69 70 func DownloadBackend(bp BaseParams, descr string, bck cmn.Bck, prefix, suffix string, ivals ...time.Duration) (string, error) { 71 dlBody := dload.BackendBody{Prefix: prefix, Suffix: suffix} 72 if len(ivals) > 0 { 73 dlBody.ProgressInterval = ivals[0].String() 74 } 75 dlBody.Bck = bck 76 dlBody.Description = descr 77 return DownloadWithParam(bp, dload.TypeBackend, dlBody) 78 } 79 80 func DownloadStatus(bp BaseParams, id string, onlyActive bool) (dlStatus *dload.StatusResp, err error) { 81 dlBody := dload.AdminBody{ID: id, OnlyActive: onlyActive} 82 bp.Method = http.MethodGet 83 reqParams := AllocRp() 84 { 85 reqParams.BaseParams = bp 86 reqParams.Path = apc.URLPathDownload.S 87 reqParams.Body = cos.MustMarshal(dlBody) 88 reqParams.Header = http.Header{cos.HdrContentType: []string{cos.ContentJSON}} 89 } 90 91 dlStatus = &dload.StatusResp{} 92 _, err = reqParams.DoReqAny(dlStatus) 93 FreeRp(reqParams) 94 return 95 } 96 97 func DownloadGetList(bp BaseParams, regex string, onlyActive bool) (dlList dload.JobInfos, err error) { 98 dlBody := dload.AdminBody{Regex: regex, OnlyActive: onlyActive} 99 bp.Method = http.MethodGet 100 reqParams := AllocRp() 101 { 102 reqParams.BaseParams = bp 103 reqParams.Path = apc.URLPathDownload.S 104 reqParams.Body = cos.MustMarshal(dlBody) 105 reqParams.Header = http.Header{cos.HdrContentType: []string{cos.ContentJSON}} 106 } 107 _, err = reqParams.DoReqAny(&dlList) 108 FreeRp(reqParams) 109 sort.Sort(dlList) 110 return 111 } 112 113 func AbortDownload(bp BaseParams, id string) error { 114 dlBody := dload.AdminBody{ID: id} 115 bp.Method = http.MethodDelete 116 reqParams := AllocRp() 117 { 118 reqParams.BaseParams = bp 119 reqParams.Path = apc.URLPathDownloadAbort.S 120 reqParams.Body = cos.MustMarshal(dlBody) 121 reqParams.Header = http.Header{cos.HdrContentType: []string{cos.ContentJSON}} 122 } 123 err := reqParams.DoRequest() 124 FreeRp(reqParams) 125 return err 126 } 127 128 func RemoveDownload(bp BaseParams, id string) error { 129 dlBody := dload.AdminBody{ID: id} 130 bp.Method = http.MethodDelete 131 reqParams := AllocRp() 132 { 133 reqParams.BaseParams = bp 134 reqParams.Path = apc.URLPathDownloadRemove.S 135 reqParams.Body = cos.MustMarshal(dlBody) 136 reqParams.Header = http.Header{cos.HdrContentType: []string{cos.ContentJSON}} 137 } 138 err := reqParams.DoRequest() 139 FreeRp(reqParams) 140 return err 141 } 142 143 // TODO: simplify `dload.DlPostResp` => string 144 func (reqParams *ReqParams) doDlDownloadRequest() (string, error) { 145 var resp dload.DlPostResp 146 _, err := reqParams.DoReqAny(&resp) 147 return resp.ID, err 148 }