go get github.com/nikunicke/ftapi
The ftapi Go package lets you view and manage all available data from the 42 API. The implementation of the package is based on a type of Discovery Document found in the 42 API documentation. You can view it here. Unfortunately the document is missing crucial data, which results in some caveats on the package.
The 42 API uses Oauth 2.0 to handle authentication and authorization. To authenticate you need to have a registered application on the 42 Intra and a credentials.json file in the root of your project. The credentails file should include all necessary data for the Oauth2 authentication flow. If you are using client credentials grant type, you do not need redirect uris nor the auth uri.
{
"client_id": "YOU_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET",
"auth_uri": "https://api.intra.42.fr/oauth/authorize",
"token_uri": "https://api.intra.42.fr/oauth/token",
"scopes": ["THE", "SCOPES", "YOU", "NEED"],
"redirect_uris": ["urn:ietf:wg:oauth:2.0:oob","http://localhost"]
}
Please note that the authorization flow is currently only designed for command-line applications.
- Some types do not have any members and cannot really be used for anything. The response body is still available and can be decoded to a struct by your program. 💩
- Some struct members are of unknown type and therefore been defined as interface{}, requiring the user to use type assertions.
- Broadcasts, commands, exams_users, squads and squads_users are currently unavailable
To initiate a client service you need to specify the grant type. Currently supporting Client Credentials and Authorization Code grant types. Authorization Code will cache your token so that you do not need to login all the time.
// ftapi.AuthorizationCode or ftapi.ClientCredentials
service, err := ftapi.NewService(ftapi.AuthorizationCode)
if err != nil {
log.Fatalf("%v", err)
}
Get your user data (auth code grant type required):
me, err := service.Me()
if err != nil {
log.Fatalf("%v", err)
}
fmt.Println("Hello,", me.FirstName, me.LastName)
Initiate an events service:
events := ftapi.Events(service)
oneEvent, _ := events.Get("3647").Do()
eventsList, _ := events.List().P("campus_id", "13").Do() // P() is optional
for _, event := range eventsList.Events {
fmt.Println(event.Name)
}
POST/PUT/DELETE features