Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interfaces/externalをinfrastructure/externalに移動 #588

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package infrastructure
package external

import (
"fmt"
Expand Down
50 changes: 33 additions & 17 deletions infrastructure/knoq.go → infrastructure/external/knoq.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
package infrastructure
//go:generate go run github.com/golang/mock/mockgen@latest -source=$GOFILE -destination=mock_$GOPACKAGE/mock_$GOFILE

package external

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"time"

"github.com/gofrs/uuid"
"github.com/traPtitech/traPortfolio/interfaces/external"
"github.com/traPtitech/traPortfolio/interfaces/external/mock_external_e2e"
"github.com/traPtitech/traPortfolio/usecases/repository"
"github.com/traPtitech/traPortfolio/util/config"
)

type KnoqAPI struct {
apiClient
type EventResponse struct {
ID uuid.UUID `json:"eventId"`
Name string `json:"name"`
Description string `json:"description"`
Place string `json:"place"`
GroupID uuid.UUID `json:"groupId"`
RoomID uuid.UUID `json:"roomId"`
TimeStart time.Time `json:"timeStart"`
TimeEnd time.Time `json:"timeEnd"`
SharedRoom bool `json:"sharedRoom"`
Admins []uuid.UUID `json:"admins"`
}

func NewKnoqAPI(conf *config.KnoqConfig, isDevelopment bool) (external.KnoqAPI, error) {
if isDevelopment {
return &mock_external_e2e.MockKnoqAPI{}, nil
}
type KnoqAPI interface {
GetEvents() ([]*EventResponse, error)
GetEvent(eventID uuid.UUID) (*EventResponse, error)
GetEventsByUserID(userID uuid.UUID) ([]*EventResponse, error)
}

type knoqAPI struct {
apiClient
}

func NewKnoqAPI(conf *config.KnoqConfig) (KnoqAPI, error) {

Check warning on line 40 in infrastructure/external/knoq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/knoq.go#L40

Added line #L40 was not covered by tests
jar, err := newCookieJar(conf.API(), "session")
if err != nil {
return nil, err
}

return &KnoqAPI{newAPIClient(jar, conf.API())}, nil
return &knoqAPI{newAPIClient(jar, conf.API())}, nil

Check warning on line 46 in infrastructure/external/knoq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/knoq.go#L46

Added line #L46 was not covered by tests
}

func (a *KnoqAPI) GetEvents() ([]*external.EventResponse, error) {
func (a *knoqAPI) GetEvents() ([]*EventResponse, error) {

Check warning on line 49 in infrastructure/external/knoq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/knoq.go#L49

Added line #L49 was not covered by tests
res, err := a.apiGet("/events")
if err != nil {
return nil, err
Expand All @@ -41,14 +57,14 @@
return nil, errors.New("GET /events failed")
}

var er []*external.EventResponse
var er []*EventResponse

Check warning on line 60 in infrastructure/external/knoq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/knoq.go#L60

Added line #L60 was not covered by tests
if err := json.NewDecoder(res.Body).Decode(&er); err != nil {
return nil, err
}
return er, nil
}

func (a *KnoqAPI) GetEvent(eventID uuid.UUID) (*external.EventResponse, error) {
func (a *knoqAPI) GetEvent(eventID uuid.UUID) (*EventResponse, error) {

Check warning on line 67 in infrastructure/external/knoq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/knoq.go#L67

Added line #L67 was not covered by tests
res, err := a.apiGet(fmt.Sprintf("/events/%s", eventID))
if err != nil {
return nil, err
Expand All @@ -63,14 +79,14 @@
return nil, fmt.Errorf("GET /events/%s failed: %d", eventID, res.StatusCode)
}

var er external.EventResponse
var er EventResponse

Check warning on line 82 in infrastructure/external/knoq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/knoq.go#L82

Added line #L82 was not covered by tests
if err := json.NewDecoder(res.Body).Decode(&er); err != nil {
return nil, err
}
return &er, nil
}

func (a *KnoqAPI) GetEventsByUserID(userID uuid.UUID) ([]*external.EventResponse, error) {
func (a *knoqAPI) GetEventsByUserID(userID uuid.UUID) ([]*EventResponse, error) {

Check warning on line 89 in infrastructure/external/knoq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/knoq.go#L89

Added line #L89 was not covered by tests
res, err := a.apiGet(fmt.Sprintf("/users/%s/events", userID))
if err != nil {
return nil, err
Expand All @@ -81,7 +97,7 @@
return nil, fmt.Errorf("GET /users/%s/events failed", userID)
}

var er []*external.EventResponse
var er []*EventResponse

Check warning on line 100 in infrastructure/external/knoq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/knoq.go#L100

Added line #L100 was not covered by tests
if err := json.NewDecoder(res.Body).Decode(&er); err != nil {
return nil, err
}
Expand All @@ -90,5 +106,5 @@

// Interface guards
var (
_ external.KnoqAPI = (*KnoqAPI)(nil)
_ KnoqAPI = (*knoqAPI)(nil)
)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mock_external_e2e //nolint:revive

import (
"github.com/gofrs/uuid"
"github.com/traPtitech/traPortfolio/interfaces/external"
"github.com/traPtitech/traPortfolio/infrastructure/external"
"github.com/traPtitech/traPortfolio/usecases/repository"
"github.com/traPtitech/traPortfolio/util/mockdata"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mock_external_e2e //nolint:revive
import (
"fmt"

"github.com/traPtitech/traPortfolio/interfaces/external"
"github.com/traPtitech/traPortfolio/infrastructure/external"
"github.com/traPtitech/traPortfolio/util/mockdata"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/gofrs/uuid"
"github.com/traPtitech/traPortfolio/domain"
"github.com/traPtitech/traPortfolio/interfaces/external"
"github.com/traPtitech/traPortfolio/infrastructure/external"
"github.com/traPtitech/traPortfolio/util/mockdata"
)

Expand Down
39 changes: 23 additions & 16 deletions infrastructure/portal.go → infrastructure/external/portal.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package infrastructure
//go:generate go run github.com/golang/mock/mockgen@latest -source=$GOFILE -destination=mock_$GOPACKAGE/mock_$GOFILE

package external

import (
"encoding/json"
Expand All @@ -7,40 +9,45 @@
"time"

"github.com/patrickmn/go-cache"
"github.com/traPtitech/traPortfolio/interfaces/external"
"github.com/traPtitech/traPortfolio/interfaces/external/mock_external_e2e"
"github.com/traPtitech/traPortfolio/util/config"
)

type PortalUserResponse struct {
TraQID string `json:"id"`
RealName string `json:"name"`
AlphabeticName string `json:"alphabeticName"`
}

type PortalAPI interface {
GetUsers() ([]*PortalUserResponse, error)
GetUserByTraqID(traQID string) (*PortalUserResponse, error)
}

const (
cacheKey = "portalUsers"
)

type PortalAPI struct {
type portalAPI struct {
apiClient
cache *cache.Cache
}

func NewPortalAPI(conf *config.PortalConfig, isDevelopment bool) (external.PortalAPI, error) {
if isDevelopment {
return mock_external_e2e.NewMockPortalAPI(), nil
}

func NewPortalAPI(conf *config.PortalConfig) (PortalAPI, error) {

Check warning on line 35 in infrastructure/external/portal.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/portal.go#L35

Added line #L35 was not covered by tests
jar, err := newCookieJar(conf.API(), "access_token")
if err != nil {
return nil, err
}

return &PortalAPI{
return &portalAPI{

Check warning on line 41 in infrastructure/external/portal.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/portal.go#L41

Added line #L41 was not covered by tests
apiClient: newAPIClient(jar, conf.API()),
cache: cache.New(1*time.Hour, 2*time.Hour),
}, nil
}

func (a *PortalAPI) GetUsers() ([]*external.PortalUserResponse, error) {
func (a *portalAPI) GetUsers() ([]*PortalUserResponse, error) {

Check warning on line 47 in infrastructure/external/portal.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/portal.go#L47

Added line #L47 was not covered by tests
portalUsers, found := a.cache.Get(cacheKey)
if found {
return portalUsers.([]*external.PortalUserResponse), nil
return portalUsers.([]*PortalUserResponse), nil

Check warning on line 50 in infrastructure/external/portal.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/portal.go#L50

Added line #L50 was not covered by tests
}

res, err := a.apiGet("/user")
Expand All @@ -52,15 +59,15 @@
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("GET /user failed: %v", res.Status)
}
var userResponses []*external.PortalUserResponse
var userResponses []*PortalUserResponse

Check warning on line 62 in infrastructure/external/portal.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/portal.go#L62

Added line #L62 was not covered by tests
if err := json.NewDecoder(res.Body).Decode(&userResponses); err != nil {
return nil, fmt.Errorf("decode failed: %w", err)
}
a.cache.Set(cacheKey, userResponses, cache.DefaultExpiration)
return userResponses, nil
}

func (a *PortalAPI) GetUserByTraqID(traQID string) (*external.PortalUserResponse, error) {
func (a *portalAPI) GetUserByTraqID(traQID string) (*PortalUserResponse, error) {

Check warning on line 70 in infrastructure/external/portal.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/portal.go#L70

Added line #L70 was not covered by tests
if traQID == "" {
return nil, fmt.Errorf("invalid traQID")
}
Expand All @@ -75,7 +82,7 @@
return nil, fmt.Errorf("GET /user/%v failed: %v", traQID, res.Status)
}

var userResponse external.PortalUserResponse
var userResponse PortalUserResponse

Check warning on line 85 in infrastructure/external/portal.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/portal.go#L85

Added line #L85 was not covered by tests
if err := json.NewDecoder(res.Body).Decode(&userResponse); err != nil {
return nil, fmt.Errorf("decode failed: %w", err)
}
Expand All @@ -84,5 +91,5 @@

// Interface guards
var (
_ external.PortalAPI = (*PortalAPI)(nil)
_ PortalAPI = (*portalAPI)(nil)
)
42 changes: 27 additions & 15 deletions infrastructure/traq.go → infrastructure/external/traq.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
package infrastructure
//go:generate go run github.com/golang/mock/mockgen@latest -source=$GOFILE -destination=mock_$GOPACKAGE/mock_$GOFILE

package external

import (
"encoding/json"
"fmt"
"net/http"

"github.com/gofrs/uuid"
"github.com/traPtitech/traPortfolio/interfaces/external"
"github.com/traPtitech/traPortfolio/interfaces/external/mock_external_e2e"
"github.com/traPtitech/traPortfolio/domain"
"github.com/traPtitech/traPortfolio/util/config"
)

type TraQAPI struct {
apiClient
type TraQUserResponse struct {
ID uuid.UUID `json:"id"`
State domain.TraQState `json:"state"`
}

func NewTraQAPI(conf *config.TraqConfig, isDevelopment bool) (external.TraQAPI, error) {
if isDevelopment {
return mock_external_e2e.NewMockTraQAPI(), nil
}
type TraQGetAllArgs struct {
IncludeSuspended bool
Name string
}

type TraQAPI interface {
GetUsers(args *TraQGetAllArgs) ([]*TraQUserResponse, error)
GetUser(userID uuid.UUID) (*TraQUserResponse, error)
}

type traQAPI struct {
apiClient
}

func NewTraQAPI(conf *config.TraqConfig) (TraQAPI, error) {

Check warning on line 34 in infrastructure/external/traq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/traq.go#L34

Added line #L34 was not covered by tests
jar, err := newCookieJar(conf.API(), "r_session")
if err != nil {
return nil, err
}

return &TraQAPI{newAPIClient(jar, conf.API())}, nil
return &traQAPI{newAPIClient(jar, conf.API())}, nil

Check warning on line 40 in infrastructure/external/traq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/traq.go#L40

Added line #L40 was not covered by tests
}

func (a *TraQAPI) GetUsers(args *external.TraQGetAllArgs) ([]*external.TraQUserResponse, error) {
func (a *traQAPI) GetUsers(args *TraQGetAllArgs) ([]*TraQUserResponse, error) {

Check warning on line 43 in infrastructure/external/traq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/traq.go#L43

Added line #L43 was not covered by tests
res, err := a.apiGet(fmt.Sprintf("/users?include-suspended=%t&name=%s", args.IncludeSuspended, args.Name))
if err != nil {
return nil, err
Expand All @@ -39,14 +51,14 @@
return nil, fmt.Errorf("GET /users failed: %v", res.Status)
}

var usersResponse []*external.TraQUserResponse
var usersResponse []*TraQUserResponse

Check warning on line 54 in infrastructure/external/traq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/traq.go#L54

Added line #L54 was not covered by tests
if err := json.NewDecoder(res.Body).Decode(&usersResponse); err != nil {
return nil, fmt.Errorf("decode failed: %w", err)
}
return usersResponse, nil
}

func (a *TraQAPI) GetUser(userID uuid.UUID) (*external.TraQUserResponse, error) {
func (a *traQAPI) GetUser(userID uuid.UUID) (*TraQUserResponse, error) {

Check warning on line 61 in infrastructure/external/traq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/traq.go#L61

Added line #L61 was not covered by tests
res, err := a.apiGet(fmt.Sprintf("/users/%v", userID))
if err != nil {
return nil, err
Expand All @@ -57,7 +69,7 @@
return nil, fmt.Errorf("GET /users/%v failed: %v", userID, res.Status)
}

var userResponse external.TraQUserResponse
var userResponse TraQUserResponse

Check warning on line 72 in infrastructure/external/traq.go

View check run for this annotation

Codecov / codecov/patch

infrastructure/external/traq.go#L72

Added line #L72 was not covered by tests
if err := json.NewDecoder(res.Body).Decode(&userResponse); err != nil {
return nil, fmt.Errorf("decode failed: %v", err)
}
Expand All @@ -66,5 +78,5 @@

// Interface guards
var (
_ external.TraQAPI = (*TraQAPI)(nil)
_ TraQAPI = (*traQAPI)(nil)
)
Loading
Loading