-
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(api/registry, api/search, api/version): add api endpoints to get…
… closer to command line functionality Merge pull request #475 from qri-io/api
- Loading branch information
Showing
31 changed files
with
344 additions
and
203 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
util "github.com/datatogether/api/apiutil" | ||
"github.com/qri-io/qri/lib" | ||
"github.com/qri-io/qri/repo" | ||
) | ||
|
||
// RegistryHandlers wraps a requests struct to interface with http.HandlerFunc | ||
type RegistryHandlers struct { | ||
lib.RegistryRequests | ||
} | ||
|
||
// NewRegistryHandlers allocates a RegistryHandlers pointer | ||
func NewRegistryHandlers(r repo.Repo) *RegistryHandlers { | ||
req := lib.NewRegistryRequests(r, nil) | ||
h := RegistryHandlers{*req} | ||
return &h | ||
} | ||
|
||
// RegistryHandler is the endpoint to call to the registry | ||
func (h *RegistryHandlers) RegistryHandler(w http.ResponseWriter, r *http.Request) { | ||
switch r.Method { | ||
case "OPTIONS": | ||
util.EmptyOkHandler(w, r) | ||
// case "GET": | ||
// // get status of dataset, is it published or not | ||
// h.statusRegistryHandler(w, r) | ||
case "POST", "PUT": | ||
// publish a dataset to the registry | ||
h.publishRegistryHandler(w, r) | ||
case "DELETE": | ||
// unpublish a dataset from the registry | ||
h.unpublishRegistryHandler(w, r) | ||
default: | ||
util.NotFoundHandler(w, r) | ||
} | ||
} | ||
|
||
func (h *RegistryHandlers) statusRegistryHandler(w http.ResponseWriter, r *http.Request) { | ||
ref, err := DatasetRefFromPath(r.URL.Path[len("/registry"):]) | ||
if err != nil { | ||
util.WriteErrResponse(w, http.StatusBadRequest, err) | ||
return | ||
} | ||
var res bool | ||
if err := h.RegistryRequests.Status(&ref, &res); err != nil { | ||
util.WriteResponse(w, fmt.Sprintf("error getting status from registry: %s", err)) | ||
return | ||
} | ||
|
||
util.WriteResponse(w, fmt.Sprintf("dataset %s is published to the registry", ref)) | ||
} | ||
|
||
func (h *RegistryHandlers) publishRegistryHandler(w http.ResponseWriter, r *http.Request) { | ||
ref, err := DatasetRefFromPath(r.URL.Path[len("/registry"):]) | ||
if err != nil { | ||
util.WriteErrResponse(w, http.StatusBadRequest, err) | ||
return | ||
} | ||
var res bool | ||
p := &lib.PublishParams{ | ||
Ref: ref, | ||
Pin: true, | ||
} | ||
if err = h.RegistryRequests.Publish(p, &res); err != nil { | ||
util.WriteErrResponse(w, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
util.WriteResponse(w, fmt.Sprintf("published dataset %s", ref)) | ||
} | ||
|
||
func (h *RegistryHandlers) unpublishRegistryHandler(w http.ResponseWriter, r *http.Request) { | ||
ref, err := DatasetRefFromPath(r.URL.Path[len("/registry"):]) | ||
if err != nil { | ||
util.WriteErrResponse(w, http.StatusBadRequest, err) | ||
return | ||
} | ||
var res bool | ||
if err = h.RegistryRequests.Unpublish(&ref, &res); err != nil { | ||
util.WriteErrResponse(w, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
util.WriteResponse(w, fmt.Sprintf("unpublished dataset %s", ref)) | ||
} |
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,59 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
util "github.com/datatogether/api/apiutil" | ||
"github.com/qri-io/qri/lib" | ||
"github.com/qri-io/qri/repo" | ||
) | ||
|
||
// SearchHandlers wraps a requests struct to interface with http.HandlerFunc | ||
type SearchHandlers struct { | ||
lib.SearchRequests | ||
} | ||
|
||
// NewSearchHandlers allocates a SearchHandlers pointer | ||
func NewSearchHandlers(r repo.Repo) *SearchHandlers { | ||
req := lib.NewSearchRequests(r, nil) | ||
return &SearchHandlers{*req} | ||
} | ||
|
||
// SearchHandler is the endpoint for searching qri | ||
func (h *SearchHandlers) SearchHandler(w http.ResponseWriter, r *http.Request) { | ||
switch r.Method { | ||
case "OPTIONS": | ||
util.EmptyOkHandler(w, r) | ||
case "GET": | ||
h.searchHandler(w, r) | ||
default: | ||
util.NotFoundHandler(w, r) | ||
} | ||
} | ||
|
||
func (h *SearchHandlers) searchHandler(w http.ResponseWriter, r *http.Request) { | ||
// p := util.PageFromRequest(r) | ||
sp := &lib.SearchParams{ | ||
QueryString: r.FormValue("q"), | ||
Limit: 100, | ||
Offset: 0, | ||
} | ||
|
||
if r.Header.Get("Content-Type") == "application/json" { | ||
if err := json.NewDecoder(r.Body).Decode(sp); err != nil { | ||
util.WriteErrResponse(w, http.StatusBadRequest, err) | ||
return | ||
} | ||
} | ||
|
||
results := []lib.SearchResult{} | ||
|
||
if err := h.SearchRequests.Search(sp, &results); err != nil { | ||
log.Infof("search error: %s", err.Error()) | ||
util.WriteErrResponse(w, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
util.WriteResponse(w, results) | ||
} |
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
Oops, something went wrong.