Skip to content

Commit

Permalink
Starting to get some kind of structure..
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzaadi committed Nov 21, 2011
1 parent 62b881b commit 3d5e553
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 65 deletions.
161 changes: 123 additions & 38 deletions Core/API/Gist.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,123 @@
using GithubSharp.Core.Services;

namespace GithubSharp.Core.API
{
public class Gist : Base.BaseAPI
{
public Gist(ICacheProvider CacheProvider, ILogProvider LogProvider) : base(CacheProvider, LogProvider) { }
//http://github.com/mattikus/pygist/blob/master/pygist.py
//http://github.com/miyagawa/gistp/blob/master/gistp
//http://github.com/defunkt/gist/blob/master/gist.rb
//http://gist.github.com/4277

//http://gist.github.com/api/v1/xml/search/gist
//http://gist.github.com/api/v1/xml/new

//Delete
//http://gist.github.com/delete/:id
// _method = delete ,authenticity_token,

//Edit
//http://gist.github.com/gists/:id/edit
// _method = put, authenticity_token, file_ext[fileN.ext] , file_contents[fileN.ext], file_name[fileN.ext]

//Get all gists for user
//http://gist.github.com/api/v1/xml/gists/erikzaadi

//Download
// /gists/:id/download

//Raw
//http://gist.github.com/{repo}.txt

//format - Parse to get the syntax highlighted html
//http://gist.github.com/291158.json

//http://gist.github.com/291158.js
}
}
using GithubSharp.Core.Services;
using System.Collections.Generic;

namespace GithubSharp.Core.API
{
public class Gist : Base.GithubApiBase
{
public Gist(
ILogProvider logProvider,
ICacheProvider cacheProvider,
IAuthProvider authProvider)
: base(
logProvider,
cacheProvider,
authProvider) { }

public IEnumerable<Models.Gist> UserGists(string username)
{
return base.Get<IEnumerable<Models.Gist>>(
string.Format(
"users/{0}/gists",
username),
"GET").Result;
}
public IEnumerable<Models.Gist> CurrentUserGists()
{
return base.Get<IEnumerable<Models.Gist>>(
string.Format(
"users/{0}/gists",
base.AuthProvider.Username),
"GET").Result;
}

public IEnumerable<Models.Gist> PublicGists()
{
return base.Get<IEnumerable<Models.Gist>>(
"gists/public",
"GET").Result;
}

public IEnumerable<Models.Gist> StarredGists()
{
return base.Get<IEnumerable<Models.Gist>>(
"gists/starred",
"GET").Result;
}

public Models.Gist Get(string id)
{
return base.Get<Models.Gist>(
string.Format(
"gists/{0}",
id),
"GET").Result;
}

public Models.Gist Create(Models.GistToCreateOrEdit gist)
{
return base.Get<Models.Gist, Models.GistToCreateOrEdit>(
"gists",
"POST",
gist).Result;
}

public Models.Gist Edit(string id, Models.GistToCreateOrEdit gist)
{
return base.Get<Models.Gist, Models.GistToCreateOrEdit>(
string.Format(
"gists/{0}",
id),
"PATCH",
gist).Result;
}

public void StarGist(string id)
{
base.Get(
string.Format(
"gists/{0}/star",
id),
"PUT");
}

public void UnstarGist(string id)
{
base.Get(
string.Format(
"gists/{0}/star",
id),
"DELETE");
}

public bool IsGistStarred(string id)
{
return base.Get(
string.Format(
"gists/{0}/star",
id),
"GET"
).StatusCode == 204;
}

public Models.Gist Fork(string id)
{
return base.Get<Models.Gist>(
string.Format(
"gists/{0}/fork",
id),
"POST").Result;
}

public void Delete(string id)
{
base.Get(
string.Format(
"gists/{0}",
id),
"DELETE");

}

}
}
64 changes: 64 additions & 0 deletions Core/Base/GithubApiBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;

