Skip to content

Commit

Permalink
version v0.1.0 implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
GaMoCh committed Aug 17, 2021
0 parents commit 2d1b4f1
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_size = 2
end_of_line = lf
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
34 changes: 34 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Release Project

env:
GO_VERSION: 1.16

on:
push:
tags:
- '*'

permissions:
contents: write

jobs:
build:
name: GoReleaser build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
.vscode/
17 changes: 17 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
project_name: popular_repos

archives:
- format_overrides:
- goos: windows
format: zip

builds:
- id: lab01s01
main: ./cmd/lab01s01
binary: bin/lab01s01
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Gabriel Moreira Chaves and Ian Bittencourt Andrade Jacinto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
44 changes: 44 additions & 0 deletions cmd/lab01s01/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"context"
"encoding/json"
"os"

"github.com/gamoch/popular_repos/internal/pkg/configuration"
"github.com/gamoch/popular_repos/pkg/graphql"
"github.com/gamoch/popular_repos/pkg/graphql/providers/github"
"github.com/gamoch/popular_repos/pkg/logs"
)

const query = `query PopularRepos {
search(query: "stars:>1", type: REPOSITORY, first: 100) {
nodes {
... on Repository {
nameWithOwner
stargazerCount
createdAt
}
}
}
}`

func main() {
config := configuration.Get()
ctx := context.Background()

githubClient := github.NewClient(config.Token)
req := graphql.NewRequest(query)

graphQLData := new(GraphQLData)
if err := githubClient.Run(ctx, req, graphQLData); err != nil {
logs.Error.Fatal(err)
}

graphqlJSON, err := json.MarshalIndent(graphQLData, "", " ")
if err != nil {
logs.Error.Fatal(err)
}

os.Stdout.Write(graphqlJSON)
}
13 changes: 13 additions & 0 deletions cmd/lab01s01/structures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "time"

type GraphQLData struct {
Search struct {
Nodes []struct {
NameWithOwner string `json:"nameWithOwner"`
StargazerCount int `json:"stargazerCount"`
CreatedAt time.Time `json:"createdAt"`
} `json:"nodes"`
} `json:"search"`
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/gamoch/popular_repos

go 1.16
19 changes: 19 additions & 0 deletions internal/pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package configuration

import (
"flag"
"os"
)

type config struct {
Token string
}

func Get() *config {
token := flag.String("token", os.Getenv("GITHUB_TOKEN"), "GitHub Token (env: GITHUB_TOKEN)")
flag.Parse()

return &config{
Token: *token,
}
}
85 changes: 85 additions & 0 deletions pkg/graphql/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package graphql

import (
"bytes"
"context"
"encoding/json"
"net/http"
)

type Client struct {
endpoint string
httpClient *http.Client
}

type ClientOption func(*Client)

type ClientRunner interface {
Run(ctx context.Context, req *Request, res interface{}) error
}

func NewClient(endpoint string, options ...ClientOption) *Client {
client := &Client{
endpoint: endpoint,
httpClient: http.DefaultClient,
}

for _, optionFunc := range options {
optionFunc(client)
}

return client
}

func WithHTTPClient(httpClient *http.Client) ClientOption {
return func(client *Client) {
client.httpClient = httpClient
}
}

func (c *Client) Run(ctx context.Context, req *Request, res interface{}) error {
if req == nil {
return &Err{err: ErrNilRequest}
}

if ctx == nil {
ctx = context.Background()
} else if err := ctx.Err(); err != nil {
return &Err{err: err}
}

reqBody := new(bytes.Buffer)
if err := json.NewEncoder(reqBody).Encode(req.requestBody); err != nil {
return &Err{err: err}
}

graphQLReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpoint, reqBody)
if err != nil {
return &Err{err: err}
}
graphQLReq.Header = req.Header

resp, err := c.httpClient.Do(graphQLReq)
if err != nil {
return &Err{err: err}
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return &Err{err: &ErrStatus{
Status: resp.Status,
StatusCode: resp.StatusCode,
}}
}

graphQLRes := &response{Data: res}
if err = json.NewDecoder(resp.Body).Decode(graphQLRes); err != nil {
return &Err{err: err}
}

if len(graphQLRes.Errors) > 0 {
return &Err{err: joinResponseErrors(graphQLRes.Errors)}
}

return nil
}
26 changes: 26 additions & 0 deletions pkg/graphql/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package graphql

import "errors"

type Err struct {
err error
}

func (e *Err) Error() string {
return "graphql: " + e.err.Error()
}

func (e *Err) Unwrap() error {
return e.err
}

type ErrStatus struct {
StatusCode int
Status string
}

func (e *ErrStatus) Error() string {
return "wrong HTTP status: " + e.Status
}

var ErrNilRequest = errors.New("graphQL request not provided")
49 changes: 49 additions & 0 deletions pkg/graphql/providers/github/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package github

import (
"context"
"errors"
"net/http"

"github.com/gamoch/popular_repos/pkg/graphql"
"github.com/gamoch/popular_repos/pkg/logs"
)

const endpoint = "https://api.github.com/graphql"

type client struct {
token string
client *graphql.Client
}

func NewClient(token string, options ...graphql.ClientOption) *client {
if token == "" {
logs.Error.Fatalln("GITHUB_TOKEN is required")
}

return &client{
token: token,
client: graphql.NewClient(endpoint, options...),
}
}

func (c *client) Run(ctx context.Context, req *graphql.Request, res interface{}) error {
if req != nil {
req.Header.Add("Authorization", "bearer "+c.token)
}

err := c.client.Run(ctx, req, res)
if cause := errors.Unwrap(err); cause != nil {
if errStatus, ok := cause.(*graphql.ErrStatus); ok {
if errStatus.StatusCode == http.StatusUnauthorized {
return &Err{err: &ErrInvalidToken{err: err}}
}
}
}

if err != nil {
return &Err{err: err}
}

return nil
}
25 changes: 25 additions & 0 deletions pkg/graphql/providers/github/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package github

type Err struct {
err error
}

func (e *Err) Error() string {
return "github: " + e.err.Error()
}

func (e *Err) Unwrap() error {
return e.err
}

type ErrInvalidToken struct {
err error
}

func (e *ErrInvalidToken) Error() string {
return "invalid token; " + e.err.Error()
}

func (e *ErrInvalidToken) Unwrap() error {
return e.err
}
Loading

0 comments on commit 2d1b4f1

Please sign in to comment.