github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/resource/api/download.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  
    10  	"github.com/juju/errors"
    11  	charmresource "gopkg.in/juju/charm.v6-unstable/resource"
    12  
    13  	"github.com/juju/juju/resource"
    14  )
    15  
    16  // NewHTTPDownloadRequest creates a new HTTP download request
    17  // for the given resource.
    18  //
    19  // Intended for use on the client side.
    20  func NewHTTPDownloadRequest(resourceName string) (*http.Request, error) {
    21  	return http.NewRequest("GET", "/resources/"+resourceName, nil)
    22  }
    23  
    24  // ExtractDownloadRequest pulls the download request info out of the
    25  // given HTTP request.
    26  //
    27  // Intended for use on the server side.
    28  func ExtractDownloadRequest(req *http.Request) string {
    29  	return req.URL.Query().Get(":resource")
    30  }
    31  
    32  // UpdateDownloadResponse sets the appropriate headers in the response
    33  // to an HTTP download request.
    34  //
    35  // Intended for use on the server side.
    36  func UpdateDownloadResponse(resp http.ResponseWriter, resource resource.Resource) {
    37  	resp.Header().Set("Content-Type", ContentTypeRaw)
    38  	resp.Header().Set("Content-Length", fmt.Sprint(resource.Size))
    39  	resp.Header().Set("Content-Sha384", resource.Fingerprint.String())
    40  }
    41  
    42  // ExtractDownloadResponse pulls the download size and checksum
    43  // from the HTTP response.
    44  func ExtractDownloadResponse(resp *http.Response) (int64, charmresource.Fingerprint, error) {
    45  	var fp charmresource.Fingerprint
    46  
    47  	// TODO(ericsnow) Finish!
    48  	// See UpdateDownloadResponse for the data to extract.
    49  	return 0, fp, errors.New("not finished")
    50  }