namespace GithubSharp.Core.Base
{
public class GithubApiBase
{
public GithubApiBase (
Services.ILogProvider logProvider,
Services.ICacheProvider cacheProvider,
Services.IAuthProvider authProvider)
{
LogProvider = logProvider;
CacheProvider = cacheProvider;
AuthProvider = authProvider;
}

public Services.ILogProvider LogProvider { get;set;}
public Services.ICacheProvider CacheProvider { get;set;}
public Services.IAuthProvider AuthProvider { get;set;}

protected IGithubResponse Get(
string Path,
string Method)
{
return new GithubRequest(
LogProvider,
CacheProvider,
AuthProvider,
Path,
Method).GetResponse();
}

protected IGithubResponseWithReturnType<TReturnType> Get<TReturnType>(
string Path,
string Method)
where TReturnType : class
{
return new GithubRequestWithReturnType<TReturnType>(
LogProvider,
CacheProvider,
AuthProvider,
Path,
Method)
.GetResponseWithReturnType();
}

protected IGithubResponseWithReturnType<TReturnType> Get<TReturnType, TInputType>(
string Path,
string Method,
TInputType ToSend)
where TReturnType : class
{
return new GithubRequestWithInputAndReturnType<TInputType, TReturnType>(
LogProvider,
CacheProvider,
AuthProvider,
Path,
Method,
ToSend)
.GetResponseWithReturnType();
}
}
}

1 change: 1 addition & 0 deletions Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Compile Include="Models\ChangeStatus.cs" />
<Compile Include="GithubRequestWithInputAndReturnType.cs" />
<Compile Include="GithubError.cs" />
<Compile Include="Base\GithubApiBase.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
Expand Down
2 changes: 1 addition & 1 deletion Core/Models/Gist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class GistFile
}

[Serializable]
public class GistToCreate
public class GistToCreateOrEdit
{
public string description { get; set;}
public bool @public { get;set;}
Expand Down
2 changes: 2 additions & 0 deletions Core/Services/IAuthProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ IAuthPreRequestResponse PreRequestAuth(
string PrepareUri(string uri);
string GetToken();
void RestoreFromToken(string token);
bool IsAuthenticated {get;set;}
string Username {get;set;}
}

public interface IAuthResponse
Expand Down
15 changes: 0 additions & 15 deletions GithubSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,22 @@ Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Default|Any CPU = Default|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{26AB1C43-34F7-446C-A514-EB0E38730631}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{26AB1C43-34F7-446C-A514-EB0E38730631}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26AB1C43-34F7-446C-A514-EB0E38730631}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26AB1C43-34F7-446C-A514-EB0E38730631}.Release|Any CPU.Build.0 = Release|Any CPU
{308EBC64-BFA7-40E3-9855-20099B94C665}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{308EBC64-BFA7-40E3-9855-20099B94C665}.Debug|Any CPU.Build.0 = Debug|Any CPU
{308EBC64-BFA7-40E3-9855-20099B94C665}.Release|Any CPU.ActiveCfg = Release|Any CPU
{308EBC64-BFA7-40E3-9855-20099B94C665}.Release|Any CPU.Build.0 = Release|Any CPU
{33D4B918-DCF3-4A85-A353-ED141ECF7E3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{33D4B918-DCF3-4A85-A353-ED141ECF7E3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33D4B918-DCF3-4A85-A353-ED141ECF7E3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33D4B918-DCF3-4A85-A353-ED141ECF7E3B}.Release|Any CPU.Build.0 = Release|Any CPU
{38C76F61-476B-4821-A054-E5FC54F9EDA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{38C76F61-476B-4821-A054-E5FC54F9EDA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38C76F61-476B-4821-A054-E5FC54F9EDA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38C76F61-476B-4821-A054-E5FC54F9EDA5}.Release|Any CPU.Build.0 = Release|Any CPU
{3C916941-003B-480E-9981-2B9FEFFBF447}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C916941-003B-480E-9981-2B9FEFFBF447}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C916941-003B-480E-9981-2B9FEFFBF447}.Default|Any CPU.ActiveCfg = Debug|Any CPU
{3C916941-003B-480E-9981-2B9FEFFBF447}.Default|Any CPU.Build.0 = Debug|Any CPU
{3C916941-003B-480E-9981-2B9FEFFBF447}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C916941-003B-480E-9981-2B9FEFFBF447}.Release|Any CPU.Build.0 = Release|Any CPU
{44196ED8-BD8F-4D50-A906-A3B038F00570}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand All @@ -103,26 +96,18 @@ Global
{5EB1DF0E-EDBD-44EB-8A63-070F13A5D0EE}.Release|Any CPU.Build.0 = Release|Any CPU
{61EFFFA9-0713-47CA-95D9-193E16BBA3B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61EFFFA9-0713-47CA-95D9-193E16BBA3B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61EFFFA9-0713-47CA-95D9-193E16BBA3B9}.Default|Any CPU.ActiveCfg = Debug|Any CPU
{61EFFFA9-0713-47CA-95D9-193E16BBA3B9}.Default|Any CPU.Build.0 = Debug|Any CPU
{61EFFFA9-0713-47CA-95D9-193E16BBA3B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61EFFFA9-0713-47CA-95D9-193E16BBA3B9}.Release|Any CPU.Build.0 = Release|Any CPU
{A4F37523-CA27-4966-9BC1-51B74F77BE70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4F37523-CA27-4966-9BC1-51B74F77BE70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4F37523-CA27-4966-9BC1-51B74F77BE70}.Default|Any CPU.ActiveCfg = Debug|Any CPU
{A4F37523-CA27-4966-9BC1-51B74F77BE70}.Default|Any CPU.Build.0 = Debug|Any CPU
{A4F37523-CA27-4966-9BC1-51B74F77BE70}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4F37523-CA27-4966-9BC1-51B74F77BE70}.Release|Any CPU.Build.0 = Release|Any CPU
{D2AA69DA-E0F8-4583-B161-48E763DB8D23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2AA69DA-E0F8-4583-B161-48E763DB8D23}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2AA69DA-E0F8-4583-B161-48E763DB8D23}.Default|Any CPU.ActiveCfg = Debug|Any CPU
{D2AA69DA-E0F8-4583-B161-48E763DB8D23}.Default|Any CPU.Build.0 = Debug|Any CPU
{D2AA69DA-E0F8-4583-B161-48E763DB8D23}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2AA69DA-E0F8-4583-B161-48E763DB8D23}.Release|Any CPU.Build.0 = Release|Any CPU
{EF80C27A-4AE0-4B92-8270-B44D66996801}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF80C27A-4AE0-4B92-8270-B44D66996801}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF80C27A-4AE0-4B92-8270-B44D66996801}.Default|Any CPU.ActiveCfg = Debug|Any CPU
{EF80C27A-4AE0-4B92-8270-B44D66996801}.Default|Any CPU.Build.0 = Debug|Any CPU
{EF80C27A-4AE0-4B92-8270-B44D66996801}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF80C27A-4AE0-4B92-8270-B44D66996801}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
Expand Down
8 changes: 5 additions & 3 deletions NOTES
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Test Creation + Deletion of Gist to see that GithubRequestWithInput works

