-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bsync): initial Receivers implementation, HTTP support
- Loading branch information
Showing
4 changed files
with
250 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package bsync | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/qri-io/qri/manifest" | ||
) | ||
|
||
// HTTPRemote implents the Remote interface via HTTP POST requests | ||
type HTTPRemote struct { | ||
URL string | ||
} | ||
|
||
// ReqSend initiates a send session | ||
func (rem *HTTPRemote) ReqSend(mfst *manifest.Manifest) (sid string, diff *manifest.Manifest, err error) { | ||
buf := &bytes.Buffer{} | ||
if err = json.NewEncoder(buf).Encode(mfst); err != nil { | ||
return | ||
} | ||
|
||
req, err := http.NewRequest("POST", rem.URL, buf) | ||
if err != nil { | ||
return | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
err = fmt.Errorf("remote repsonse: %d", res.StatusCode) | ||
return | ||
} | ||
|
||
sid = res.Header.Get("sid") | ||
diff = &manifest.Manifest{} | ||
err = json.NewDecoder(res.Body).Decode(diff) | ||
|
||
return | ||
} | ||
|
||
// SendBlock sends a block over HTTP to a remote source | ||
func (rem *HTTPRemote) SendBlock(sid, hash string, data []byte) Response { | ||
url := fmt.Sprintf("%s?sid=%s&hash=%s", rem.URL, sid, hash) | ||
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(data)) | ||
if err != nil { | ||
return Response{ | ||
Hash: hash, | ||
Status: StatusErrored, | ||
Err: err, | ||
} | ||
} | ||
req.Header.Set("Content-Type", "application/octet-stream") | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return Response{ | ||
Hash: hash, | ||
Status: StatusErrored, | ||
Err: err, | ||
} | ||
} | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return Response{ | ||
Hash: hash, | ||
Status: StatusRetry, | ||
} | ||
} | ||
|
||
return Response{ | ||
Hash: hash, | ||
Status: StatusOk, | ||
} | ||
} |
Oops, something went wrong.