func (s *playersClient) Upload(userID int64, p api.Player, executable io.Reader) (*api.Player, error) { params := map[string]string{ "name": p.Name, "version": strconv.FormatInt(p.Version, 10), "author_id": strconv.FormatInt(userID, 10), } path, err := router.NewAPIRouter().Get(router.PlayersCreate).URL() if err != nil { return nil, err } url := s.client.BaseURL + path.String() req, err := httputil2.NewFileUploadRequest(url, params, "exe", "exe", executable) if err != nil { return nil, err } resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err } if resp.StatusCode != http.StatusOK { return nil, httputil2.NewHTTPError(resp) } var player api.Player if err := json.NewDecoder(resp.Body).Decode(&player); err != nil { return nil, err } return &player, nil }
func ConfigureRouter(s *api.Services, staticpath string) *mux.Router { r := router.NewAPIRouter() handlers.MountHandlers(r, s) // Finally, if none of the above routes match, delegate to the single-page // app's client-side router. Rewrite the path in order to load the // single-page app's root HTML entrypoint. The app will handle the route. r.NotFoundHandler = StaticPathFallback(staticpath) return r }
func NewTestServer(t *testing.T) *httptest.Server { if *pgaddr == "" { t.SkipNow() } ds, err := db.Open(dialect, *pgaddr, os.TempDir()) if err != nil { t.Fatal(err) } r := router.NewAPIRouter() MountHandlers(r, ds) return httptest.NewServer(r) }
func NewClient(opts ...Option) *api.Services { client := &APIClient{ httpClient: http.DefaultClient, BaseURL: DefaultBaseURL, apiRouter: router.NewAPIRouter(), } for _, o := range opts { o(client) } return &api.Services{ Matches: &matchesClient{client}, Players: &playersClient{client}, Users: &usersClient{client}, } }
func (s *playersClient) Create(userID int64, p api.Player) (*api.Player, error) { p.AuthorId = userID var buf bytes.Buffer if err := json.NewEncoder(&buf).Encode(&p); err != nil { return nil, err } path, err := router.NewAPIRouter().Get(router.PlayersCreate).URL() if err != nil { return nil, err } resp, err := http.Post(s.client.BaseURL+path.String(), "application/json", &buf) if err != nil { return nil, err } if resp.StatusCode != http.StatusOK { return nil, newHTTPError(resp) } var player api.Player if err := json.NewDecoder(resp.Body).Decode(&player); err != nil { return nil, err } return &player, nil }