Skip to content

Commit 98f683e

Browse files
authored
renaming (#46)
* renaming * rename to client
1 parent 201db6c commit 98f683e

22 files changed

+181
-172
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ WebSite docs: https://docs.mixpanel.com/tracking/reference/go
1010
## Getting Started
1111

1212
```go
13-
mp := mixpanel.NewClient("<PROJECT_TOKEN>")
13+
mp := mixpanel.NewApiClient("<PROJECT_TOKEN>")
1414
```

Diff for: examples/alias.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func Alias() error {
1010
ctx := context.Background()
1111

12-
mp := mixpanel.NewClient(
12+
mp := mixpanel.NewApiClient(
1313
"token",
1414
)
1515

Diff for: examples/deleteProfile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func DeleteProfile() error {
1010
ctx := context.Background()
1111

12-
mp := mixpanel.NewClient(
12+
mp := mixpanel.NewApiClient(
1313
"token",
1414
)
1515

Diff for: examples/export.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ func Export() error {
2020
ctx := context.Background()
2121

2222
// fill in your token and project id and service account user name and secret
23-
mp := mixpanel.NewClient(
23+
mp := mixpanel.NewApiClient(
2424
"token",
25-
mixpanel.ProjectID(0),
2625
// Can use either ApiSecret or ServiceAccount
27-
mixpanel.ServiceAccount("user_name", "secret"),
26+
mixpanel.ServiceAccount(0, "user_name", "secret"),
2827
)
2928

3029
startDate, err := parseTime("2023-05-22")

Diff for: examples/import.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ func ImportExample() error {
1313
ctx := context.Background()
1414

1515
// fill in your token and project id and service account user name and secret
16-
mp := mixpanel.NewClient(
16+
mp := mixpanel.NewApiClient(
1717
"token",
18-
// Need to provide project id if you want to use the import api
19-
mixpanel.ProjectID(0),
2018
// Need to provide service account or api secret if you want to use the import api
21-
mixpanel.ServiceAccount("user_name", "secret"),
19+
mixpanel.ServiceAccount(0, "user_name", "secret"),
2220
)
2321

2422
event := mp.NewEvent("import test event", mixpanel.EmptyDistinctID, nil)

Diff for: examples/merge.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func Merge() error {
1010
ctx := context.Background()
1111

12-
mp := mixpanel.NewClient(
12+
mp := mixpanel.NewApiClient(
1313
"token",
1414
// Need to provide api secret if you want to use the merge api
1515
mixpanel.ApiSecret("secret"),

Diff for: examples/peopleAppendListProperty.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func PeopleAppendListProperty() error {
1111
ctx := context.Background()
1212

1313
// fill in your token and project id and service account user name and secret
14-
mp := mixpanel.NewClient(
14+
mp := mixpanel.NewApiClient(
1515
"token",
1616
)
1717

Diff for: examples/peopleIncrement.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func PeopleIncrement() error {
1111
ctx := context.Background()
1212

1313
// fill in your token and project id and service account user name and secret
14-
mp := mixpanel.NewClient(
14+
mp := mixpanel.NewApiClient(
1515
"token",
1616
)
1717

Diff for: examples/peopleRemoveListProperty.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func PeopleRemoveListProperty() error {
1111
ctx := context.Background()
1212

1313
// fill in your token and project id and service account user name and secret
14-
mp := mixpanel.NewClient(
14+
mp := mixpanel.NewApiClient(
1515
"token",
1616
)
1717

Diff for: examples/peopleSet.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func PeopleSet() error {
1111
ctx := context.Background()
1212

1313
// fill in your token and project id and service account user name and secret
14-
mp := mixpanel.NewClient(
14+
mp := mixpanel.NewApiClient(
1515
"token",
1616
)
1717

Diff for: examples/peopleUnionProperty.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func PeopleUnionProperties() error {
1010
ctx := context.Background()
1111

1212
// fill in your token and project id and service account user name and secret
13-
mp := mixpanel.NewClient(
13+
mp := mixpanel.NewApiClient(
1414
"token",
1515
)
1616

Diff for: examples/track.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ func Track() error {
1111
ctx := context.Background()
1212

1313
// fill in your token and project id and service account user name and secret
14-
mp := mixpanel.NewClient(
14+
mp := mixpanel.NewApiClient(
1515
"token",
16-
mixpanel.ProjectID(0),
17-
mixpanel.ServiceAccount("user_name", "secret"),
16+
mixpanel.ServiceAccount(0, "user_name", "secret"),
1817
)
1918

2019
if err := mp.Track(ctx, []*mixpanel.Event{

Diff for: export.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020

2121
// Export calls the Raw Export API
2222
// https://developer.mixpanel.com/reference/raw-event-export
23-
func (m *Mixpanel) Export(ctx context.Context, fromDate, toDate time.Time, limit int, event, where string) ([]*Event, error) {
23+
func (a *ApiClient) Export(ctx context.Context, fromDate, toDate time.Time, limit int, event, where string) ([]*Event, error) {
2424
query := url.Values{}
2525
query.Add("from_date", fromDate.Format("2006-01-02"))
2626
query.Add("to_date", toDate.Format("2006-01-02"))
@@ -34,12 +34,12 @@ func (m *Mixpanel) Export(ctx context.Context, fromDate, toDate time.Time, limit
3434
query.Add("where", where)
3535
}
3636

37-
httpResponse, err := m.doRequestBody(
37+
httpResponse, err := a.doRequestBody(
3838
ctx,
3939
http.MethodGet,
40-
m.dataEndpoint+exportUrl,
40+
a.dataEndpoint+exportUrl,
4141
nil,
42-
m.exportServiceAccount(), acceptPlainText(), addQueryParams(query),
42+
a.exportServiceAccount(), acceptPlainText(), addQueryParams(query),
4343
)
4444
if err != nil {
4545
return nil, err

Diff for: export_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestExport(t *testing.T) {
4444
}, nil
4545
})
4646

47-
mp := NewClient("token", ProjectID(117), ServiceAccount("username", "secret"))
47+
mp := NewApiClient("token", ServiceAccount(117, "username", "secret"))
4848
events, err := mp.Export(ctx, parseDate(t, "2023-01-01"), parseDate(t, "2023-01-02"), ExportNoLimit, ExportNoEventFilter, ExportNoWhereFilter)
4949
require.NoError(t, err)
5050

@@ -69,7 +69,7 @@ func TestExport(t *testing.T) {
6969
}, nil
7070
})
7171

72-
mp := NewClient("token", ProjectID(117), ServiceAccount("username", "secret"))
72+
mp := NewApiClient("token", ServiceAccount(117, "username", "secret"))
7373
_, err := mp.Export(ctx, parseDate(t, "2023-01-01"), parseDate(t, "2023-01-02"), ExportNoLimit, ExportNoEventFilter, ExportNoWhereFilter)
7474
require.NoError(t, err)
7575
})
@@ -91,7 +91,7 @@ func TestExport(t *testing.T) {
9191
}, nil
9292
})
9393

94-
mp := NewClient("token")
94+
mp := NewApiClient("token")
9595
_, err := mp.Export(ctx, parseDate(t, "2023-01-01"), parseDate(t, "2023-01-02"), ExportNoLimit, ExportNoEventFilter, ExportNoWhereFilter)
9696
require.NoError(t, err)
9797
})

Diff for: http.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,26 @@ func applicationFormData() httpOptions {
7070
}
7171
}
7272

73-
func (m *Mixpanel) useServiceAccountOrProjectSecret() httpOptions {
73+
func (m *ApiClient) useServiceAccountOrProjectSecret() httpOptions {
7474
return func(req *http.Request) {
7575
if m.serviceAccount != nil {
7676
req.SetBasicAuth(m.serviceAccount.Username, m.serviceAccount.Secret)
77+
7778
} else {
7879
req.SetBasicAuth(m.apiSecret, "")
7980
}
8081
}
8182
}
8283

83-
func (m *Mixpanel) useApiSecret() httpOptions {
84+
func (m *ApiClient) useApiSecret() httpOptions {
8485
return func(req *http.Request) {
8586
req.SetBasicAuth(m.apiSecret, "")
8687
}
8788
}
8889

8990
// exportServiceAccount uses the service account if available and adds the query params
9091
// or falls back to apiSecret
91-
func (m *Mixpanel) exportServiceAccount() httpOptions {
92+
func (m *ApiClient) exportServiceAccount() httpOptions {
9293
return func(req *http.Request) {
9394
if m.serviceAccount != nil {
9495
req.SetBasicAuth(m.serviceAccount.Username, m.serviceAccount.Secret)
@@ -213,7 +214,7 @@ func requestForm(jsonPayload []byte) (*bytes.Reader, error) {
213214
return bytes.NewReader([]byte(form.Encode())), nil
214215
}
215216

216-
func (m *Mixpanel) doRequestBody(
217+
func (m *ApiClient) doRequestBody(
217218
ctx context.Context,
218219
method string,
219220
requestUrl string,
@@ -241,7 +242,7 @@ func (m *Mixpanel) doRequestBody(
241242
return m.client.Do(request)
242243
}
243244

244-
func (m *Mixpanel) doPeopleRequest(ctx context.Context, body any, u string) error {
245+
func (m *ApiClient) doPeopleRequest(ctx context.Context, body any, u string) error {
245246
requestBody, err := makeRequestBody(body, jsonPayload, None)
246247
if err != nil {
247248
return fmt.Errorf("failed to create request body: %w", err)
@@ -263,7 +264,7 @@ func (m *Mixpanel) doPeopleRequest(ctx context.Context, body any, u string) erro
263264
return processPeopleRequestResponse(response)
264265
}
265266

266-
func (m *Mixpanel) doIdentifyRequest(ctx context.Context, body any, u string, option ...httpOptions) error {
267+
func (m *ApiClient) doIdentifyRequest(ctx context.Context, body any, u string, option ...httpOptions) error {
267268
requestBody, err := makeRequestBody(body, formPayload, None)
268269
if err != nil {
269270
return fmt.Errorf("failed to create request body: %w", err)

Diff for: http_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ func TestHttpError(t *testing.T) {
5050
}
5151

5252
func TestProcessPeopleRequestResponse(t *testing.T) {
53+
t.Run("http 200 but code 0", func(t *testing.T) {
54+
response := &http.Response{
55+
StatusCode: http.StatusOK,
56+
Body: io.NopCloser(strings.NewReader(`
57+
0
58+
`)),
59+
}
60+
61+
err := processPeopleRequestResponse(response)
62+
require.Error(t, err)
63+
})
64+
5365
t.Run("StatusUnauthorized", func(t *testing.T) {
5466
response := &http.Response{
5567
StatusCode: http.StatusUnauthorized,

Diff for: identity.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ type aliasProperties struct {
1919
}
2020

2121
// https://developer.mixpanel.com/reference/identity-create-alias
22-
func (m *Mixpanel) Alias(ctx context.Context, aliasID, distinctID string) error {
22+
func (a *ApiClient) Alias(ctx context.Context, aliasID, distinctID string) error {
2323
payload := &aliasPayload{
2424
Event: "$create_alias",
2525
Properties: aliasProperties{
2626
DistinctId: distinctID,
2727
Alias: aliasID,
28-
Token: m.token,
28+
Token: a.token,
2929
},
3030
}
3131

32-
return m.doIdentifyRequest(ctx, payload, aliasEndpoint)
32+
return a.doIdentifyRequest(ctx, payload, aliasEndpoint)
3333
}
3434

3535
type mergePayload struct {
@@ -43,13 +43,13 @@ type mergeProperties struct {
4343

4444
// https://developer.mixpanel.com/reference/identity-merge
4545
// must provide api secret
46-
func (m *Mixpanel) Merge(ctx context.Context, distinctID1, distinctID2 string) error {
46+
func (a *ApiClient) Merge(ctx context.Context, distinctID1, distinctID2 string) error {
4747
payload := &mergePayload{
4848
Event: "$merge",
4949
Properties: mergeProperties{
5050
DistinctId: []string{distinctID1, distinctID2},
5151
},
5252
}
5353

54-
return m.doIdentifyRequest(ctx, payload, mergeEndpoint, m.useApiSecret())
54+
return a.doIdentifyRequest(ctx, payload, mergeEndpoint, a.useApiSecret())
5555
}

Diff for: identity_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/stretchr/testify/require"
1515
)
1616

17-
func setupIdentityEndpoint(t *testing.T, client *Mixpanel, endpoint string, testReq func(req *http.Request), testPayload func(body io.Reader), httpResponse *http.Response) {
17+
func setupIdentityEndpoint(t *testing.T, client *ApiClient, endpoint string, testReq func(req *http.Request), testPayload func(body io.Reader), httpResponse *http.Response) {
1818
httpmock.Activate()
1919
t.Cleanup(httpmock.DeactivateAndReset)
2020

@@ -33,7 +33,7 @@ func setupIdentityEndpoint(t *testing.T, client *Mixpanel, endpoint string, test
3333
func TestAlias(t *testing.T) {
3434
ctx := context.Background()
3535

36-
mp := NewClient("token")
36+
mp := NewApiClient("token")
3737
setupIdentityEndpoint(t, mp, aliasEndpoint, func(req *http.Request) {}, func(body io.Reader) {
3838
payload := &aliasPayload{}
3939
require.NoError(t, json.NewDecoder(body).Decode(&payload))
@@ -54,7 +54,7 @@ func TestAlias(t *testing.T) {
5454
func TestMerge(t *testing.T) {
5555
ctx := context.Background()
5656

57-
mp := NewClient("token")
57+
mp := NewApiClient("token")
5858
setupIdentityEndpoint(t, mp, mergeEndpoint, func(req *http.Request) {
5959
auth := req.Header.Get("authorization")
6060
require.Equal(t, auth, "Basic "+base64.StdEncoding.EncodeToString([]byte(mp.apiSecret+":")))

0 commit comments

Comments
 (0)