-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
236 additions
and
98 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,40 @@ | ||
package api | ||
|
||
import ( | ||
"NEOs/configs" | ||
"io" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
type Body struct { | ||
Body []byte | ||
Err error | ||
} | ||
|
||
func GetAPI(link ...string) *http.Response { | ||
// Read Configs | ||
configs := configs.ReadConfigs() | ||
|
||
// Send GET request | ||
if link != nil { | ||
configs.Link = link[0] | ||
} | ||
resp, err := http.Get(configs.Link) | ||
|
||
if err != nil { | ||
log.Fatalf("No response from request") | ||
} | ||
return resp | ||
} | ||
|
||
func GetResp() *Body { | ||
|
||
// Read Response | ||
body, err := io.ReadAll(GetAPI().Body) | ||
if err != nil { | ||
log.Fatalf("Unable to read a JSON due to %s", err) | ||
} | ||
|
||
return &Body{body, err} | ||
} |
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,24 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type APISuite struct { | ||
suite.Suite | ||
response *http.Response | ||
} | ||
|
||
func (suite *APISuite) TestGetAPI() { | ||
link := "https://api.nasa.gov/neo/rest/v1/feed?start_date=2015-09-07&end_date=2015-09-08&api_key=DEMO_KEY" | ||
|
||
suite.response = GetAPI(link) | ||
suite.Equal(suite.response.StatusCode, http.StatusOK) | ||
} | ||
|
||
func TestGetAPISuite(t *testing.T) { | ||
suite.Run(t, new(APISuite)) | ||
} |
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,102 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"NEOs/configs" | ||
"encoding/json" | ||
"NEOs/api" | ||
"NEOs/pkg/parser" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
type Response struct { | ||
Total interface{} `json:"element_count"` | ||
Near_earth_objects Near_earth_objects `json:"near_earth_objects"` | ||
} | ||
|
||
type Near_earth_objects []struct { | ||
Date interface{} `json:"date"` | ||
ID interface{} `json:"id"` | ||
Name interface{} `json:"name"` | ||
Check interface{} `json:"is_potentially_hazardous_asteroid"` | ||
} | ||
|
||
func NewResponse(total interface{}, near_earth_objects Near_earth_objects) *Response { | ||
return &Response{ | ||
Total: total, | ||
Near_earth_objects: near_earth_objects, | ||
} | ||
} | ||
|
||
var data map[string]interface{} | ||
"github.com/joho/godotenv" | ||
) | ||
|
||
func main() { | ||
|
||
// Read Configs | ||
configs := configs.ReadConfigs() | ||
|
||
// Send GET request | ||
resp, err := http.Get(configs.Link) | ||
|
||
if err != nil { | ||
fmt.Println("No response from request") | ||
} | ||
|
||
// Read Response | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatalf("Unable to read a JSON due to %s", err) | ||
} | ||
err := godotenv.Load(".env") | ||
|
||
// Decode a Json | ||
err = json.Unmarshal([]byte(body), &data) | ||
if err != nil { | ||
log.Fatalf("Unable to marshal JSON due to %s", err) | ||
log.Fatalf("Failed to read .env: %v", err) | ||
} | ||
|
||
// Assigning a Total value | ||
result := NewResponse(data["element_count"], Near_earth_objects{}) | ||
|
||
// Creating a Wait Group and Mutex | ||
wg := sync.WaitGroup{} | ||
mutex := sync.Mutex{} | ||
|
||
// Iteration through data | ||
for date, value := range data["near_earth_objects"].(map[string]interface{}) { | ||
var id, name, check interface{} | ||
wg.Add(1) | ||
|
||
// Concurrent data fetching for each day | ||
go getDay(&id, &name, &check, value, &date, &wg, &mutex, result) | ||
} | ||
wg.Wait() | ||
fmt.Println(string(parser.Parser(api.GetResp()))) | ||
|
||
// Returning a result | ||
jsonResult, _ := json.MarshalIndent(result, "", "\t") | ||
fmt.Println(string(jsonResult)) | ||
} | ||
|
||
// Assigning received data to Response object | ||
func addResponse(date *string, id, name, check *interface{}, wg *sync.WaitGroup, result *Response) { | ||
result.Near_earth_objects = append(result.Near_earth_objects, struct { | ||
Date interface{} "json:\"date\"" | ||
ID interface{} "json:\"id\"" | ||
Name interface{} "json:\"name\"" | ||
Check interface{} "json:\"is_potentially_hazardous_asteroid\"" | ||
}{date, id, name, check}) | ||
} | ||
|
||
// Fetching data from one day | ||
func getDay(id, name, check *interface{}, value interface{}, date *string, wg *sync.WaitGroup, mutex *sync.Mutex, result *Response) { | ||
mutex.Lock() | ||
for _, element := range value.([]interface{}) { | ||
*id = element.(map[string]interface{})["id"] | ||
*name = element.(map[string]interface{})["name"] | ||
*check = element.(map[string]interface{})["is_potentially_hazardous_asteroid"] | ||
|
||
addResponse(date, id, name, check, wg, result) | ||
} | ||
mutex.Unlock() | ||
wg.Done() | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
API_KEY="your_api_key" | ||
DEMO_API_KEY="DEMO_KEY" | ||
BaseLink="https://api.nasa.gov/neo/rest/v1/feed?start_date=%s&end_date=%s&api_key=%s" |
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
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,82 @@ | ||
package parser | ||
|
||
import ( | ||
"NEOs/api" | ||
"encoding/json" | ||
"log" | ||
"sync" | ||
) | ||
|
||
type Neo interface { | ||
AddNeo(obj Near_earth_objects) | ||
} | ||
|
||
type Response struct { | ||
Total interface{} `json:"element_count"` | ||
Near_earth_objects []Near_earth_objects `json:"near_earth_objects"` | ||
} | ||
|
||
type Near_earth_objects struct { | ||
Date interface{} `json:"date"` | ||
ID interface{} `json:"id"` | ||
Name interface{} `json:"name"` | ||
Check interface{} `json:"is_potentially_hazardous_asteroid"` | ||
} | ||
|
||
func NewResponse(total interface{}, near_earth_objects []Near_earth_objects) *Response { | ||
return &Response{ | ||
Total: total, | ||
Near_earth_objects: near_earth_objects, | ||
} | ||
} | ||
|
||
func (res *Response) AddNeo(obj Near_earth_objects) []Near_earth_objects { | ||
res.Near_earth_objects = append(res.Near_earth_objects, obj) | ||
return res.Near_earth_objects | ||
} | ||
|
||
func Parser(body *api.Body) []byte { | ||
|
||
// Decode a Json | ||
var inputJson map[string]interface{} | ||
|
||
body.Err = json.Unmarshal([]byte(body.Body), &inputJson) | ||
if body.Err != nil { | ||
log.Fatalf("Unable to marshal JSON due to %s", body.Err) | ||
} | ||
|
||
// Assigning a Total value | ||
response := NewResponse(inputJson["element_count"], []Near_earth_objects{}) | ||
|
||
// Creating a Wait Group and Mutex | ||
wg := sync.WaitGroup{} | ||
mutex := sync.Mutex{} | ||
|
||
// Iteration through data | ||
for date, value := range inputJson["near_earth_objects"].(map[string]interface{}) { | ||
wg.Add(1) | ||
|
||
// Concurrent data fetching for each day | ||
go getDay(value, &date, &wg, &mutex, response) | ||
} | ||
wg.Wait() | ||
|
||
// Returning a result | ||
jsonResponse, _ := json.MarshalIndent(response, "", "\t") | ||
return jsonResponse | ||
} | ||
|
||
// Fetching data from one day | ||
func getDay(value interface{}, date *string, wg *sync.WaitGroup, mut *sync.Mutex, res *Response) { | ||
mut.Lock() | ||
for _, element := range value.([]interface{}) { | ||
id := element.(map[string]interface{})["id"] | ||
name := element.(map[string]interface{})["name"] | ||
check := element.(map[string]interface{})["is_potentially_hazardous_asteroid"] | ||
|
||
neo := Near_earth_objects{Date: date, ID: id, Name: name, Check: check} | ||
res.AddNeo(neo) | ||
} | ||
mut.Unlock() | ||
wg.Done() | ||
} |
Oops, something went wrong.