Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL support for parsing (issue #77) #78

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ A ICS / ICal parser and serialiser for Golang.
Because the other libraries didn't quite do what I needed.

Usage, parsing:
```
```golang
cal, err := ParseCalendar(strings.NewReader(input))

```

Creating:
Usage, parsing from a URL :
```golang
cal, err := ParseCalendar("an-ics-url")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but an example URL + a http client custom example might be nice. I've been contemplating making this more readable.

```

Creating:
```golang
cal := ics.NewCalendar()
cal.SetMethod(ics.MethodRequest)
event := cal.AddEvent(fmt.Sprintf("id@domain", p.SessionKey.IntID()))
Expand Down
49 changes: 49 additions & 0 deletions calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"net/http"
"time"
)

Expand Down Expand Up @@ -436,6 +437,54 @@ func (calendar *Calendar) Events() (r []*VEvent) {
return
}

type OnlineParsingOpts struct {
client *http.Client
req *http.Request
}

type OnlineParsingOptsFunc func(*OnlineParsingOpts)

func defaultOnlineParsingOpts(url string) *OnlineParsingOpts {
req, err := http.NewRequest("GET", url, nil)
if err != nil { //shouldn't return an error because NewRequest throws an error when context equals nil or http method doesn't exist. GET exists and the context is Background
panic(err) // so it should never panic
}
return &OnlineParsingOpts{
client: &http.Client{},
req: req,
}
}

func withCustomClient(client *http.Client) OnlineParsingOptsFunc {
return func(opo *OnlineParsingOpts) {
opo.client = client
}
}

func withCustomRequest(request *http.Request) OnlineParsingOptsFunc {
return func(opo *OnlineParsingOpts) {
opo.req = request
}
}

func ParseCalendarFromUrl(url string, opts ...OnlineParsingOptsFunc) (*Calendar, error) {
calendar_opts := defaultOnlineParsingOpts(url)
for _, fn := range opts {
fn(calendar_opts)
}
return parseCalendarFromUrl(calendar_opts.client, calendar_opts.req)
}

func parseCalendarFromUrl(client *http.Client, request *http.Request) (*Calendar, error) {
resp, err := client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()

return ParseCalendar(resp.Body)
}

func ParseCalendar(r io.Reader) (*Calendar, error) {
state := "begin"
c := &Calendar{}
Expand Down
15 changes: 14 additions & 1 deletion calendar_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ics

import (
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"os"
Expand All @@ -11,6 +10,8 @@ import (
"testing"
"time"
"unicode/utf8"

"github.com/stretchr/testify/assert"
)

func TestTimeParsing(t *testing.T) {
Expand Down Expand Up @@ -360,3 +361,15 @@ func TestIssue52(t *testing.T) {
t.Fatalf("cannot read test directory: %v", err)
}
}

func TestIssue77(t *testing.T) {
url := "https://proseconsult.umontpellier.fr/jsp/custom/modules/plannings/direct_cal.jsp?data=58c99062bab31d256bee14356aca3f2423c0f022cb9660eba051b2653be722c4c7f281e4e3ad06b85d3374100ac416a4dc5c094f7d1a811b903031bde802c7f50e0bd1077f9461bed8f9a32b516a3c63525f110c026ed6da86f487dd451ca812c1c60bb40b1502b6511435cf9908feb2166c54e36382c1aa3eb0ff5cb8980cdb,1"

cal, err := ParseCalendarFromUrl(url)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it would be making a real HTTPS request in tests. It should be using a mock http client.


if err != nil {
t.Fatalf("Error reading file: %s", err)
}

t.Log(cal.CalendarProperties)
}