Think of a better way to inherit between GithubRequestWithInput and GithubRequestWithInputAndReturnType
Add pagination defaults to base api class
Continue to implement models to understand the needs of the base api class
Add attribute for an api method that needs authentication?
Add a simple parameterized string format for methods?
Add raw url helper methods - with content types?
10 changes: 10 additions & 0 deletions Plugins/AuthProviders/NullAuthProvider/NullAuthProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public GithubSharp.Core.Services.IAuthPreRequestResponse PreRequestAuth (
};
}

public bool IsAuthenticated
{
get { return true;}
set{ return;}
}


public string PrepareUri(string uri)
{
return uri;
Expand All @@ -47,7 +54,10 @@ public string GetToken ()

public void RestoreFromToken (string token)
{

}

public string Username {get;set;}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ namespace GithubSharp.Plugins.AuthProviders.UserPasswordAuthProvider
{
public class UserPasswordAuthProvider : GithubSharp.Core.Services.IAuthProvider
{
public string User {
get;
set;
}
public string Password {
get;
set;
Expand All @@ -20,8 +16,9 @@ public UserPasswordAuthProvider (string Token)

public UserPasswordAuthProvider (string user, string password)
{
User = user;
Username = user;
Password = password;
IsAuthenticated = true;
}

public GithubSharp.Core.Services.IAuthResponse Login ()
Expand All @@ -45,7 +42,7 @@ public GithubSharp.Core.Services.IAuthPreRequestResponse PreRequestAuth (
GithubSharp.Core.IGithubRequest githubRequest,
System.Net.HttpWebRequest webRequest)
{
var authInfo = string.Format("{0}:{1}",User, Password);
var authInfo = string.Format("{0}:{1}",Username, Password);
authInfo = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(authInfo));
webRequest.Headers["Authorization"] = "Basic " + authInfo;
return new GithubSharp.Core.Services.AuthPreRequestResponse
Expand All @@ -71,6 +68,9 @@ public void RestoreFromToken (string token)
//TODO : Unhash details here?
}

public bool IsAuthenticated {get;set;}
public string Username {get;set;}

}
}

1 change: 1 addition & 0 deletions Tests/CoreTests/CoreTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<ItemGroup>
<Compile Include="GithubRequestTest.cs" />
<Compile Include="TestSettings.cs" />
<Compile Include="GistModelTest.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
Loading

0 comments on commit 3d5e553

Please sign in to comment.