Skip to content

Commit

Permalink
Merge pull request #5 from AllanCapistrano/feat/limit-messages
Browse files Browse the repository at this point in the history
Feat/limit messages
  • Loading branch information
AllanCapistrano authored Dec 3, 2023
2 parents 74c5539 + 3820dbe commit d4f2542
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ We recommend you update Go [to the latest stable version to use the library](htt
| GET | `/nodeInfo/all` | Shows all information about Tangle Hornet Network. |
| GET | `/message/messageId/{messageId}` | Get a message by given message ID. |
| GET | `/message/{index}` | Get all messages using a specific index. |
| GET | `/message/{index}/{maxMessages}` | Get a maximum number of messages from the last hour, using a specific index. |
| POST | `/message` | Create and submit a new message. |

You also can use the files in the [`apiClientFiles`](./apiClientFiles/) directory to import all routes into your API Client.

### POST Body Examples

#### Routes
Expand Down
1 change: 1 addition & 0 deletions apiClientFiles/Insomnia_2023-12-03.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_type":"export","__export_format":4,"__export_date":"2023-12-03T20:30:36.107Z","__export_source":"insomnia.desktop.app:v2023.5.8","resources":[{"_id":"req_c86d21b145eb4ee0a8d1f3ac54573d52","parentId":"fld_de8ac77b66964dadb68a58aad5c4303c","modified":1697461797662,"created":1695579416492,"url":"{{ _.localhost }}/nodeInfo","name":"Get Node Info","description":"","method":"GET","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"Insomnia/2023.5.7"}],"authentication":{},"metaSortKey":-1697461797618,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"fld_de8ac77b66964dadb68a58aad5c4303c","parentId":"wrk_235b56b7308b4b258983dc1cb3b1935e","modified":1697461790739,"created":1697461790739,"name":"Node Info","description":"","environment":{},"environmentPropertyOrder":null,"metaSortKey":-1697461790739,"_type":"request_group"},{"_id":"wrk_235b56b7308b4b258983dc1cb3b1935e","parentId":null,"modified":1700484287754,"created":1700484287754,"name":"Tangle Hornet API","description":"","scope":"collection","_type":"workspace"},{"_id":"req_701159a32bc54f35921598b2acca4c6a","parentId":"fld_de8ac77b66964dadb68a58aad5c4303c","modified":1697462029450,"created":1697462026677,"url":"{{ _.localhost }}/nodeInfo/all","name":"Get All Node Info","description":"","method":"GET","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"Insomnia/2023.5.7"}],"authentication":{},"metaSortKey":-1696520632526.5,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_504b0d1781f44dd2bb46d15732a1d9bf","parentId":"fld_a1095ff3c99f438c8bb1251c9e8dea17","modified":1696168542625,"created":1696168512961,"url":"{{ _.localhost }}/message/messageId/1ce1acc6b9d6fc82713cac49356fd693d9aec070ea20a8b671ace6416477962f","name":"Get Message By MessageID","description":"","method":"GET","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"Insomnia/2023.5.7"}],"authentication":{},"metaSortKey":-1695579467435,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"fld_a1095ff3c99f438c8bb1251c9e8dea17","parentId":"wrk_235b56b7308b4b258983dc1cb3b1935e","modified":1698755389021,"created":1695579461090,"name":"Message","description":"","environment":{},"environmentPropertyOrder":null,"metaSortKey":-1696991208326.75,"_type":"request_group"},{"_id":"req_790a811f54f44d2bb089ddd5618c9d7a","parentId":"fld_a1095ff3c99f438c8bb1251c9e8dea17","modified":1701635387228,"created":1695579464156,"url":"{{ _.localhost }}/message/test","name":"Get All Messages By Index","description":"","method":"GET","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"Insomnia/2023.5.7"}],"authentication":{},"metaSortKey":-1695579467335,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_7b88d5a3fa1d46e09b608d71e96beb9c","parentId":"fld_a1095ff3c99f438c8bb1251c9e8dea17","modified":1701635393010,"created":1701634479779,"url":"{{ _.localhost }}/message/test/2","name":"Get Last Hour Messages By Index","description":"","method":"GET","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"Insomnia/2023.5.7"}],"authentication":{},"metaSortKey":-1695579467285,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_dac032b9a4cc4f95a1696c5d8fe6bc61","parentId":"fld_a1095ff3c99f438c8bb1251c9e8dea17","modified":1701635402628,"created":1695579513166,"url":"{{ _.localhost }}/message","name":"Create Message","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"index\": \"test\",\n\t\"data\": {\"createdAt\":1701634693830,\"group\":\"cloud/c1\",\"publishedAt\":1701634693830,\"source\":\"e0635c77-effe-4d57-9ea6-7d5c4e63ff28\",\"target\":\"92015a2d-4bae-428d-a428-6338f465e72c\",\"type\":\"REP_EVALUATION\",\"value\":1}\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"},{"name":"User-Agent","value":"Insomnia/2023.5.7"}],"authentication":{},"metaSortKey":-1695579467235,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_f16e5f6e172f4c268da7fce6dd376e39","parentId":"fld_fc023edd614645158f9c567696790bde","modified":1698758539111,"created":1698758530249,"url":"{{ _.localhost }}/message/REP_SVC_HUMIDITY_SENSOR","name":"Get Humidity Sensor Service","description":"","method":"GET","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"Insomnia/2023.5.7"}],"authentication":{},"metaSortKey":-1698108590717.5,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"fld_fc023edd614645158f9c567696790bde","parentId":"wrk_235b56b7308b4b258983dc1cb3b1935e","modified":1698755387366,"created":1698755375764,"name":"Node Services","description":"","environment":{},"environmentPropertyOrder":null,"metaSortKey":-1696520625914.5,"_type":"request_group"},{"_id":"req_d51c1833a23e453ebbbca44ca1f74af7","parentId":"fld_fc023edd614645158f9c567696790bde","modified":1698758605428,"created":1698758597747,"url":"{{ _.localhost }}/message/REP_SVC_PULSE_OXYMETER","name":"Get Pulse Oximeter Service","description":"","method":"GET","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"Insomnia/2023.5.7"}],"authentication":{},"metaSortKey":-1697946892442.625,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_895bd823fa2845a2a7829b47206f079b","parentId":"fld_fc023edd614645158f9c567696790bde","modified":1698758612360,"created":1698758555613,"url":"{{ _.localhost }}/message/REP_SVC_THERMOMETER","name":"Get Thermometer Service","description":"","method":"GET","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"Insomnia/2023.5.7"}],"authentication":{},"metaSortKey":-1697906467873.9062,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_ebc6221282234c5fb9f5e1c85e4a52c0","parentId":"fld_fc023edd614645158f9c567696790bde","modified":1698758610746,"created":1698333319365,"url":"{{ _.localhost }}/message/REP_SVC_WIND_DIRECTION_SENSOR","name":"Get Wind Direction Sensor Service","description":"","method":"GET","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"Insomnia/2023.5.7"}],"authentication":{},"metaSortKey":-1697866043305.1875,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"env_9b8da4e88aa841f3aeb09fb913ea6f8d","parentId":"wrk_235b56b7308b4b258983dc1cb3b1935e","modified":1698862282299,"created":1695579403722,"name":"Base Environment","data":{"localhost":"http://localhost:3000"},"dataPropertyOrder":{"&":["localhost"]},"color":null,"isPrivate":false,"metaSortKey":1695579403722,"_type":"environment"},{"_id":"jar_a206d2c3e6b3447fa9a185f3bb4a458e","parentId":"wrk_235b56b7308b4b258983dc1cb3b1935e","modified":1695579403724,"created":1695579403724,"name":"Default Jar","cookies":[],"_type":"cookie_jar"}]}
42 changes: 42 additions & 0 deletions endpoints/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"net/http"
"strconv"

"github.com/allancapistrano/tangle-client-go/messages"
"github.com/allancapistrano/tangle-hornet-api/config"
Expand Down Expand Up @@ -46,6 +47,47 @@ func GetAllMessagesByIndex(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer, jsonInString)
}

// Get a limited amount of messages created in the last hour, available on the
// node by a given index.
func GetLastHourMessagesByIndex(writer http.ResponseWriter, request *http.Request) {
var jsonInString string
nodeURL := config.GetNodeUrl(CONFIG_FILE_NAME, true)
nodePort := config.GetNodePort(CONFIG_FILE_NAME, true)
nodeAddress := fmt.Sprintf("http://%s:%s", nodeURL, nodePort)

vars := mux.Vars(request)
index := vars["index"]
maxMessagesInString := vars["maxMessages"]

maxMessages, err := strconv.Atoi(maxMessagesInString)
if err != nil {
jsonInString = "{\"error\": \"Invalid maximum number of messages.\"}"
http.Error(writer, jsonInString, http.StatusBadRequest)
}

lastHourMessagesByIndex, err := messages.GetLastHourMessagesByIndex(
nodeAddress,
index,
maxMessages,
)

if err != nil {
jsonInString = fmt.Sprintf("{\"error\": \"%s\"}", err.Error())
http.Error(writer, jsonInString, http.StatusInternalServerError)
} else {
jsonInBytes, err := json.Marshal(lastHourMessagesByIndex)

if err != nil {
jsonInString = "{\"error\": \"Unable to convert the messages struct into JSON format.\"}"
http.Error(writer, jsonInString, http.StatusInternalServerError)
} else {
jsonInString = string(jsonInBytes)
}
}

fmt.Fprint(writer, jsonInString)
}

// Get a message by given message ID.
func GetMessageByMessageId(writer http.ResponseWriter, request *http.Request) {
var jsonInString string
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/allancapistrano/tangle-hornet-api
go 1.20

require (
github.com/allancapistrano/tangle-client-go v1.4.0
github.com/allancapistrano/tangle-client-go v1.5.0
github.com/gorilla/mux v1.8.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/Joker/jade v1.0.1-0.20190614124447-d475f43051e7/go.mod h1:6E6s8o2AE4K
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/allancapistrano/tangle-client-go v1.4.0 h1:elTcsvSFkrXZCsfNS3GQxhOAx0nWF3hjSr81ev+ck5c=
github.com/allancapistrano/tangle-client-go v1.4.0/go.mod h1:AwPSVwcfCVDDXdLQIM//9s9AHL7EOOn/LOxbk+0Sq80=
github.com/allancapistrano/tangle-client-go v1.5.0 h1:3ng1B23YtnTVK74IxFySROygVQxUGhkfMXnbmr/H9RY=
github.com/allancapistrano/tangle-client-go v1.5.0/go.mod h1:AwPSVwcfCVDDXdLQIM//9s9AHL7EOOn/LOxbk+0Sq80=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down
4 changes: 4 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func Routes() (router *mux.Router) {
// Messages
router.HandleFunc("/message", endpoints.CreateNewMessage).Methods("POST")
router.HandleFunc("/message/{index}", endpoints.GetAllMessagesByIndex)
router.HandleFunc(
"/message/{index}/{maxMessages:[0-9]+}",
endpoints.GetLastHourMessagesByIndex,
)
router.HandleFunc("/message/messageId/{messageID}", endpoints.GetMessageByMessageId)

return router
Expand Down

0 comments on commit d4f2542

Please sign in to comment.