Skip to content

Commit

Permalink
feat: sessions implementation in ocpi WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraBassani committed Nov 13, 2023
1 parent d6e8355 commit 3418e6e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
42 changes: 42 additions & 0 deletions manager/ocpi/ocpi.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Api interface {
SetToken(ctx context.Context, token Token) error
GetToken(ctx context.Context, countryCode string, partyID string, tokenUID string) (*Token, error)
PushLocation(ctx context.Context, location Location) error
PushSession(ctx context.Context, token store.Token) error
}

type OCPI struct {
Expand Down Expand Up @@ -259,3 +260,44 @@ func (o *OCPI) setRequestHeaders(ctx context.Context, req *http.Request, token s
req.Header.Set("OCPI-to-country-code", toCountryCode)
req.Header.Set("OCPI-to-party-id", toPartyId)
}
func (o *OCPI) getSessionsUrl(endpoints []Endpoint) (string, error) {
for _, endpoint := range endpoints {
if endpoint.Identifier == "sessions" && endpoint.Role == RECEIVER {
return fmt.Sprintf("%s/%s/%s", endpoint.Url, o.countryCode, o.partyId), nil
}
}
return "", errors.New("no locations endpoint for receiver found")
}

func (o *OCPI) PushSession(ctx context.Context, token store.Token) error {
// will post the session to the emsp
// we need partyId and CountryCode (how do we map chargeStationId with the emps that will need this info).
// we'll use these to look up the party url and the api token
// look at the token module to see if we can map the partyCode and CountryCode
//generate a new sessionId
session := new(Session)
b, err := json.Marshal(session)
if err != nil {
return err
}
url := ""
req, err := http.NewRequestWithContext(ctx, http.MethodPut, fmt.Sprintf("%s/%s", url, session.Id), bytes.NewReader(b))
if err != nil {
return err
}
o.setRequestHeaders(ctx, req, token.Uid, token.CountryCode, token.PartyId)

resp, err := o.httpClient.Do(req)
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()

if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("status code: %d", resp.StatusCode)
}

return nil
}
46 changes: 46 additions & 0 deletions manager/ocpi/ocpi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,49 @@ func TestPushLocation(t *testing.T) {

require.NoError(t, err)
}

func TestPushSession(t *testing.T) {
engine := inmemory.NewStore(clock.RealClock{})
ocpiApi := ocpi.NewOCPI(engine, http.DefaultClient, "GB", "TWK")
mux := http.NewServeMux()
receiverServer := httptest.NewServer(mux)
defer receiverServer.Close()
mux.HandleFunc("/ocpi/versions", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf(`{"data":[{"version":"2.2","url":"%s/ocpi/2.2"}], "status_code":1000}`, receiverServer.URL)))
})
mux.HandleFunc("/ocpi/2.2", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf(`{"data":{
"version":"2.2",
"endpoints":[{"identifier":"locations","role":"RECEIVER","url":"%s/ocpi/receiver/2.2/locations"}]},
"status_code":1000}`,
receiverServer.URL)))
})
mux.HandleFunc("/ocpi/receiver/2.2/sessions/GB/TWK/s001", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method)
assert.Equal(t, r.Header.Get("X-Correlation-ID"), "some-correlation-id")
var got []byte
r.Body.Read(got)
assert.Equal(t, []byte(nil), got)
w.WriteHeader(http.StatusCreated)
})
err := ocpiApi.SetCredentials(context.Background(), "some-token-123", ocpi.Credentials{
Roles: []ocpi.CredentialsRole{
{
CountryCode: "GB",
PartyId: "TWK",
Role: ocpi.CredentialsRoleRoleEMSP,
},
},
Token: "some-token-456",
Url: receiverServer.URL + "/ocpi/versions",
})
require.NoError(t, err)
token, _ := engine.LookupToken(context.Background(), "some-token-123")
err = ocpiApi.PushSession(context.Background(), *token)

require.NoError(t, err)
}

0 comments on commit 3418e6e

Please sign in to comment.