github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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/client"
    27  	"github.com/snapcore/snapd/overlord/auth"
    28  	"github.com/snapcore/snapd/store"
    29  )
    30  
    31  var (
    32  	sectionsCmd = &Command{
    33  		Path:   "/v2/sections",
    34  		UserOK: true,
    35  		GET:    getSections,
    36  	}
    37  )
    38  
    39  func getSections(c *Command, r *http.Request, user *auth.UserState) Response {
    40  	// TODO: test this
    41  	route := c.d.router.Get(snapCmd.Path)
    42  	if route == nil {
    43  		return InternalError("cannot find route for snaps")
    44  	}
    45  
    46  	theStore := getStore(c)
    47  
    48  	// TODO: use a per-request context
    49  	sections, err := theStore.Sections(context.TODO(), user)
    50  	switch err {
    51  	case nil:
    52  		// pass
    53  	case store.ErrBadQuery:
    54  		return SyncResponse(&resp{
    55  			Type:   ResponseTypeError,
    56  			Result: &errorResult{Message: err.Error(), Kind: client.ErrorKindBadQuery},
    57  			Status: 400,
    58  		}, nil)
    59  	case store.ErrUnauthenticated, store.ErrInvalidCredentials:
    60  		return Unauthorized("%v", err)
    61  	default:
    62  		return InternalError("%v", err)
    63  	}
    64  
    65  	return SyncResponse(sections, nil)
    66  }