-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from g0tiu5a/change_repo_layout
Change repo layout
- Loading branch information
Showing
12 changed files
with
269 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/g0tiu5a/ctftime" | ||
) | ||
|
||
func main() { | ||
url := ctftime.GetUrl("events", nil) | ||
fmt.Printf("[==>] Requesting %s ...\n", url) | ||
events := ctftime.GetAPIData("events", nil) | ||
for idx, event := range events.([]ctftime.Event) { | ||
fmt.Printf("[event%d]\n", idx) | ||
fmt.Printf("%#v\n", event) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ctftime | ||
|
||
import ( | ||
"log" | ||
) | ||
|
||
type apiClient interface { | ||
GetUrl() string | ||
GetAPIData() interface{} | ||
} | ||
|
||
type apiContext map[string]interface{} | ||
type apiClientFactory func(ctx apiContext) apiClient | ||
|
||
var apiClientFactories = make(map[string]apiClientFactory) | ||
|
||
func registerAPIClient(name string, factory apiClientFactory) { | ||
if factory == nil { | ||
log.Panicf("API Client Factory %s does not exist.", name) | ||
} | ||
|
||
_, registered := apiClientFactories[name] | ||
if registered { | ||
log.Panicf("API Client Factory %s already registered. Ignoring.", name) | ||
} | ||
|
||
apiClientFactories[name] = factory | ||
} | ||
|
||
// この関数はパッケージがimportされた時に呼び出されます | ||
func init() { | ||
registerAPIClient("events", newEventsAPIClient) | ||
} | ||
|
||
func newAPIClient(name string, ctx apiContext) apiClient { | ||
clientFactory, ok := apiClientFactories[name] | ||
if !ok { | ||
log.Panicf("Invalid API Client name!") | ||
} | ||
|
||
return clientFactory(ctx) | ||
} | ||
|
||
func GetUrl(name string, ctx apiContext) string { | ||
client := newAPIClient(name, ctx) | ||
return client.GetUrl() | ||
} | ||
|
||
func GetAPIData(name string, ctx map[string]interface{}) interface{} { | ||
client := newAPIClient(name, ctx) | ||
return client.GetAPIData() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ctftime | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestEventsAPIClient(t *testing.T) { | ||
var client interface{} = newAPIClient("events", nil) | ||
|
||
if valid, ok := client.(apiClient); ok { | ||
valid.GetUrl() | ||
valid.GetAPIData() | ||
} else { | ||
t.Errorf("Invalid Typeof API Client %#v!", client) | ||
} | ||
} | ||
|
||
func TestGetUrl(t *testing.T) { | ||
apiUrl := GetUrl("events", nil) | ||
|
||
_, err := url.Parse(apiUrl) | ||
if err != nil { | ||
t.Errorf("Invalid URL %#v\n", apiUrl) | ||
} | ||
} | ||
|
||
func TestGetAPIData(t *testing.T) { | ||
data := GetAPIData("events", nil) | ||
|
||
body, err := json.Marshal(data) | ||
if err != nil { | ||
t.Errorf("Invalid JSON Format %#v\n", body) | ||
} | ||
|
||
dummy_resp := &http.Response{ | ||
StatusCode: 200, | ||
ProtoMajor: 1, | ||
ProtoMinor: 0, | ||
Body: ioutil.NopCloser(bytes.NewReader(body)), | ||
} | ||
|
||
var dummy_events []Event | ||
httpResponseToStruct(dummy_resp, &dummy_events) | ||
if len(dummy_events) != 3 { | ||
t.Error("Invalid event length!") | ||
} | ||
|
||
for _, event := range dummy_events { | ||
valid := reflect.TypeOf(Event{}) | ||
actual := reflect.TypeOf(event) | ||
if actual != valid { | ||
t.Errorf("Invalid event type of %v! (should be %v).", actual, valid) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package ctftime | ||
|
||
const ( | ||
BUFSIZE = 1024 | ||
URL_PREFIX = "https://ctftime.org/api/v1" // API Endpoint | ||
LIMIT = 3 | ||
BUFSIZE = 1024 | ||
API_ENDPOINT = "https://ctftime.org/api/v1" | ||
test_dir = "./test_data/" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package ctftime | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type eventsAPIClient struct { | ||
Ctx apiContext | ||
} | ||
|
||
func newEventsAPIClient(ctx apiContext) apiClient { | ||
return &eventsAPIClient{ | ||
Ctx: ctx, | ||
} | ||
} | ||
|
||
func (client *eventsAPIClient) GetUrl() string { | ||
now := time.Now().Unix() | ||
|
||
req, err := http.NewRequest("GET", API_ENDPOINT+"/events/", nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
q := req.URL.Query() | ||
q.Add("limit", fmt.Sprintf("%d", LIMIT)) | ||
q.Add("start", strconv.FormatInt(now, 10)) | ||
req.URL.RawQuery = q.Encode() | ||
|
||
return req.URL.String() | ||
} | ||
|
||
func (client *eventsAPIClient) GetAPIData() interface{} { | ||
url := client.GetUrl() | ||
|
||
resp, err := http.Get(url) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
var events []Event | ||
httpResponseToStruct(resp, &events) | ||
return events | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ctftime | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetEventsData(t *testing.T) { | ||
client := newAPIClient("events", nil) | ||
|
||
result := client.GetAPIData() | ||
|
||
body, err := json.Marshal(result) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
dummy_resp := &http.Response{ | ||
StatusCode: 200, | ||
ProtoMajor: 1, | ||
ProtoMinor: 0, | ||
Body: ioutil.NopCloser(bytes.NewReader(body)), | ||
} | ||
|
||
var dummy_events []Event | ||
httpResponseToStruct(dummy_resp, &dummy_events) | ||
if len(dummy_events) != 3 { | ||
t.Error("Invalid event length!") | ||
} | ||
|
||
for _, event := range dummy_events { | ||
valid := reflect.TypeOf(Event{}) | ||
actual := reflect.TypeOf(event) | ||
if actual != valid { | ||
t.Errorf("Invalid event type of %v! (should be %v).", actual, valid) | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters