Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Fix message scheduling

Acho Arnoldb4c11613f551
diff --git a/api/cmd/loadtest/main.go b/api/cmd/loadtest/main.go
index 8d419185..89c2a80d 100644
--- a/api/cmd/loadtest/main.go
+++ b/api/cmd/loadtest/main.go
@@ -1,41 +1,33 @@
package main
import (
- "bytes"
- "encoding/json"
+ "context"
"fmt"
- "io/ioutil"
"log"
- "net/http"
+ "github.com/carlmjohnson/requests"
"github.com/palantir/stacktrace"
)
func main() {
- client := http.Client{}
- for i := 0; i < 100; i++ {
- payload, err := json.Marshal(map[string]string{
- "content": fmt.Sprintf("testing http api sample: [%d]", i),
- "from": "+37259139660",
- "to": "+37253517181",
- })
+ for i := 0; i < 50; i++ {
+ var responsePayload string
+ err := requests.
+ URL("/v1/messages/send").
+ Host("localhost:8000").
+ Scheme("http").
+ Header("x-api-key", "Uv38ByGCZU8WP18PmmIdcpVmx00QA3xNe7sEB9HixkmBhVrYaB0NhtHpHgAWeTnL").
+ BodyJSON(&map[string]string{
+ "content": fmt.Sprintf("testing http api sample: [%d]", i),
+ "from": "+37259139660",
+ "to": "+37253517181",
+ }).
+ ToString(&responsePayload).
+ Fetch(context.Background())
if err != nil {
log.Fatal(stacktrace.Propagate(err, "cannot create json payload"))
}
- response, err := client.Post("https://api.httpsms.com/v1/messages/send", "application/json", bytes.NewBuffer(payload))
- if err != nil {
- log.Fatal(stacktrace.Propagate(err, "cannot perform http request"))
- }
-
- if response.StatusCode != http.StatusOK {
- log.Fatal(stacktrace.NewError(fmt.Sprintf("status code [%d] is different from expected [%d]", response.StatusCode, http.StatusOK)))
- }
-
- responsePayload, err := ioutil.ReadAll(response.Body)
- if err != nil {
- log.Fatal(stacktrace.NewError(fmt.Sprintf("cannot read response")))
- }
- log.Println(string(responsePayload))
+ log.Println(responsePayload)
}
}
diff --git a/api/go.mod b/api/go.mod
index 4507ff5c..36eeb3a5 100644
--- a/api/go.mod
+++ b/api/go.mod
@@ -7,6 +7,7 @@ require (
firebase.google.com/go v3.13.0+incompatible
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.8.3
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
+ github.com/carlmjohnson/requests v0.22.3
github.com/cheggaaa/pb/v3 v3.0.8
github.com/cloudevents/sdk-go/v2 v2.10.0
github.com/cockroachdb/cockroach-go/v2 v2.2.11
diff --git a/api/go.sum b/api/go.sum
index 7abf1cc4..a079707a 100644
--- a/api/go.sum
+++ b/api/go.sum
@@ -91,6 +91,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
+github.com/carlmjohnson/requests v0.22.3 h1:ip16AKXNYuArdw9L5/1mL+mNorlZO5XhkLg617yOumc=
+github.com/carlmjohnson/requests v0.22.3/go.mod h1:iTsaX9TdFg2+L4WtZO/HFyDMPEfBnogV3i4A4gjDnvs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
diff --git a/api/pkg/repositories/gorm_phone_notification_repository.go b/api/pkg/repositories/gorm_phone_notification_repository.go
index 86dad2d2..c8d19e48 100644
--- a/api/pkg/repositories/gorm_phone_notification_repository.go
+++ b/api/pkg/repositories/gorm_phone_notification_repository.go
@@ -65,6 +65,7 @@ func (repository gormPhoneNotificationRepository) Schedule(ctx context.Context,
err := crdbgorm.ExecuteTx(ctx, repository.db, nil, func(tx *gorm.DB) error {
var messagesCount int64
err := tx.WithContext(ctx).
+ Model(&entities.PhoneNotification{}).
Where("phone_id = ?", notification.PhoneID).
Where("status = ?", entities.PhoneNotificationStatusPending).
Count(&messagesCount).