github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/daemon/api_sections.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2015-2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package daemon
    21  
    22  import (
    23  	"context"
    24  	"net/http"
    25  
    26  	"github.com/snapcore/snapd/overlord/auth"
    27  	"github.com/snapcore/snapd/store"
    28  )
    29  
    30  var (
    31  	sectionsCmd = &Command{
    32  		Path:       "/v2/sections",
    33  		GET:        getSections,
    34  		ReadAccess: openAccess{},
    35  	}
    36  )
    37  
    38  func getSections(c *Command, r *http.Request, user *auth.UserState) Response {
    39  	// TODO: test this
    40  	route := c.d.router.Get(snapCmd.Path)
    41  	if route == nil {
    42  		return InternalError("cannot find route for snaps")
    43  	}
    44  
    45  	theStore := storeFrom(c.d)
    46  
    47  	// TODO: use a per-request context
    48  	sections, err := theStore.Sections(context.TODO(), user)
    49  	switch err {
    50  	case nil:
    51  		// pass
    52  	case store.ErrBadQuery:
    53  		return BadQuery()
    54  	case store.ErrUnauthenticated, store.ErrInvalidCredentials:
    55  		return Unauthorized("%v", err)
    56  	default:
    57  		return InternalError("%v", err)
    58  	}
    59  
    60  	return SyncResponse(sections)
    61  }