@@ -33,6 +33,11 @@ type RegistrationResponse struct {
3333 PublicKey string `json:"public_key"`
3434}
3535
36+ type TeslaAPIResponse struct {
37+ Response * RegistrationResponse `json:"response"`
38+ Error string `json:"error"`
39+ }
40+
3641func generateToken () (* TokenResponse , error ) {
3742 data := url.Values {}
3843 data .Set ("grant_type" , "client_credentials" )
@@ -56,16 +61,28 @@ func generateToken() (*TokenResponse, error) {
5661}
5762
5863func registerPartnerAccount (token string ) (* RegistrationResponse , error ) {
64+ // Clean the domain - remove any https:// prefix and ensure lowercase
5965 domain := strings .TrimSpace (string (domainName ))
66+ domain = strings .ToLower (domain )
67+ domain = strings .TrimPrefix (domain , "https://" )
68+ domain = strings .TrimPrefix (domain , "http://" )
69+ domain = strings .TrimSuffix (domain , "/" )
70+
71+ log .Printf ("Using domain for registration: %s" , domain )
72+
6073 payload := map [string ]string {
61- "domain" : domain ,
74+ "domain" : domain ,
75+ "name" : "Tesla Fleet Telemetry Operator" ,
76+ "description" : "Fleet telemetry data ingestion service" ,
6277 }
6378
6479 jsonPayload , err := json .Marshal (payload )
6580 if err != nil {
6681 return nil , fmt .Errorf ("error marshaling payload: %v" , err )
6782 }
6883
84+ log .Printf ("Registration payload: %s" , string (jsonPayload ))
85+
6986 req , err := http .NewRequest ("POST" ,
7087 "https://fleet-api.prd.na.vn.cloud.tesla.com/api/1/partner_accounts" ,
7188 bytes .NewBuffer (jsonPayload ))
@@ -88,16 +105,23 @@ func registerPartnerAccount(token string) (*RegistrationResponse, error) {
88105 return nil , fmt .Errorf ("error reading response: %v" , err )
89106 }
90107
91- if resp .StatusCode != http .StatusOK {
92- return nil , fmt .Errorf ("registration failed with status %d: %s" , resp .StatusCode , string (body ))
93- }
108+ log .Printf ("Registration response status: %d" , resp .StatusCode )
109+ log .Printf ("Registration response body: %s" , string (body ))
94110
95- var regResp RegistrationResponse
96- if err := json .Unmarshal (body , & regResp ); err != nil {
111+ var teslaResp TeslaAPIResponse
112+ if err := json .Unmarshal (body , & teslaResp ); err != nil {
97113 return nil , fmt .Errorf ("error parsing response: %v" , err )
98114 }
99115
100- return & regResp , nil
116+ if resp .StatusCode != http .StatusOK && resp .StatusCode != http .StatusCreated {
117+ return nil , fmt .Errorf ("registration failed with status %d: %s" , resp .StatusCode , teslaResp .Error )
118+ }
119+
120+ if teslaResp .Response == nil {
121+ return nil , fmt .Errorf ("empty response from Tesla API" )
122+ }
123+
124+ return teslaResp .Response , nil
101125}
102126
103127var (
0 commit comments